mixpanel-js icon indicating copy to clipboard operation
mixpanel-js copied to clipboard

Reset Mixpanel before init

Open levrik opened this issue 1 year ago • 0 comments

I need to reset Mixpanel (mixpanel.reset()) when a redirect to login is triggered due to an expired session. As I only initialize Mixpanel for authenticated users, the call to mixpanel.reset() fails as it runs before mixpanel.init(...) in this case.

That's how I currently work around this limitation by resetting Mixpanel immediately after initialization if the user ID has changed from what Mixpanel persisted.

mixpanel.init(token);

const previousUserId = mixpanel.get_distinct_id();
if (previousUserId && previousUserId !== userInfo.id) {
  mixpanel.reset();
}

mixpanel.identify(userInfo.id);

// @ts-expect-error: Private API
mixpanel._init_url_change_tracking('url-with-path');

I think it would be very useful to be able to call mixpanel.reset() also without having called mixpanel.init(...) before. That way I could simply do the reset whenever the code triggers a redirect to the external login page.

As you can see there's also another workaround towards the end to delay the SPA pageview tracking to prevent Mixpanel from tracking a pageview with a persisted outdated user ID.

If you have an idea to better solve this with current functionality, let me know.

levrik avatar Feb 13 '24 14:02 levrik