-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: lazy load session recording #1588
Closed
+328
−158
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
163 changes: 82 additions & 81 deletions
163
src/__tests__/extensions/replay/sessionrecording.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { mockLogger } from './helpers/mock-logger' | ||
|
||
import { Info } from '../utils/event-utils' | ||
import { document, window } from '../utils/globals' | ||
import { uuidv7 } from '../uuidv7' | ||
import * as globals from '../utils/globals' | ||
import { assignableWindow, document, window } from '../utils/globals' | ||
import { uuidv7 } from '../uuidv7' | ||
import { ENABLE_PERSON_PROCESSING, USER_STATE } from '../constants' | ||
import { createPosthogInstance, defaultPostHog } from './helpers/posthog-instance' | ||
import { PostHogConfig, RemoteConfig } from '../types' | ||
import { PostHog } from '../posthog-core' | ||
import { OnlyValidKeys, PostHog } from '../posthog-core' | ||
import { PostHogPersistence } from '../posthog-persistence' | ||
import { SessionIdManager } from '../sessionid' | ||
import { RequestQueue } from '../request-queue' | ||
import { SessionRecording } from '../extensions/replay/sessionrecording' | ||
import { PostHogFeatureFlags } from '../posthog-featureflags' | ||
import { SessionRecordingLoader } from '../extensions/replay/session-recording-loader' | ||
|
||
describe('posthog core', () => { | ||
const baseUTCDateTime = new Date(Date.UTC(2020, 0, 1, 0, 0, 0)) | ||
|
@@ -31,6 +31,23 @@ describe('posthog core', () => { | |
|
||
beforeEach(() => { | ||
jest.useFakeTimers().setSystemTime(baseUTCDateTime) | ||
|
||
assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {} | ||
assignableWindow.__PosthogExtensions__.initSessionRecording = () => ({ | ||
start: jest.fn(), | ||
stop: jest.fn(), | ||
onRemoteConfig: jest.fn(), | ||
status: 'buffering', | ||
started: true, | ||
overrideLinkedFlag: jest.fn(), | ||
overrideSampling: jest.fn(), | ||
overrideTrigger: jest.fn(), | ||
}) | ||
assignableWindow.__PosthogExtensions__.loadExternalDependency = jest | ||
.fn() | ||
.mockImplementation(() => (_ph: PostHog, _name: string, cb: (err?: Error) => void) => { | ||
cb() | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
|
@@ -419,6 +436,7 @@ describe('posthog core', () => { | |
}, | ||
overrides | ||
) | ||
posthog.sessionRecording = new SessionRecordingLoader(posthog, () => true) | ||
}) | ||
|
||
it('returns calculated properties', () => { | ||
|
@@ -455,7 +473,6 @@ describe('posthog core', () => { | |
$lib_custom_api_host: 'https://custom.posthog.com', | ||
$is_identified: false, | ||
$process_person_profile: false, | ||
$recording_status: 'buffering', | ||
}) | ||
}) | ||
|
||
|
@@ -825,7 +842,10 @@ describe('posthog core', () => { | |
posthog.sessionRecording = { | ||
afterDecideResponse: jest.fn(), | ||
startIfEnabledOrStop: jest.fn(), | ||
} as unknown as SessionRecording | ||
} as OnlyValidKeys< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting... i expected this change to highlight the incorrect setup here that the |
||
Partial<SessionRecordingLoader>, | ||
Partial<SessionRecordingLoader> | ||
> as SessionRecordingLoader | ||
posthog.persistence = { | ||
register: jest.fn(), | ||
update_config: jest.fn(), | ||
|
@@ -1136,7 +1156,7 @@ describe('posthog core', () => { | |
instance._send_request = jest.fn() | ||
instance._loaded() | ||
|
||
expect(instance._send_request.mock.calls[0][0]).toMatchObject({ | ||
expect(jest.mocked(instance._send_request).mock.calls[0][0]).toMatchObject({ | ||
url: 'http://localhost/decide/?v=3', | ||
}) | ||
expect(instance.featureFlags.setReloadingPaused).toHaveBeenCalledWith(true) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { assignableWindow, document, LazyLoadedSessionRecordingInterface, window } from '../../utils/globals' | ||
import { PostHog } from '../../posthog-core' | ||
import { RemoteConfig } from '../../types' | ||
import { createLogger } from '../../utils/logger' | ||
import { SESSION_RECORDING_ENABLED_SERVER_SIDE } from '../../constants' | ||
import { isBoolean, isUndefined } from '../../utils/type-utils' | ||
|
||
const logger = createLogger('[Session-Recording-Loader]') | ||
|
||
export const isSessionRecordingEnabled = (loader: SessionRecordingLoader) => { | ||
const enabled_server_side = !!loader.instance.get_property(SESSION_RECORDING_ENABLED_SERVER_SIDE) | ||
const enabled_client_side = !loader.instance.config.disable_session_recording | ||
return !!window && enabled_server_side && enabled_client_side | ||
} | ||
|
||
export class SessionRecordingLoader { | ||
_forceAllowLocalhostNetworkCapture = false | ||
|
||
get lazyLoaded(): LazyLoadedSessionRecordingInterface | undefined { | ||
return this._lazyLoadedSessionRecording | ||
} | ||
|
||
private _lazyLoadedSessionRecording: LazyLoadedSessionRecordingInterface | undefined | ||
|
||
constructor(readonly instance: PostHog, readonly isEnabled: (srl: SessionRecordingLoader) => boolean) { | ||
this.startIfEnabled() | ||
} | ||
|
||
public onRemoteConfig(response: RemoteConfig) { | ||
if (this.instance.persistence) { | ||
this._lazyLoadedSessionRecording?.onRemoteConfig(response) | ||
} | ||
this.startIfEnabled() | ||
} | ||
|
||
public startIfEnabled() { | ||
if (this.isEnabled(this)) { | ||
this.loadScript(() => { | ||
this.start() | ||
}) | ||
} | ||
} | ||
|
||
private loadScript(cb: () => void): void { | ||
if (assignableWindow.__PosthogExtensions__?.initSessionRecording) { | ||
// already loaded | ||
cb() | ||
} | ||
assignableWindow.__PosthogExtensions__?.loadExternalDependency?.(this.instance, 'session-recorder', (err) => { | ||
if (err) { | ||
logger.error('failed to load script', err) | ||
return | ||
} | ||
cb() | ||
}) | ||
} | ||
|
||
private start() { | ||
if (!document) { | ||
logger.error('`document` not found. Cannot start.') | ||
return | ||
} | ||
|
||
if (!this._lazyLoadedSessionRecording && assignableWindow.__PosthogExtensions__?.initSessionRecording) { | ||
if ( | ||
isUndefined(this.instance.config.session_recording._forceAllowLocalhostNetworkCapture) && | ||
isBoolean(this._forceAllowLocalhostNetworkCapture) | ||
) { | ||
logger.warn( | ||
'`_forceAllowLocalhostNetworkCapture` has moved to `session_recording` config. Copying your setting over.' | ||
) | ||
this.instance.config.session_recording._forceAllowLocalhostNetworkCapture = | ||
this._forceAllowLocalhostNetworkCapture | ||
} | ||
|
||
this._lazyLoadedSessionRecording = assignableWindow.__PosthogExtensions__.initSessionRecording( | ||
this.instance | ||
) | ||
this._lazyLoadedSessionRecording.start() | ||
} | ||
} | ||
|
||
stop() { | ||
if (this._lazyLoadedSessionRecording) { | ||
this._lazyLoadedSessionRecording.stop() | ||
this._lazyLoadedSessionRecording = undefined | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to add tests on the loader separately from the loaded file