From 4e7c87d35ef0d6371059f258259b24983e560887 Mon Sep 17 00:00:00 2001 From: Robbie Date: Wed, 3 Apr 2024 19:06:20 +0100 Subject: [PATCH] feat: measure identified events (#1114) * Add $is_identified event property * Move to persistence properties --- src/__tests__/posthog-core.js | 7 +++++-- src/__tests__/posthog-persistence.test.ts | 5 +++-- src/posthog-persistence.ts | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index 83bac25e4..3bbda2371 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -60,6 +60,7 @@ describe('posthog core', () => { Object.assign(this.props, properties) }, props: {}, + get_user_state: () => 'anonymous', }, sessionPersistence: { update_search_keyword: jest.fn(), @@ -369,7 +370,7 @@ describe('posthog core', () => { given('overrides', () => ({ config: given.config, persistence: { - properties: () => ({ distinct_id: 'abc', persistent: 'prop' }), + properties: () => ({ distinct_id: 'abc', persistent: 'prop', $is_identified: false }), remove_event_timer: jest.fn(), }, sessionPersistence: { @@ -406,6 +407,7 @@ describe('posthog core', () => { persistent: 'prop', $window_id: 'windowId', $session_id: 'sessionId', + $is_identified: false, }) }) @@ -426,11 +428,12 @@ describe('posthog core', () => { $window_id: 'windowId', $session_id: 'sessionId', $lib_custom_api_host: 'https://custom.posthog.com', + $is_identified: false, }) }) it('respects property_denylist and property_blacklist', () => { - given('property_denylist', () => ['$lib', 'persistent']) + given('property_denylist', () => ['$lib', 'persistent', '$is_identified']) given('property_blacklist', () => ['token']) expect(given.subject).toEqual({ diff --git a/src/__tests__/posthog-persistence.test.ts b/src/__tests__/posthog-persistence.test.ts index b74964ec5..04be185d4 100644 --- a/src/__tests__/posthog-persistence.test.ts +++ b/src/__tests__/posthog-persistence.test.ts @@ -54,7 +54,7 @@ describe('persistence', () => { const lib = new PostHogPersistence(makePostHogConfig('bla', persistenceMode)) lib.register({ distinct_id: 'testy', test_prop: 'test_value' }) lib.set_user_state('identified') - expect(lib.properties()).toEqual({ distinct_id: 'testy', test_prop: 'test_value' }) + expect(lib.properties()).toEqual({ distinct_id: 'testy', test_prop: 'test_value', $is_identified: true }) }) it(`should only call save if props changes`, () => { @@ -107,6 +107,7 @@ describe('persistence', () => { expect(library.properties()).toEqual({ '$feature/flag': 'variant', '$feature/other': true, + $is_identified: false, }) }) }) @@ -169,7 +170,7 @@ describe('persistence', () => { }) it('should allow swapping between storage methods', () => { - const expectedProps = () => ({ distinct_id: 'test', test_prop: 'test_val' }) + const expectedProps = () => ({ distinct_id: 'test', test_prop: 'test_val', $is_identified: false }) let config = makePostHogConfig('test', 'localStorage+cookie') const lib = new PostHogPersistence(makePostHogConfig('test', 'localStorage+cookie')) lib.register(expectedProps()) diff --git a/src/posthog-persistence.ts b/src/posthog-persistence.ts index 245e9380e..b00adf83a 100644 --- a/src/posthog-persistence.ts +++ b/src/posthog-persistence.ts @@ -111,6 +111,9 @@ export class PostHogPersistence { p[k] = v } }) + + p['$is_identified'] = this.get_user_state() === 'identified' + return p }