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

Posthog properly handle Analytics ID changing from under us #10702

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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