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: 'example@example.com',
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?