Skip to content

Commit

Permalink
opting out means we don't download recorder.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Jan 13, 2024
1 parent 4dd2b20 commit 08e3465
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
58 changes: 28 additions & 30 deletions cypress/e2e/session-recording.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,23 @@ function onPageLoad(options = {}) {
cy.wait('@recorder')
}

function assertPostHogEndpointAreNotCalled(expectedCalls) {
cy.wait(500)
cy.get('@session-recording').then((interceptions) => {
if (interceptions) {
expect(interceptions).to.have.length(0)
} else {
cy.log('no session recording endpoint called')
}
})
cy.get('@decide').then((interceptions) => {
if (interceptions && expectedCalls && expectedCalls.decide) {
expect(interceptions).to.be.an('object')
} else if (interceptions) {
expect(interceptions).not.to.be.an('object')
} else {
cy.log('no decide endpoint called')
}
})
cy.get('@recorder').then((interceptions) => {
if (interceptions && expectedCalls && expectedCalls.recorder) {
expect(interceptions).to.be.an('object')
} else if (interceptions) {
expect(interceptions).not.to.be.an('object')
} else {
cy.log('no recorder endpoint called')
}
})
/**
* Receives an object with keys as the name of the route and values as whether the route should have been called.
* e.g. { '@recorder': true, '@decide': false }
* the keys must match a `cy.intercept` alias
**/
function assertWhetherPostHogRequestsWereCalled(expectedCalls) {
cy.wait(200)

for (const [key, value] of Object.entries(expectedCalls)) {
cy.get(key).then((interceptions) => {
if (value) {
expect(interceptions).to.be.an('object')
} else {
expect(interceptions).not.to.be.an('object')
}
})
}
}

describe('Session recording', () => {
Expand All @@ -57,7 +47,11 @@ describe('Session recording', () => {
it('does not capture events without init', () => {
cy.get('[data-cy-input]').type('hello world! ')

assertPostHogEndpointAreNotCalled({ recorder: false, decide: false })
assertWhetherPostHogRequestsWereCalled({
'@recorder': false,
'@decide': false,
'@session-recording': false,
})

cy.get('[data-cy-input]')
.type('hello posthog!')
Expand All @@ -69,11 +63,15 @@ describe('Session recording', () => {
})

it('does not capture events when config opts out by default', () => {
onPageLoad({ opt_out_capturing_by_default: true })
cy.posthogInit({ opt_out_capturing_by_default: true })

cy.get('[data-cy-input]').type('hello world! ')

assertPostHogEndpointAreNotCalled({ decide: true, recorder: false })
assertWhetherPostHogRequestsWereCalled({
'@recorder': false,
'@decide': true,
'@session-recording': false,
})

cy.get('[data-cy-input]')
.type('hello posthog!')
Expand Down
10 changes: 5 additions & 5 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ export class SessionRecording {
return
}

if (this.instance.config.opt_out_capturing_by_default) {
return
}

// We do not switch recorder versions midway through a recording.
if (this._captureStarted || this.instance.config.disable_session_recording) {
if (
this._captureStarted ||
this.instance.config.disable_session_recording ||
this.instance.config.opt_out_capturing_by_default
) {
return
}

Expand Down

0 comments on commit 08e3465

Please sign in to comment.