Skip to content

Commit

Permalink
Don't enable session id manager or replay when in cookieless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Dec 13, 2024
1 parent f9b1295 commit 833d570
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
30 changes: 15 additions & 15 deletions src/__tests__/cookieless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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('')
})
})
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
11 changes: 7 additions & 4 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<typeof setInterval>
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 11 additions & 8 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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') {
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/sessionid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 833d570

Please sign in to comment.