Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Posthog properly handle Analytics ID changing from under us (#10702)
Browse files Browse the repository at this point in the history
* Posthog properly handle Analytics ID changing from under us

* Update tests
  • Loading branch information
t3chguy authored Apr 26, 2023
1 parent 857e22f commit e4610e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PosthogAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ export class PosthogAnalytics {
Object.assign({ id: analyticsID }, accountData),
);
}
if (this.posthog.get_distinct_id() === analyticsID) {
// No point identifying again
return;
}
if (this.posthog.persistence.get_user_state() === "identified") {
// Analytics ID has changed, reset as Posthog will refuse to merge in this case
this.posthog.reset();
}
this.posthog.identify(analyticsID);
} catch (e) {
// The above could fail due to network requests, but not essential to starting the application,
Expand Down
4 changes: 4 additions & 0 deletions test/PosthogAnalytics-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const getFakePosthog = (): PostHog =>
identify: jest.fn(),
reset: jest.fn(),
register: jest.fn(),
get_distinct_id: jest.fn(),
persistence: {
get_user_state: jest.fn(),
},
} as unknown as PostHog);

interface ITestEvent extends IPosthogEvent {
Expand Down

0 comments on commit e4610e4

Please sign in to comment.