Skip to content

Commit

Permalink
feat: capture session options in a custom event (#954)
Browse files Browse the repository at this point in the history
* feat: capture session options in a custom event

* update integration tests
  • Loading branch information
pauldambra authored Jan 10, 2024
1 parent a3736f8 commit 1106c7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
9 changes: 6 additions & 3 deletions cypress/e2e/session-recording.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ describe('Session recording', () => {
// a meta and then a full snapshot
expect(captures[1]['properties']['$snapshot_data'][0].type).to.equal(4) // meta
expect(captures[1]['properties']['$snapshot_data'][1].type).to.equal(2) // full_snapshot
expect(captures[1]['properties']['$snapshot_data'][2].type).to.equal(5) // custom event with options
// Making a set from the rest should all be 3 - incremental snapshots
const incrementalSnapshots = captures[1]['properties']['$snapshot_data'].slice(2)
const incrementalSnapshots = captures[1]['properties']['$snapshot_data'].slice(3)
expect(new Set(incrementalSnapshots.map((s) => s.type))).to.deep.equal(new Set([3]))
})
})
Expand Down Expand Up @@ -84,9 +85,10 @@ describe('Session recording', () => {
// a meta and then a full snapshot
expect(captures[1]['properties']['$snapshot_data'][0].type).to.equal(4) // meta
expect(captures[1]['properties']['$snapshot_data'][1].type).to.equal(2) // full_snapshot
expect(captures[1]['properties']['$snapshot_data'][2].type).to.equal(5) // custom event with options
// Making a set from the rest should all be 3 - incremental snapshots
expect(
new Set(captures[1]['properties']['$snapshot_data'].slice(2).map((s) => s.type))
new Set(captures[1]['properties']['$snapshot_data'].slice(3).map((s) => s.type))
).to.deep.equal(new Set([3]))
})
})
Expand Down Expand Up @@ -205,9 +207,10 @@ describe('Session recording', () => {
// a meta and then a full snapshot
expect(captures[1]['properties']['$snapshot_data'][0].type).to.equal(4) // meta
expect(captures[1]['properties']['$snapshot_data'][1].type).to.equal(2) // full_snapshot
expect(captures[1]['properties']['$snapshot_data'][2].type).to.equal(5) // custom event with options

const xPositions = []
for (let i = 2; i < captures[1]['properties']['$snapshot_data'].length; i++) {
for (let i = 3; i < captures[1]['properties']['$snapshot_data'].length; i++) {
expect(captures[1]['properties']['$snapshot_data'][i].type).to.equal(3)
expect(captures[1]['properties']['$snapshot_data'][i].data.source).to.equal(
6,
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,36 @@ describe('SessionRecording', () => {
// need to cast as any to mock private methods
jest.spyOn(sessionRecording as any, 'startCaptureAndTrySendingQueuedSnapshots')
jest.spyOn(sessionRecording, 'stopRecording')
jest.spyOn(sessionRecording as any, '_tryAddCustomEvent')
})

it('call startCaptureAndTrySendingQueuedSnapshots if its enabled', () => {
sessionRecording.startRecordingIfEnabled()
expect((sessionRecording as any).startCaptureAndTrySendingQueuedSnapshots).toHaveBeenCalled()
})

it('emits an options event', () => {
sessionRecording.startRecordingIfEnabled()
expect((sessionRecording as any)['_tryAddCustomEvent']).toHaveBeenCalledWith('$session_options', {
activePlugins: [],
sessionRecordingOptions: {
blockClass: 'ph-no-capture',
blockSelector: undefined,
collectFonts: false,
ignoreClass: 'ph-ignore-input',
inlineStylesheet: true,
maskAllInputs: false,
maskInputFn: undefined,
maskInputOptions: {},
maskTextClass: 'ph-mask',
maskTextFn: undefined,
maskTextSelector: undefined,
recordCrossOriginIframes: false,
slimDOMOptions: {},
},
})
})

it('call stopRecording if its not enabled', () => {
posthog.config.disable_session_recording = true
sessionRecording.startRecordingIfEnabled()
Expand Down
8 changes: 7 additions & 1 deletion src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,12 @@ export class SessionRecording {
},
})

const activePlugins = this._gatherRRWebPlugins()
this.stopRrweb = this.rrwebRecord({
emit: (event) => {
this.onRRwebEmit(event)
},
plugins: this._gatherRRWebPlugins(),
plugins: activePlugins,
...sessionRecordingOptions,
})

Expand All @@ -540,6 +541,11 @@ export class SessionRecording {
// We reset the last activity timestamp, resetting the idle timer
this._lastActivityTimestamp = Date.now()
this.isIdle = false

this._tryAddCustomEvent('$session_options', {
sessionRecordingOptions,
activePlugins: activePlugins.map((p) => p?.name),
})
}

private _gatherRRWebPlugins() {
Expand Down

0 comments on commit 1106c7d

Please sign in to comment.