Skip to content

Commit

Permalink
typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Dec 10, 2024
1 parent 3b3da0e commit d7d71ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/entrypoints/tracing-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
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
windowId: string
sessionId: string | null
windowId: string | null
}

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
private sessionId: string
private windowId: string | null
private sessionId: string | null
private _linkedFlag: string | FlagVariant | null = null

private _fullSnapshotTimer?: ReturnType<typeof setInterval>
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions src/sessionid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d7d71ef

Please sign in to comment.