From f9b12957016790f7cb0fd187537b3c6336b0c5fc Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Thu, 12 Dec 2024 10:35:24 +0000 Subject: [PATCH] Handle reset --- src/__tests__/cookieless.test.ts | 24 ++++++++++++++++++++---- src/posthog-core.ts | 28 ++++++++++++++++++---------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/__tests__/cookieless.test.ts b/src/__tests__/cookieless.test.ts index 9ad89ccd7..95f056620 100644 --- a/src/__tests__/cookieless.test.ts +++ b/src/__tests__/cookieless.test.ts @@ -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 @@ -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 @@ -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) @@ -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('') }) }) diff --git a/src/posthog-core.ts b/src/posthog-core.ts index f51ae83b8..3bcb564b5 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -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, + }, + '' + ) + } } /**