Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 21, 2023
1 parent d072662 commit cd851a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
26 changes: 13 additions & 13 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,67 +108,67 @@ describe('SessionRecording', () => {
describe('isRecordingEnabled', () => {
it('is enabled if both the server and client config says enabled', () => {
posthog.persistence?.register({ [SESSION_RECORDING_ENABLED_SERVER_SIDE]: true })
expect(sessionRecording.isRecordingEnabled()).toBeTruthy()
expect(sessionRecording.isRecordingEnabled).toBeTruthy()
})

it('is disabled if the server is disabled', () => {
posthog.persistence?.register({ [SESSION_RECORDING_ENABLED_SERVER_SIDE]: false })
expect(sessionRecording.isRecordingEnabled()).toBe(false)
expect(sessionRecording.isRecordingEnabled).toBe(false)
})

it('is disabled if the client config is disabled', () => {
posthog.config.disable_session_recording = true
expect(sessionRecording.isRecordingEnabled()).toBe(false)
expect(sessionRecording.isRecordingEnabled).toBe(false)
})
})

describe('isConsoleLogCaptureEnabled', () => {
it('uses client side setting when set to false', () => {
posthog.persistence?.register({ [CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: true })
posthog.config.enable_recording_console_log = false
expect(sessionRecording.isConsoleLogCaptureEnabled()).toBe(false)
expect(sessionRecording.isConsoleLogCaptureEnabled).toBe(false)
})

it('uses client side setting when set to true', () => {
posthog.persistence?.register({ [CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: false })
posthog.config.enable_recording_console_log = true
expect(sessionRecording.isConsoleLogCaptureEnabled()).toBe(true)
expect(sessionRecording.isConsoleLogCaptureEnabled).toBe(true)
})

it('uses server side setting if client side setting is not set', () => {
posthog.config.enable_recording_console_log = undefined
posthog.persistence?.register({ [CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: false })
expect(sessionRecording.isConsoleLogCaptureEnabled()).toBe(false)
expect(sessionRecording.isConsoleLogCaptureEnabled).toBe(false)

posthog.persistence?.register({ [CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE]: true })
expect(sessionRecording.isConsoleLogCaptureEnabled()).toBe(true)
expect(sessionRecording.isConsoleLogCaptureEnabled).toBe(true)
})
})

describe('getRecordingVersion', () => {
it('uses client side setting v2 over server side', () => {
posthog.persistence?.register({ [SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE]: 'v1' })
posthog.config.session_recording.recorderVersion = 'v2'
expect(sessionRecording.getRecordingVersion()).toBe('v2')
expect(sessionRecording.recordingVersion).toBe('v2')
})

it('uses client side setting v1 over server side', () => {
posthog.persistence?.register({ [SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE]: 'v2' })
posthog.config.session_recording.recorderVersion = 'v1'
expect(sessionRecording.getRecordingVersion()).toBe('v1')
expect(sessionRecording.recordingVersion).toBe('v1')
})

it('uses server side setting if client side setting is not set', () => {
posthog.config.session_recording.recorderVersion = undefined

posthog.persistence?.register({ [SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE]: 'v1' })
expect(sessionRecording.getRecordingVersion()).toBe('v1')
expect(sessionRecording.recordingVersion).toBe('v1')

posthog.persistence?.register({ [SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE]: 'v2' })
expect(sessionRecording.getRecordingVersion()).toBe('v2')
expect(sessionRecording.recordingVersion).toBe('v2')

posthog.persistence?.register({ [SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE]: undefined })
expect(sessionRecording.getRecordingVersion()).toBe('v1')
expect(sessionRecording.recordingVersion).toBe('v1')
})
})

Expand Down Expand Up @@ -964,7 +964,7 @@ describe('SessionRecording', () => {
expect(sessionRecording.lastActivityTimestamp).toEqual(lastActivityTimestamp + 100)
expect((window as any).rrwebRecord.takeFullSnapshot).toHaveBeenCalledTimes(0)

// this triggers idle state _and_ is a user interaction so we take a full snapshot
// this triggers idle state _and_ is a user interaction, so we take a full snapshot
_emit({
event: 123,
type: INCREMENTAL_SNAPSHOT_EVENT_TYPE,
Expand Down
55 changes: 26 additions & 29 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE,
SESSION_RECORDING_ENABLED_SERVER_SIDE,
SESSION_RECORDING_IS_SAMPLED,
SESSION_RECORDING_LINKED_FLAG,
SESSION_RECORDING_MINIMUM_DURATION,
SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE,
SESSION_RECORDING_SAMPLE_RATE,
SESSION_RECORDING_LINKED_FLAG,
} from '../../constants'
import {
ensureMaxMessageSize,
Expand Down Expand Up @@ -125,28 +125,43 @@ export class SessionRecording {
return this.instance.get_property(SESSION_RECORDING_LINKED_FLAG)
}

get isRecordingEnabled() {
const enabled_server_side = !!this.instance.get_property(SESSION_RECORDING_ENABLED_SERVER_SIDE)
const enabled_client_side = !this.instance.config.disable_session_recording
return enabled_server_side && enabled_client_side
}

get isConsoleLogCaptureEnabled() {
const enabled_server_side = !!this.instance.get_property(CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE)
const enabled_client_side = this.instance.config.enable_recording_console_log
return enabled_client_side ?? enabled_server_side
}

get recordingVersion() {
const recordingVersion_server_side = this.instance.get_property(SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE)
const recordingVersion_client_side = this.instance.config.session_recording?.recorderVersion
return recordingVersion_client_side || recordingVersion_server_side || 'v1'
}

/**
* defaults to buffering mode until a decide response is received
* once a decide response is received status can be disabled, active or sampled
*/
get status(): SessionRecordingStatus {
const isEnabled = this.isRecordingEnabled()
const isSampled = this.isSampled

if (!this.receivedDecide) {
return 'buffering'
}

if (!isEnabled) {
if (!this.isRecordingEnabled) {
return 'disabled'
}

if (_isString(this.linkedFlag) && !this._linkedFlagSeen) {
return 'buffering'
}

if (_isBoolean(isSampled)) {
return isSampled ? 'sampled' : 'disabled'
if (_isBoolean(this.isSampled)) {
return this.isSampled ? 'sampled' : 'disabled'
} else {
return 'active'
}
Expand Down Expand Up @@ -196,7 +211,7 @@ export class SessionRecording {
}

startRecordingIfEnabled() {
if (this.isRecordingEnabled()) {
if (this.isRecordingEnabled) {
this.startCaptureAndTrySendingQueuedSnapshots()
} else {
this.stopRecording()
Expand All @@ -212,24 +227,6 @@ export class SessionRecording {
}
}

isRecordingEnabled() {
const enabled_server_side = !!this.instance.get_property(SESSION_RECORDING_ENABLED_SERVER_SIDE)
const enabled_client_side = !this.instance.config.disable_session_recording
return enabled_server_side && enabled_client_side
}

isConsoleLogCaptureEnabled() {
const enabled_server_side = !!this.instance.get_property(CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE)
const enabled_client_side = this.instance.config.enable_recording_console_log
return enabled_client_side ?? enabled_server_side
}

getRecordingVersion() {
const recordingVersion_server_side = this.instance.get_property(SESSION_RECORDING_RECORDER_VERSION_SERVER_SIDE)
const recordingVersion_client_side = this.instance.config.session_recording?.recorderVersion
return recordingVersion_client_side || recordingVersion_server_side || 'v1'
}

private makeSamplingDecision(sessionId: string): void {
const sessionIdChanged = this.sessionId !== sessionId

Expand Down Expand Up @@ -340,12 +337,12 @@ export class SessionRecording {
// We want to ensure the sessionManager is reset if necessary on load of the recorder
this.getSessionManager().checkAndGetSessionAndWindowId()

const recorderJS = this.getRecordingVersion() === 'v2' ? 'recorder-v2.js' : 'recorder.js'
const recorderJS = this.recordingVersion === 'v2' ? 'recorder-v2.js' : 'recorder.js'

// If recorder.js is already loaded (if array.full.js snippet is used or posthog-js/dist/recorder is
// imported) or matches the requested recorder version, don't load script. Otherwise, remotely import
// recorder.js from cdn since it hasn't been loaded.
if (this.instance.__loaded_recorder_version !== this.getRecordingVersion()) {
if (this.instance.__loaded_recorder_version !== this.recordingVersion) {
loadScript(this.instance.config.api_host + `/static/${recorderJS}?v=${Config.LIB_VERSION}`, (err) => {
if (err) {
return logger.error(`Could not load ${recorderJS}`, err)
Expand Down Expand Up @@ -483,7 +480,7 @@ export class SessionRecording {
this.onRRwebEmit(event)
},
plugins:
(window as any).rrwebConsoleRecord && this.isConsoleLogCaptureEnabled()
(window as any).rrwebConsoleRecord && this.isConsoleLogCaptureEnabled
? [(window as any).rrwebConsoleRecord.getRecordConsolePlugin()]
: [],
...sessionRecordingOptions,
Expand Down

0 comments on commit cd851a5

Please sign in to comment.