diff --git a/src/__tests__/cookieless.test.ts b/src/__tests__/cookieless.test.ts index 95f056620..da1f55ba2 100644 --- a/src/__tests__/cookieless.test.ts +++ b/src/__tests__/cookieless.test.ts @@ -27,9 +27,9 @@ describe('cookieless', () => { 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(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 @@ -42,9 +42,9 @@ describe('cookieless', () => { 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(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 @@ -54,9 +54,9 @@ describe('cookieless', () => { 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.$cklsh).toEqual(true) + 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) @@ -65,9 +65,9 @@ describe('cookieless', () => { 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.$cklsh).toEqual(true) + expect(event.properties.$session_id).toBe(undefined) + expect(event.properties.$window_id).toBe(undefined) + expect(event.properties.$cklsh_mode).toEqual(true) // reset posthog.reset() @@ -80,9 +80,9 @@ describe('cookieless', () => { 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(event.properties.$session_id).toBe(undefined) + expect(event.properties.$window_id).toBe(undefined) + expect(event.properties.$cklsh_mode).toEqual(true) expect(document.cookie).toBe('') }) }) diff --git a/src/constants.ts b/src/constants.ts index cbc360d23..34b7a8bad 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -54,6 +54,7 @@ export const TOOLBAR_CONTAINER_CLASS = 'toolbar-global-fade-container' * Sentinel value for distinct id, device id, session id. Signals that the server should generate the value * */ export const COOKIELESS_SENTINEL_VALUE = '$posthog_cklsh' +export const COOKIELESS_MODE_FLAG_PROPERTY = '$cklsh_mode' export const WEB_EXPERIMENTS = '$web_experiments' diff --git a/src/extensions/replay/sessionrecording.ts b/src/extensions/replay/sessionrecording.ts index 0200c6832..a94350534 100644 --- a/src/extensions/replay/sessionrecording.ts +++ b/src/extensions/replay/sessionrecording.ts @@ -97,8 +97,8 @@ type SessionRecordingStatus = 'disabled' | 'sampled' | 'active' | 'buffering' | export interface SnapshotBuffer { size: number data: any[] - sessionId: string | null - windowId: string | null + sessionId: string + windowId: string } interface QueuedRRWebEvent { @@ -252,8 +252,8 @@ export class SessionRecording { private _linkedFlagSeen: boolean = false private _lastActivityTimestamp: number = Date.now() - private windowId: string | null - private sessionId: string | null + private windowId: string + private sessionId: string private _linkedFlag: string | FlagVariant | null = null private _fullSnapshotTimer?: ReturnType @@ -452,6 +452,9 @@ export class SessionRecording { logger.error('started without valid sessionManager') throw new Error(LOGGER_PREFIX + ' started without valid sessionManager. This is a bug.') } + if (this.instance.config.__preview_experimental_cookieless_mode) { + throw new Error(LOGGER_PREFIX + ' cannot be used with __preview_experimental_cookieless_mode.') + } // we know there's a sessionManager, so don't need to start without a session id const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId() diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 3bcb564b5..84e42334d 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -20,6 +20,7 @@ import { USER_STATE, ENABLE_PERSON_PROCESSING, COOKIELESS_SENTINEL_VALUE, + COOKIELESS_MODE_FLAG_PROPERTY, } from './constants' import { SessionRecording } from './extensions/replay/sessionrecording' import { Decide } from './decide' @@ -430,16 +431,20 @@ export class PostHog { this._retryQueue = new RetryQueue(this) this.__request_queue = [] - this.sessionManager = new SessionIdManager(this) - this.sessionPropsManager = new SessionPropsManager(this.sessionManager, this.persistence) + if (!this.config.__preview_experimental_cookieless_mode) { + this.sessionManager = new SessionIdManager(this) + this.sessionPropsManager = new SessionPropsManager(this.sessionManager, this.persistence) + } new TracingHeaders(this).startIfEnabledOrStop() this.siteApps = new SiteApps(this) this.siteApps?.init() - this.sessionRecording = new SessionRecording(this) - this.sessionRecording.startIfEnabledOrStop() + if (!this.config.__preview_experimental_cookieless_mode) { + this.sessionRecording = new SessionRecording(this) + this.sessionRecording.startIfEnabledOrStop() + } if (!this.config.disable_scroll_properties) { this.scrollManager.startMeasuringScrollPosition() @@ -926,9 +931,7 @@ export class PostHog { if (this.config.__preview_experimental_cookieless_mode) { // Set a flag to tell the plugin server to use cookieless server hash mode - // on naming: $cklsh = cookieless server hash - // I didn't call it $cookieless, as it's possible to use cookies in this mode (after consent is given) - properties['$cklsh'] = true + properties[COOKIELESS_MODE_FLAG_PROPERTY] = true } if (event_name === '$snapshot') { @@ -946,7 +949,7 @@ export class PostHog { const infoProperties = Info.properties() - if (this.sessionManager) { + if (this.sessionManager && !this.config.__preview_experimental_cookieless_mode) { const { sessionId, windowId } = this.sessionManager.checkAndGetSessionAndWindowId() properties['$session_id'] = sessionId properties['$window_id'] = windowId diff --git a/src/sessionid.ts b/src/sessionid.ts index 0ef93b2b8..43480c4e3 100644 --- a/src/sessionid.ts +++ b/src/sessionid.ts @@ -216,12 +216,9 @@ export class SessionIdManager { */ checkAndGetSessionAndWindowId(readOnly = false, _timestamp: number | null = null) { if (this.config.__preview_experimental_cookieless_mode) { - return { - sessionId: null, - windowId: null, - sessionStartTimestamp: null, - lastActivityTimestamp: null, - } + throw new Error( + 'checkAndGetSessionAndWindowId should not be called in __preview_experimental_cookieless_mode' + ) } const timestamp = _timestamp || new Date().getTime()