Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Dec 10, 2024
1 parent 63f64c6 commit 153c38a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
72 changes: 72 additions & 0 deletions src/__tests__/cookieless.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).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(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).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(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).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(null)
expect(event.properties.$window_id).toBe(null)
expect(event.properties.$cookieless).toEqual(true)
})
})
17 changes: 13 additions & 4 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,15 @@ export class PostHog {
this.featureFlags.receivedFeatureFlags({ featureFlags: activeFlags, featureFlagPayloads })
}

if (!this.get_distinct_id()) {
if (this.config.__preview_experimental_cookieless_mode) {
this.register_once(
{
distinct_id: COOKIELESS_SENTINEL_VALUE,
$device_id: null,
},
''
)
} else if (!this.get_distinct_id()) {
// There is no need to set the distinct id
// or the device id if something was already stored
// in the persistence
Expand Down Expand Up @@ -916,6 +924,10 @@ export class PostHog {
let properties = { ...event_properties }
properties['token'] = this.config.token

if (this.config.__preview_experimental_cookieless_mode) {
properties['$cookieless'] = true
}

if (event_name === '$snapshot') {
const persistenceProps = { ...this.persistence.properties(), ...this.sessionPersistence.properties() }
properties['distinct_id'] = persistenceProps.distinct_id
Expand Down Expand Up @@ -2057,9 +2069,6 @@ export class PostHog {
}

private _create_device_id(): string {
if (this.config.__preview_experimental_cookieless_mode) {
return COOKIELESS_SENTINEL_VALUE
}
return this.config.get_device_id(uuidv7())
}

Expand Down

0 comments on commit 153c38a

Please sign in to comment.