Skip to content

Commit

Permalink
feat: allow decide to provide script name for recorder (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Nov 28, 2024
1 parent cc59e66 commit 2186b6e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,15 @@ describe('SessionRecording', () => {
jest.spyOn(sessionRecording, 'startIfEnabledOrStop')
})

it('loads script based on script config', () => {
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', scriptConfig: { script: 'experimental-recorder' } },
})
)
expect(loadScriptMock).toHaveBeenCalledWith(posthog, 'experimental-recorder', expect.any(Function))
})

it('when the first event is a meta it does not take a manual full snapshot', () => {
sessionRecording.startIfEnabledOrStop()
expect(loadScriptMock).toHaveBeenCalled()
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const SESSION_RECORDING_NETWORK_PAYLOAD_CAPTURE = '$session_recording_net
export const SESSION_RECORDING_CANVAS_RECORDING = '$session_recording_canvas_recording'
export const SESSION_RECORDING_SAMPLE_RATE = '$replay_sample_rate'
export const SESSION_RECORDING_MINIMUM_DURATION = '$replay_minimum_duration'
export const SESSION_RECORDING_SCRIPT_CONFIG = '$replay_script_config'
export const SESSION_ID = '$sesid'
export const SESSION_RECORDING_IS_SAMPLED = '$session_is_sampled'
export const SESSION_RECORDING_URL_TRIGGER_ACTIVATED_SESSION = '$session_recording_url_trigger_activated_session'
Expand Down
13 changes: 11 additions & 2 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SESSION_RECORDING_MINIMUM_DURATION,
SESSION_RECORDING_NETWORK_PAYLOAD_CAPTURE,
SESSION_RECORDING_SAMPLE_RATE,
SESSION_RECORDING_SCRIPT_CONFIG,
SESSION_RECORDING_URL_TRIGGER_ACTIVATED_SESSION,
} from '../../constants'
import {
Expand Down Expand Up @@ -38,7 +39,7 @@ import {

import { isBoolean, isFunction, isNullish, isNumber, isObject, isString, isUndefined } from '../../utils/type-utils'
import { logger } from '../../utils/logger'
import { assignableWindow, document, window } from '../../utils/globals'
import { assignableWindow, document, PostHogExtensionKind, window } from '../../utils/globals'
import { buildNetworkRequestOptions } from './config'
import { isLocalhost } from '../../utils/request-utils'
import { MutationRateLimiter } from './mutation-rate-limiter'
Expand Down Expand Up @@ -696,6 +697,7 @@ export class SessionRecording {
[SESSION_RECORDING_MINIMUM_DURATION]: isUndefined(receivedMinimumDuration)
? null
: receivedMinimumDuration,
[SESSION_RECORDING_SCRIPT_CONFIG]: response.sessionRecording?.scriptConfig,
})
}

Expand Down Expand Up @@ -752,7 +754,7 @@ export class SessionRecording {
// If recorder.js is already loaded (if array.full.js snippet is used or posthog-js/dist/recorder is
// imported), don't load script. Otherwise, remotely import recorder.js from cdn since it hasn't been loaded.
if (!this.rrwebRecord) {
assignableWindow.__PosthogExtensions__?.loadExternalDependency?.(this.instance, 'recorder', (err) => {
assignableWindow.__PosthogExtensions__?.loadExternalDependency?.(this.instance, this.scriptName, (err) => {
if (err) {
return logger.error(LOGGER_PREFIX + ` could not load recorder`, err)
}
Expand All @@ -769,6 +771,13 @@ export class SessionRecording {
}
}

private get scriptName(): PostHogExtensionKind {
return (
(this.instance?.persistence?.get_property(SESSION_RECORDING_SCRIPT_CONFIG)
?.script as PostHogExtensionKind) || 'recorder'
)
}

private isInteractiveEvent(event: eventWithTime) {
return (
event.type === INCREMENTAL_SNAPSHOT_EVENT_TYPE &&
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export interface DecideResponse {
linkedFlag?: string | FlagVariant | null
networkPayloadCapture?: Pick<NetworkRecordOptions, 'recordBody' | 'recordHeaders'>
urlTriggers?: SessionRecordingUrlTrigger[]
scriptConfig?: { script?: string | undefined }
urlBlocklist?: SessionRecordingUrlTrigger[]
eventTriggers?: string[]
}
Expand Down

0 comments on commit 2186b6e

Please sign in to comment.