-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug/analytics reset no longer clears anonoymousId #655
bug/analytics reset no longer clears anonoymousId #655
Conversation
|
1baeca8
to
03c4cbb
Compare
void analytics.track('foo') | ||
expect(getAnonId()).toBeTruthy() | ||
analytics.reset() | ||
expect(getAnonId()).toBeFalsy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the race condition was that the track
call would eventually set anonId again, I think checking that it is falsey after the track call completes would cover it (and fail without these changes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually moved the test to a different file, tested both failure and success condition!
c50b59b
to
7e21da2
Compare
@@ -0,0 +1,70 @@ | |||
import cookie from 'js-cookie' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to make type safe
4e3a058
to
255c040
Compare
255c040
to
4507681
Compare
@@ -322,6 +322,48 @@ describe('Event Factory', () => { | |||
innerProp: '👻', | |||
}) | |||
}) | |||
|
|||
describe.skip('anonymousId', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests currently don't work? Are they regressions or existing issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not regressions, they are just existing edge cases / undefined behavior that I uncovered when working on this bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
@@ -205,6 +205,15 @@ export class EventFactory { | |||
} | |||
|
|||
public normalize(event: SegmentEvent): SegmentEvent { | |||
const anonymousIdOverride = event.options?.anonymousId |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this is more readble instead of the whole if block?
event.options?.anonymousId && this.user.anonymousId(event.options.anonymousId)
as event.anonymousId = id
seems to be a noop and happens regardless of this line of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
There was a race condition where the segment "normalize" plugin would asyncronously instantiate / overwrite the anonymousId field in browser storage right before the http request, thus making it impossible to call analytics.reset() after a typical method call. (for example, in a .track call). Moved this to the EventFactory so at the very least this browser storage action happens syncronously.
The only behavioral change is that if someone mutates event.anonymousId in a plug-in, it's not going to also implicitly update the global user anonymousId when that event gets sent.
this seems... fine to me? I wish we could go a step further and force everyone to call "setAnonymousId" explicitly if they wanted to override the global anonymousId for all events.