Skip to content

Commit

Permalink
feat: send idle markers in session (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Nov 23, 2023
1 parent df8e08f commit b5c17bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function makeDecideResponse(partialResponse: Partial<DecideResponse>) {
}

describe('SessionRecording', () => {
const _addCustomEvent = jest.fn()
let _emit: any
let posthog: PostHog
let sessionRecording: SessionRecording
Expand All @@ -54,6 +55,8 @@ describe('SessionRecording', () => {

beforeEach(() => {
assignableWindow.rrwebRecord = jest.fn()
_addCustomEvent.mockClear()
assignableWindow.rrwebRecord = _addCustomEvent
assignableWindow.rrwebConsoleRecord = {
getRecordConsolePlugin: jest.fn(),
}
Expand Down Expand Up @@ -283,6 +286,7 @@ describe('SessionRecording', () => {
return () => {}
})
assignableWindow.rrwebRecord.takeFullSnapshot = mockFullSnapshot
assignableWindow.rrwebRecord.addCustomEvent = _addCustomEvent
;(loadScript as any).mockImplementation((_path: any, callback: any) => callback())
})

Expand Down Expand Up @@ -369,7 +373,7 @@ describe('SessionRecording', () => {
expect(sessionRecording['sessionId']).not.toBe(lastSessionId)
lastSessionId = sessionRecording['sessionId']

emitValues.push(sessionRecording.status)
emitValues.push(sessionRecording['status'])
}

// the random number generator won't always be exactly 0.5, but it should be close
Expand Down Expand Up @@ -998,6 +1002,11 @@ describe('SessionRecording', () => {
timestamp: lastActivityTimestamp + RECORDING_IDLE_ACTIVITY_TIMEOUT_MS + 1000,
})
expect(sessionRecording['isIdle']).toEqual(true)
expect(_addCustomEvent).toHaveBeenCalledWith('sessionIdle', {
reason: 'user inactivity',
threshold: 300000,
timeSinceLastActive: 300900,
})
expect(sessionRecording['_lastActivityTimestamp']).toEqual(lastActivityTimestamp + 100)
expect(assignableWindow.rrwebRecord.takeFullSnapshot).toHaveBeenCalledTimes(1)

Expand All @@ -1011,6 +1020,10 @@ describe('SessionRecording', () => {
timestamp: lastActivityTimestamp + RECORDING_IDLE_ACTIVITY_TIMEOUT_MS + 2000,
})
expect(sessionRecording['isIdle']).toEqual(false)
expect(_addCustomEvent).toHaveBeenCalledWith('sessionNoLongerIdle', {
reason: 'user activity',
type: INCREMENTAL_SNAPSHOT_EVENT_TYPE,
})
expect(sessionRecording['_lastActivityTimestamp']).toEqual(
lastActivityTimestamp + RECORDING_IDLE_ACTIVITY_TIMEOUT_MS + 2000
)
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ export class SessionRecording {
// We check if the lastActivityTimestamp is old enough to go idle
if (event.timestamp - this._lastActivityTimestamp > RECORDING_IDLE_ACTIVITY_TIMEOUT_MS) {
this.isIdle = true
this.rrwebRecord?.addCustomEvent('sessionIdle', {
reason: 'user inactivity',
timeSinceLastActive: event.timestamp - this._lastActivityTimestamp,
threshold: RECORDING_IDLE_ACTIVITY_TIMEOUT_MS,
})
}
}

Expand All @@ -397,6 +402,10 @@ export class SessionRecording {
if (this.isIdle) {
// Remove the idle state if set and trigger a full snapshot as we will have ignored previous mutations
this.isIdle = false
this.rrwebRecord?.addCustomEvent('sessionNoLongerIdle', {
reason: 'user activity',
type: event.type,
})
this._tryTakeFullSnapshot()
}
}
Expand Down

0 comments on commit b5c17bf

Please sign in to comment.