diff --git a/src/entrypoints/tracing-headers.ts b/src/entrypoints/tracing-headers.ts index 06abc4e01..a4148a45f 100644 --- a/src/entrypoints/tracing-headers.ts +++ b/src/entrypoints/tracing-headers.ts @@ -4,8 +4,12 @@ import { assignableWindow, window } from '../utils/globals' const addTracingHeaders = (sessionManager: SessionIdManager, req: Request) => { const { sessionId, windowId } = sessionManager.checkAndGetSessionAndWindowId(true) - req.headers.set('X-POSTHOG-SESSION-ID', sessionId) - req.headers.set('X-POSTHOG-WINDOW-ID', windowId) + if (sessionId) { + req.headers.set('X-POSTHOG-SESSION-ID', sessionId) + } + if (windowId) { + req.headers.set('X-POSTHOG-WINDOW-ID', windowId) + } } const patchFetch = (sessionManager: SessionIdManager): (() => void) => { diff --git a/src/extensions/replay/sessionrecording.ts b/src/extensions/replay/sessionrecording.ts index 030501aab..0200c6832 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 - windowId: string + sessionId: string | null + windowId: string | null } interface QueuedRRWebEvent { @@ -252,8 +252,8 @@ export class SessionRecording { private _linkedFlagSeen: boolean = false private _lastActivityTimestamp: number = Date.now() - private windowId: string - private sessionId: string + private windowId: string | null + private sessionId: string | null private _linkedFlag: string | FlagVariant | null = null private _fullSnapshotTimer?: ReturnType @@ -315,6 +315,9 @@ export class SessionRecording { private get sessionDuration(): number | null { const mostRecentSnapshot = this.buffer?.data[this.buffer?.data.length - 1] const { sessionStartTimestamp } = this.sessionManager.checkAndGetSessionAndWindowId(true) + if (sessionStartTimestamp == null) { + return null + } return mostRecentSnapshot ? mostRecentSnapshot.timestamp - sessionStartTimestamp : null } diff --git a/src/sessionid.ts b/src/sessionid.ts index b2e31d759..0ef93b2b8 100644 --- a/src/sessionid.ts +++ b/src/sessionid.ts @@ -217,10 +217,10 @@ export class SessionIdManager { checkAndGetSessionAndWindowId(readOnly = false, _timestamp: number | null = null) { if (this.config.__preview_experimental_cookieless_mode) { return { - sessionId: undefined, - windowId: undefined, - sessionStartTimestamp: undefined, - lastActivityTimestamp: undefined, + sessionId: null, + windowId: null, + sessionStartTimestamp: null, + lastActivityTimestamp: null, } } const timestamp = _timestamp || new Date().getTime()