Skip to content

Commit

Permalink
probably working but tests not passing
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Jan 13, 2024
1 parent 08e3465 commit 93618df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
34 changes: 32 additions & 2 deletions cypress/e2e/session-recording.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ describe('Session recording', () => {
it('does not capture events when config opts out by default', () => {
cy.posthogInit({ opt_out_capturing_by_default: true })

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

assertWhetherPostHogRequestsWereCalled({
'@recorder': false,
'@decide': true,
Expand All @@ -81,6 +79,38 @@ describe('Session recording', () => {
})
})
})

it('can start recording after starting opted out', () => {
cy.posthogInit({ opt_out_capturing_by_default: true })

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

cy.posthog().invoke('opt_in_capturing')
// TODO: should we require this call?
cy.posthog().invoke('startSessionRecording')

cy.phCaptures({ full: true }).then((captures) => {
expect((captures || []).map((c) => c.event)).to.deep.equal(['$opt_in'])
})

assertWhetherPostHogRequestsWereCalled({
'@recorder': true,
'@decide': true,
// no call to session-recording yet
})

cy.get('[data-cy-input]')
.type('hello posthog!')
.then(() => {
cy.phCaptures({ full: true }).then((captures) => {
expect(JSON.stringify((captures || []).map((c) => c.event))).to.deep.equal([])
})
})
})
})

describe('array.full.js', () => {
Expand Down
8 changes: 3 additions & 5 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { logger } from '../../utils/logger'
import { assignableWindow, window } from '../../utils/globals'
import { buildNetworkRequestOptions } from './config'
import { isLocalhost } from '../../utils/request-utils'
import { userOptedOut } from '../../gdpr-utils'

const BASE_ENDPOINT = '/s/'

Expand Down Expand Up @@ -348,11 +349,8 @@ export class SessionRecording {
}

// We do not switch recorder versions midway through a recording.
if (
this._captureStarted ||
this.instance.config.disable_session_recording ||
this.instance.config.opt_out_capturing_by_default
) {
// do not start if explicitly disabled or if the user has opted out
if (this._captureStarted || this.instance.config.disable_session_recording || userOptedOut(this.instance)) {
return
}

Expand Down
8 changes: 7 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,13 @@ export class PostHog {
}

if (this.sessionRecording && !_isUndefined(config.disable_session_recording)) {
if (oldConfig.disable_session_recording !== config.disable_session_recording) {
const disable_session_recording_has_changed =
oldConfig.disable_session_recording !== config.disable_session_recording
// if opting back in, this config might not have changed
const try_enable_after_opt_in =
!userOptedOut(this) && !config.disable_session_recording && !this.sessionRecording.started

if (disable_session_recording_has_changed || try_enable_after_opt_in) {
if (config.disable_session_recording) {
this.sessionRecording.stopRecording()
} else {
Expand Down

0 comments on commit 93618df

Please sign in to comment.