Skip to content

Commit 25021b0

Browse files
authored
fix: resetting sampling to null should clear sampling for users (#1859)
* fix: resetting sampling to null should clear sampling for users * simplify test * another test * and so that it doesn't clash with over-riding * unregister instead of setting to null
1 parent 1225108 commit 25021b0

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/__tests__/extensions/replay/sessionrecording.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,47 @@ describe('SessionRecording', () => {
854854
expect(emitValues.filter((v) => v === 'sampled').length).toBeGreaterThan(30)
855855
expect(emitValues.filter((v) => v === 'disabled').length).toBeGreaterThan(30)
856856
})
857+
858+
it('turning sample rate to null, means sessions are no longer sampled out', () => {
859+
sessionRecording.startIfEnabledOrStop()
860+
// set sample rate to 0, i.e. no sessions will run
861+
sessionRecording.onRemoteConfig(
862+
makeDecideResponse({ sessionRecording: { endpoint: '/s/', sampleRate: '0.00' } })
863+
)
864+
// then check that a session is sampled (i.e. storage is false not true or null)
865+
expect(posthog.get_property(SESSION_RECORDING_IS_SAMPLED)).toBe(false)
866+
expect(sessionRecording['status']).toBe('disabled')
867+
868+
// then turn sample rate to null
869+
sessionRecording.onRemoteConfig(
870+
makeDecideResponse({ sessionRecording: { endpoint: '/s/', sampleRate: null } })
871+
)
872+
873+
// then check that a session is no longer sampled out (i.e. storage is cleared not false)
874+
expect(posthog.get_property(SESSION_RECORDING_IS_SAMPLED)).toBe(undefined)
875+
expect(sessionRecording['status']).toBe('active')
876+
})
877+
878+
it('turning sample rate from null to 0, resets values as expected', () => {
879+
sessionRecording.startIfEnabledOrStop()
880+
881+
// first turn sample rate to null
882+
sessionRecording.onRemoteConfig(
883+
makeDecideResponse({ sessionRecording: { endpoint: '/s/', sampleRate: null } })
884+
)
885+
886+
// then check that a session is no longer sampled out (i.e. storage is cleared not false)
887+
expect(posthog.get_property(SESSION_RECORDING_IS_SAMPLED)).toBe(undefined)
888+
expect(sessionRecording['status']).toBe('active')
889+
890+
// set sample rate to 0, i.e. no sessions will run
891+
sessionRecording.onRemoteConfig(
892+
makeDecideResponse({ sessionRecording: { endpoint: '/s/', sampleRate: '0.00' } })
893+
)
894+
// then check that a session is sampled (i.e. storage is false not true or null)
895+
expect(posthog.get_property(SESSION_RECORDING_IS_SAMPLED)).toBe(false)
896+
expect(sessionRecording['status']).toBe('disabled')
897+
})
857898
})
858899

859900
describe('canvas', () => {

src/extensions/replay/sessionrecording.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ export class SessionRecording {
610610
}
611611
}
612612

613+
private _resetSampling() {
614+
this.instance.persistence?.unregister(SESSION_RECORDING_IS_SAMPLED)
615+
}
616+
613617
private makeSamplingDecision(sessionId: string): void {
614618
const sessionIdChanged = this.sessionId !== sessionId
615619

@@ -619,9 +623,7 @@ export class SessionRecording {
619623
const currentSampleRate = this.sampleRate
620624

621625
if (!isNumber(currentSampleRate)) {
622-
this.instance.persistence?.register({
623-
[SESSION_RECORDING_IS_SAMPLED]: null,
624-
})
626+
this._resetSampling()
625627
return
626628
}
627629

@@ -720,6 +722,10 @@ export class SessionRecording {
720722
const receivedSampleRate = response.sessionRecording?.sampleRate
721723

722724
const parsedSampleRate = isNullish(receivedSampleRate) ? null : parseFloat(receivedSampleRate)
725+
if (isNullish(parsedSampleRate)) {
726+
this._resetSampling()
727+
}
728+
723729
const receivedMinimumDuration = response.sessionRecording?.minimumDurationMilliseconds
724730

725731
persistence.register({

0 commit comments

Comments
 (0)