25

I am setting Mixpanel up, and I found out that if I log in with a user (and identify that user), log out and then re-register as a new user, the new user's details overwrite the previously logged in user (presumably when I call alias). How can I tell mixpanel that a user has logged out and to reset the identity token (make it anonymous again)?

Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
fredley
  • 32,953
  • 42
  • 145
  • 236

4 Answers4

14

I ran into the same issue, and after some sleuthing I discovered that you can manually clear the mixpanel cookies with mixpanel.cookie.clear().

However, you need to make sure that the mixpanel library has loaded, so I ended up putting it in a stupid timeout:

var id = window.setInterval(function() {
  if (mixpanel.cookie && mixpanel.cookie.clear) {
    mixpanel.cookie.clear();
    window.clearInterval(id);
  }
}, 50);

And then, since I didn't want to do this on every page, I added a query string parameter onto my logout redirect URL. So after visiting /logout it would redirect them to /home?_ref=logout, at which point I would clear the mixpanel cookie only if that query string parameter existed.

It was pretty annoying, but it seemed to work.

tmont
  • 2,562
  • 20
  • 15
  • 5
    For those with a single page app where you may never refresh, be sure to re-initialize immediately after you clear your cookies. `mixpanel.cookie.clear(); mixpanel.init("GARBAGE");` – kevinnuut Mar 03 '15 at 23:51
  • To be 100% sure Mixpanel loaded, you can use the `loaded` option in the `init` where you pass a function for it to run after it loads: `mixpanel.init("YOUR_TOKEN", { "loaded": function() { ... your clear code goes here ... } } );` – Anton Jun 12 '15 at 12:28
  • @kevinnuut why do we need to reinitialize again ? – gaurav5430 Sep 07 '21 at 07:25
11

It was released on Mixpanel Javascript version v2.8.0 the mixpanel.reset() function, so that's officially what should be called on user logout. See https://github.com/mixpanel/mixpanel-js/issues/67 .

Ricardo Nacif
  • 508
  • 4
  • 12
9

for android on logout you can use this MixpanelAPI.reset()

Omar Abdan
  • 1,901
  • 17
  • 29
4
  1. Call clearSuperProperties() to remove any Super Properties in local storage.
  2. Set new_distinct_id = UUID.randomUUID().toString());
  3. Call .identify(new_distinct_id) and .getPeople().identify(new_distinct_id)

This should wipe the device clean for a fresh user that can then register and be aliased to another distinct_id.

The best way to do this with javascript is to delete the cookie. The name of the cookie is mp_{mixpanel_token_id}_mixpanel

baikho
  • 5,203
  • 4
  • 40
  • 47
Karan Rana
  • 638
  • 4
  • 14