Skip to content

Commit

Permalink
feat: measure identified events (#1114)
Browse files Browse the repository at this point in the history
* Add $is_identified event property

* Move to persistence properties
  • Loading branch information
robbie-c authored Apr 3, 2024
1 parent bdf84b8 commit 4e7c87d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('posthog core', () => {
Object.assign(this.props, properties)
},
props: {},
get_user_state: () => 'anonymous',
},
sessionPersistence: {
update_search_keyword: jest.fn(),
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -406,6 +407,7 @@ describe('posthog core', () => {
persistent: 'prop',
$window_id: 'windowId',
$session_id: 'sessionId',
$is_identified: false,
})
})

Expand All @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/posthog-persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down Expand Up @@ -107,6 +107,7 @@ describe('persistence', () => {
expect(library.properties()).toEqual({
'$feature/flag': 'variant',
'$feature/other': true,
$is_identified: false,
})
})
})
Expand Down Expand Up @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions src/posthog-persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ export class PostHogPersistence {
p[k] = v
}
})

p['$is_identified'] = this.get_user_state() === 'identified'

return p
}

Expand Down

0 comments on commit 4e7c87d

Please sign in to comment.