Is reset() the only way to remove userId from segment analytics in typescript

570 Views Asked by At

I am using the @segment/analytics-next library to track the segment event. Below is the code I initialize a segment analytic and set a userId

import { AnalyticsBrowser } from '@segment/analytics-next';
let segmentAnalytics = new AnalyticsBrowser('YOUR_WRITE_KEY');
segmentAnalytics.identify('userId123', {
  email: '[email protected]',
  name: 'Example User'
});

The above code is working well, and it can successfully set a userId.

But now I am facing another issue, whenever the user is logged out, I want to set the userId back to null, but seems the typescript/segment is not providing the function.

I used segmentAnalytics.identify(null) to do, no error report, but cannot wipe the userId, all events still carry the userId.

And even from the official document, no function provided for wipe the userId. https://segment.com/docs/connections/spec/best-practices-identify/

It just tell If you call reset during a user’s browser session, it removes both their userId and anonymousId, which means the user generates a new anonymousId on the next visit.

this means I need to get the anonymousId first, then reset() the segmentAnalytics, after that I need to use setAnonymousId to assign the old anonymousId to the new segmentAnalytics after reset. This is such an inelegant way.

I think this is not quite friendly like if the user log out, I want to use the same anonymous id, with null as userId.

Any experienced stackoverflower have some idea of how to wipe it without using reset() function?

1

There are 1 best solutions below

0
On

In the browser, according to Segment docs:

Segment writes the user’s IDs to the user’s local storage, and uses that as the Segment ID on the cookie whenever possible. ... If a user returns to your site after the cookie expires, Analytics.js looks for an old ID in the user’s localStorage, and if one is found, sets it as the user’s ID again in the new cookie. If a user clears their cookies and localstorage, all of the IDs are removed, and the user gets a completely new anonymousID when they next visit the page.

So, if you wanted to clear the ID imperatively, you'd need to nuke both localStorage and the cookie. But... that'd wipe out both the userId and the anonymousId.

If you want to hang onto the original anonymousId, there's two basic ways to do it:

  1. Instead of letting Segment set anonymousId's for you, set your own.
  2. And/or, you can read the existing one, with analytics.user().anonymousId()

In either case, once you have it, stash it somewhere durable, so you can pull it back out and reassign it after a reset().

A thought: do you really need to reset() the user on logout? Even if there are multiple users on the same browser, it'll replace the user data for you once you call identify() for the next one. Worst case, you could call reset() yourself just before calling identify(). In that case, you don't know which anonymous user you're dealing with anyway, so saving the id doesn't get you much. So, even if you need to robustly support logout and login for multiple users, I think you get what you want by simply letting Segment do its thing organically, resetting only when a new user is id'd, and/or as a solution for a specific mis-behavior.