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

Commit e4610e4

Browse files
authored
Posthog properly handle Analytics ID changing from under us (#10702)
* Posthog properly handle Analytics ID changing from under us * Update tests
1 parent 857e22f commit e4610e4

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/PosthogAnalytics.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ export class PosthogAnalytics {
312312
Object.assign({ id: analyticsID }, accountData),
313313
);
314314
}
315+
if (this.posthog.get_distinct_id() === analyticsID) {
316+
// No point identifying again
317+
return;
318+
}
319+
if (this.posthog.persistence.get_user_state() === "identified") {
320+
// Analytics ID has changed, reset as Posthog will refuse to merge in this case
321+
this.posthog.reset();
322+
}
315323
this.posthog.identify(analyticsID);
316324
} catch (e) {
317325
// The above could fail due to network requests, but not essential to starting the application,

test/PosthogAnalytics-test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const getFakePosthog = (): PostHog =>
3333
identify: jest.fn(),
3434
reset: jest.fn(),
3535
register: jest.fn(),
36+
get_distinct_id: jest.fn(),
37+
persistence: {
38+
get_user_state: jest.fn(),
39+
},
3640
} as unknown as PostHog);
3741

3842
interface ITestEvent extends IPosthogEvent {

0 commit comments

Comments
 (0)