-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web-analytics): Add flag to send server hash instead of distinct…
… id (#1490) * Add flag to send server hash instead of distinct id * Use SENTINEL_COOKIELESS_SERVER_HASH for session ids too * WIP * Driveby * Package version * Rename the sentinel const * Comments and naming * Send null for session id and window id * typescript * bump posthog in next playground * Add a test * Revert get device id changes * Add a comment * Remove cklsh from next playground * Handle reset * Don't enable session id manager or replay when in cookieless mode * Make tracing headers support optional SessionIdManager * Revert session duration * Remove unnecessary comment * Remove check
- Loading branch information
Showing
12 changed files
with
179 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { defaultPostHog } from './helpers/posthog-instance' | ||
import type { PostHogConfig } from '../types' | ||
import { uuidv7 } from '../uuidv7' | ||
|
||
describe('cookieless', () => { | ||
const eventName = 'custom_event' | ||
const eventProperties = { | ||
event: 'prop', | ||
} | ||
const identifiedDistinctId = 'user-1' | ||
const setup = (config: Partial<PostHogConfig> = {}, token: string = uuidv7()) => { | ||
const beforeSendMock = jest.fn().mockImplementation((e) => e) | ||
const posthog = defaultPostHog().init(token, { ...config, before_send: beforeSendMock }, token)! | ||
posthog.debug() | ||
return { posthog, beforeSendMock } | ||
} | ||
|
||
it('should send events with the sentinel distinct id', () => { | ||
const { posthog, beforeSendMock } = setup({ | ||
persistence: 'memory', | ||
__preview_experimental_cookieless_mode: true, | ||
}) | ||
|
||
posthog.capture(eventName, eventProperties) | ||
expect(beforeSendMock).toBeCalledTimes(1) | ||
let event = beforeSendMock.mock.calls[0][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(undefined) | ||
expect(event.properties.$window_id).toBe(undefined) | ||
expect(event.properties.$cklsh_mode).toEqual(true) | ||
expect(document.cookie).toBe('') | ||
|
||
// simulate user giving cookie consent | ||
posthog.set_config({ persistence: 'localStorage+cookie' }) | ||
|
||
// send an event after consent | ||
posthog.capture(eventName, eventProperties) | ||
expect(beforeSendMock).toBeCalledTimes(2) | ||
event = beforeSendMock.mock.calls[1][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(undefined) | ||
expect(event.properties.$window_id).toBe(undefined) | ||
expect(event.properties.$cklsh_mode).toEqual(true) | ||
expect(document.cookie).not.toBe('') | ||
|
||
// a user identifying | ||
posthog.identify(identifiedDistinctId) | ||
expect(beforeSendMock).toBeCalledTimes(3) | ||
event = beforeSendMock.mock.calls[2][0] | ||
expect(event.properties.distinct_id).toBe(identifiedDistinctId) | ||
expect(event.properties.$anon_distinct_id).toBe('$posthog_cklsh') | ||
expect(event.properties.$device_id).toBe(null) | ||
expect(event.properties.$session_id).toBe(undefined) | ||
expect(event.properties.$window_id).toBe(undefined) | ||
expect(event.properties.$cklsh_mode).toEqual(true) | ||
|
||
// an event after identifying | ||
posthog.capture(eventName, eventProperties) | ||
expect(beforeSendMock).toBeCalledTimes(4) | ||
event = beforeSendMock.mock.calls[3][0] | ||
expect(event.properties.distinct_id).toBe(identifiedDistinctId) | ||
expect(event.properties.$anon_distinct_id).toBe(undefined) | ||
expect(event.properties.$device_id).toBe(null) | ||
expect(event.properties.$session_id).toBe(undefined) | ||
expect(event.properties.$window_id).toBe(undefined) | ||
expect(event.properties.$cklsh_mode).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(undefined) | ||
expect(event.properties.$window_id).toBe(undefined) | ||
expect(event.properties.$cklsh_mode).toEqual(true) | ||
expect(document.cookie).toBe('') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters