Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send idle markers in session #909

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading