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

fix: continue recording after reset #1135

Merged
merged 4 commits into from
Apr 12, 2024
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
44 changes: 38 additions & 6 deletions cypress/e2e/session-recording.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function ensureRecordingIsStopped() {
})
}

function ensureActivitySendsSnapshots() {
function ensureActivitySendsSnapshots(initial = true) {
cy.resetPhCaptures()

cy.get('[data-cy-input]')
Expand All @@ -31,12 +31,15 @@ function ensureActivitySendsSnapshots() {
// a meta and then a full snapshot
expect(captures[0]['properties']['$snapshot_data'][0].type).to.equal(4) // meta
expect(captures[0]['properties']['$snapshot_data'][1].type).to.equal(2) // full_snapshot
expect(captures[0]['properties']['$snapshot_data'][2].type).to.equal(5) // custom event with options
expect(captures[0]['properties']['$snapshot_data'][3].type).to.equal(5) // custom event with posthog config
// Not sent if recording is not stopped but session_id changes (e.g. reset called)
if (initial) {
expect(captures[0]['properties']['$snapshot_data'][2].type).to.equal(5) // custom event with options
expect(captures[0]['properties']['$snapshot_data'][3].type).to.equal(5) // custom event with posthog config
}
// Making a set from the rest should all be 3 - incremental snapshots
expect(new Set(captures[0]['properties']['$snapshot_data'].slice(4).map((s) => s.type))).to.deep.equal(
new Set([3])
)
expect(
new Set(captures[0]['properties']['$snapshot_data'].slice(initial ? 4 : 2).map((s) => s.type))
).to.deep.equal(new Set([3]))
})
})
}
Expand Down Expand Up @@ -362,6 +365,35 @@ describe('Session recording', () => {
})
})
})

it('starts a new recording after calling reset', () => {
cy.phCaptures({ full: true }).then((captures) => {
// should be a pageview at the beginning
expect(captures.map((c) => c.event)).to.deep.equal(['$pageview'])
})
cy.resetPhCaptures()

let startingSessionId: string | null = null
cy.posthog().then((ph) => {
startingSessionId = ph.get_session_id()
})

ensureActivitySendsSnapshots()

cy.posthog().then((ph) => {
ph.reset()
})

ensureActivitySendsSnapshots(false)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't call ensureActivitySendsSnapshots() again here because two custom events were missing: Screenshot 2024-04-12 at 12 46 32

I wasn't sure where these were coming from but assumed they may be fired after RRWeb initialises and hence won't fire a second time

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's alter that ensure or duplicate it so we can test for any snapshots maybe?

// the session id is rotated after reset is called
cy.posthog().then((ph) => {
const secondSessionId = ph.get_session_id()
expect(startingSessionId).not.to.be.null
expect(secondSessionId).not.to.be.null
expect(secondSessionId).not.to.equal(startingSessionId)
})
})
})

describe('with sampling', () => {
Expand Down
59 changes: 35 additions & 24 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,30 +357,7 @@ export class SessionRecording {
}

afterDecideResponse(response: DecideResponse) {
const receivedSampleRate = response.sessionRecording?.sampleRate
const parsedSampleRate = _isNullish(receivedSampleRate) ? null : parseFloat(receivedSampleRate)

const receivedMinimumDuration = response.sessionRecording?.minimumDurationMilliseconds

if (this.instance.persistence) {
this.instance.persistence.register({
[SESSION_RECORDING_ENABLED_SERVER_SIDE]: !!response['sessionRecording'],
[CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: response.sessionRecording?.consoleLogRecordingEnabled,
[SESSION_RECORDING_NETWORK_PAYLOAD_CAPTURE]: {
capturePerformance: response.capturePerformance,
...response.sessionRecording?.networkPayloadCapture,
},
[SESSION_RECORDING_CANVAS_RECORDING]: {
enabled: response.sessionRecording?.recordCanvas,
fps: response.sessionRecording?.canvasFps,
quality: response.sessionRecording?.canvasQuality,
},
[SESSION_RECORDING_SAMPLE_RATE]: parsedSampleRate,
[SESSION_RECORDING_MINIMUM_DURATION]: _isUndefined(receivedMinimumDuration)
? null
: receivedMinimumDuration,
})
}
this._persistDecideResponse(response)

this._linkedFlag = response.sessionRecording?.linkedFlag || null

Expand Down Expand Up @@ -426,6 +403,40 @@ export class SessionRecording {
}
}

private _persistDecideResponse(response: DecideResponse): void {
if (this.instance.persistence) {
const persistence = this.instance.persistence

const persistResponse = () => {
const receivedSampleRate = response.sessionRecording?.sampleRate

const parsedSampleRate = _isNullish(receivedSampleRate) ? null : parseFloat(receivedSampleRate)
const receivedMinimumDuration = response.sessionRecording?.minimumDurationMilliseconds

persistence.register({
[SESSION_RECORDING_ENABLED_SERVER_SIDE]: !!response['sessionRecording'],
[CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: response.sessionRecording?.consoleLogRecordingEnabled,
[SESSION_RECORDING_NETWORK_PAYLOAD_CAPTURE]: {
capturePerformance: response.capturePerformance,
...response.sessionRecording?.networkPayloadCapture,
},
[SESSION_RECORDING_CANVAS_RECORDING]: {
enabled: response.sessionRecording?.recordCanvas,
fps: response.sessionRecording?.canvasFps,
quality: response.sessionRecording?.canvasQuality,
},
[SESSION_RECORDING_SAMPLE_RATE]: parsedSampleRate,
[SESSION_RECORDING_MINIMUM_DURATION]: _isUndefined(receivedMinimumDuration)
? null
: receivedMinimumDuration,
})
}

persistResponse()
this.sessionManager.onSessionId(persistResponse)
}
}

log(message: string, level: 'log' | 'warn' | 'error' = 'log') {
this.instance.sessionRecording?.onRRwebEmit({
type: 6,
Expand Down
Loading