diff --git a/src/__tests__/extensions/replay/sessionrecording.test.ts b/src/__tests__/extensions/replay/sessionrecording.test.ts index d1ca09e00..d786d56e3 100644 --- a/src/__tests__/extensions/replay/sessionrecording.test.ts +++ b/src/__tests__/extensions/replay/sessionrecording.test.ts @@ -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() diff --git a/src/constants.ts b/src/constants.ts index 594f1503b..89cda8f3b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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' diff --git a/src/extensions/replay/sessionrecording.ts b/src/extensions/replay/sessionrecording.ts index ce5f9d067..4b62ac142 100644 --- a/src/extensions/replay/sessionrecording.ts +++ b/src/extensions/replay/sessionrecording.ts @@ -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 { @@ -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' @@ -696,6 +697,7 @@ export class SessionRecording { [SESSION_RECORDING_MINIMUM_DURATION]: isUndefined(receivedMinimumDuration) ? null : receivedMinimumDuration, + [SESSION_RECORDING_SCRIPT_CONFIG]: response.sessionRecording?.scriptConfig, }) } @@ -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) } @@ -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 && diff --git a/src/types.ts b/src/types.ts index 3044e10df..c52b3d6e1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -515,6 +515,7 @@ export interface DecideResponse { linkedFlag?: string | FlagVariant | null networkPayloadCapture?: Pick urlTriggers?: SessionRecordingUrlTrigger[] + scriptConfig?: { script?: string | undefined } urlBlocklist?: SessionRecordingUrlTrigger[] eventTriggers?: string[] }