Skip to content
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

Merged
merged 3 commits into from
Nov 6, 2022

Conversation

silesky
Copy link
Contributor

@silesky silesky commented Nov 4, 2022

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.

@changeset-bot
Copy link

changeset-bot bot commented Nov 4, 2022

⚠️ No Changeset found

Latest commit: c86553d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@silesky silesky force-pushed the bug/analytics-reset-no-longer-clears-anonoymousId branch 2 times, most recently from 1baeca8 to 03c4cbb Compare November 4, 2022 03:59
@silesky silesky requested review from chrisradek, pooyaj and a team November 4, 2022 04:36
void analytics.track('foo')
expect(getAnonId()).toBeTruthy()
analytics.reset()
expect(getAnonId()).toBeFalsy()
Copy link
Contributor

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)

Copy link
Contributor Author

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!

@silesky silesky force-pushed the bug/analytics-reset-no-longer-clears-anonoymousId branch from c50b59b to 7e21da2 Compare November 4, 2022 19:35
@silesky silesky requested a review from chrisradek November 4, 2022 19:35
@@ -0,0 +1,70 @@
import cookie from 'js-cookie'
Copy link
Contributor Author

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

@silesky silesky force-pushed the bug/analytics-reset-no-longer-clears-anonoymousId branch 4 times, most recently from 4e3a058 to 255c040 Compare November 4, 2022 20:41
@silesky silesky force-pushed the bug/analytics-reset-no-longer-clears-anonoymousId branch from 255c040 to 4507681 Compare November 4, 2022 20:46
@@ -322,6 +322,48 @@ describe('Event Factory', () => {
innerProp: '👻',
})
})

describe.skip('anonymousId', () => {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@chrisradek chrisradek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

Copy link
Contributor

@pooyaj pooyaj left a 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
Copy link
Contributor

@pooyaj pooyaj Nov 4, 2022

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@silesky silesky merged commit 5e3f077 into master Nov 6, 2022
@silesky silesky deleted the bug/analytics-reset-no-longer-clears-anonoymousId branch November 6, 2022 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants