Skip to content

Commit

Permalink
Handle reset
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Dec 12, 2024
1 parent dcd2d7f commit f9b1295
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
24 changes: 20 additions & 4 deletions src/__tests__/cookieless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('cookieless', () => {
expect(event.properties.$device_id).toBe(null)
expect(event.properties.$session_id).toBe(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).toEqual(true)
expect(event.properties.$cklsh).toEqual(true)
expect(document.cookie).toBe('')

// simulate user giving cookie consent
Expand All @@ -44,7 +44,7 @@ describe('cookieless', () => {
expect(event.properties.$device_id).toBe(null)
expect(event.properties.$session_id).toBe(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).toEqual(true)
expect(event.properties.$cklsh).toEqual(true)
expect(document.cookie).not.toBe('')

// a user identifying
Expand All @@ -56,7 +56,7 @@ describe('cookieless', () => {
expect(event.properties.$device_id).toBe(null)
expect(event.properties.$session_id).toBe(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).toEqual(true)
expect(event.properties.$cklsh).toEqual(true)

// an event after identifying
posthog.capture(eventName, eventProperties)
Expand All @@ -67,6 +67,22 @@ describe('cookieless', () => {
expect(event.properties.$device_id).toBe(null)
expect(event.properties.$session_id).toBe(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).toEqual(true)
expect(event.properties.$cklsh).toEqual(true)

// reset
posthog.reset()
posthog.set_config({ persistence: 'memory' })

// an event after reset
posthog.capture(eventName, eventProperties)
expect(beforeSendMock).toBeCalledTimes(5)
event = beforeSendMock.mock.calls[4][0]
expect(event.properties.distinct_id).toBe('$posthog_cklsh')
expect(event.properties.$anon_distinct_id).toBe(undefined)
expect(event.properties.$device_id).toBe(null)
expect(event.properties.$session_id).toBe(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cklsh).toEqual(true)
expect(document.cookie).toBe('')
})
})
28 changes: 18 additions & 10 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1549,16 +1549,24 @@ export class PostHog {
this.surveys?.reset()
this.persistence?.set_property(USER_STATE, 'anonymous')
this.sessionManager?.resetSessionId()
const uuid = this.config.__preview_experimental_cookieless_mode
? COOKIELESS_SENTINEL_VALUE
: this.config.get_device_id(uuidv7())
this.register_once(
{
distinct_id: uuid,
$device_id: reset_device_id ? uuid : device_id,
},
''
)
if (this.config.__preview_experimental_cookieless_mode) {
this.register_once(
{
distinct_id: COOKIELESS_SENTINEL_VALUE,
$device_id: null,
},
''
)
} else {
const uuid = this.config.get_device_id(uuidv7())
this.register_once(
{
distinct_id: uuid,
$device_id: reset_device_id ? uuid : device_id,
},
''
)
}
}

/**
Expand Down

0 comments on commit f9b1295

Please sign in to comment.