-
Notifications
You must be signed in to change notification settings - Fork 59
Feat zoning 010526 background capture #1502
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
base: feat-zoning-010526
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -94,6 +94,10 @@ export const autocapturePlugin = ( | |||||||||||||||||||
| enabled: true, | ||||||||||||||||||||
| messenger: new WindowMessenger(), | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| backgroundCaptureOptions = { | ||||||||||||||||||||
| enabled: true, | ||||||||||||||||||||
| messenger: new WindowMessenger(), | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| } = options; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| options.cssSelectorAllowlist = options.cssSelectorAllowlist ?? DEFAULT_CSS_SELECTOR_ALLOWLIST; | ||||||||||||||||||||
|
|
@@ -423,7 +427,6 @@ export const autocapturePlugin = ( | |||||||||||||||||||
|
|
||||||||||||||||||||
| /* istanbul ignore next */ | ||||||||||||||||||||
| config?.loggerProvider?.log(`${name} has been successfully added.`); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Setup visual tagging selector | ||||||||||||||||||||
| if (window.opener && visualTaggingOptions.enabled) { | ||||||||||||||||||||
| const allowlist = (options as AutoCaptureOptionsWithDefaults).cssSelectorAllowlist; | ||||||||||||||||||||
|
|
@@ -439,6 +442,15 @@ export const autocapturePlugin = ( | |||||||||||||||||||
| actionClickAllowlist: actionClickAllowlist, | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Setup background capture messenger if it is not already setup for visual tagging selector | ||||||||||||||||||||
| if (window.opener && backgroundCaptureOptions.enabled && !visualTaggingOptions.messenger) { | ||||||||||||||||||||
| /* istanbul ignore next */ | ||||||||||||||||||||
| backgroundCaptureOptions.messenger?.setup({ | ||||||||||||||||||||
| dataExtractor: dataExtractor, | ||||||||||||||||||||
| logger: config?.loggerProvider, | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+450
to
+453
Contributor
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. Background capture setup omits the
Suggested change
|
||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const execute: BrowserEnrichmentPlugin['execute'] = async (event) => { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,9 +4,13 @@ import { | |||||||||||||||
| AMPLITUDE_ORIGIN, | ||||||||||||||||
| AMPLITUDE_VISUAL_TAGGING_SELECTOR_SCRIPT_URL, | ||||||||||||||||
| AMPLITUDE_VISUAL_TAGGING_HIGHLIGHT_CLASS, | ||||||||||||||||
| AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, | ||||||||||||||||
| } from '../constants'; | ||||||||||||||||
| import { asyncLoadScript, generateUniqueId } from '../helpers'; | ||||||||||||||||
| import { ILogger, Messenger, ActionType } from '@amplitude/analytics-core'; | ||||||||||||||||
| // Inline background-capture implementation (disabled) uses rrweb snapshot. | ||||||||||||||||
| // eslint-disable-next-line import/no-extraneous-dependencies | ||||||||||||||||
| // import { snapshot } from '@amplitude/rrweb-snapshot'; | ||||||||||||||||
| import { VERSION } from '../version'; | ||||||||||||||||
| import { DataExtractor } from '../data-extractor'; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -19,7 +23,11 @@ export type Action = | |||||||||||||||
| | 'close-visual-tagging-selector' | ||||||||||||||||
| | 'element-selected' | ||||||||||||||||
| | 'track-selector-mode-changed' | ||||||||||||||||
| | 'track-selector-moved'; | ||||||||||||||||
| | 'track-selector-moved' | ||||||||||||||||
| | 'initialize-background-capture' | ||||||||||||||||
| | 'close-background-capture' | ||||||||||||||||
| | 'background-capture-loaded' | ||||||||||||||||
| | 'background-capture-complete'; | ||||||||||||||||
|
|
||||||||||||||||
| interface InitializeVisualTaggingSelectorData { | ||||||||||||||||
| actionType: ActionType; | ||||||||||||||||
|
|
@@ -53,6 +61,10 @@ export type ActionData = { | |||||||||||||||
| 'element-selected': ElementSelectedData; | ||||||||||||||||
| 'track-selector-mode-changed': TrackSelectorModeChangedData; | ||||||||||||||||
| 'track-selector-moved': TrackSelectorMovedData; | ||||||||||||||||
| 'initialize-background-capture': null | undefined; | ||||||||||||||||
| 'close-background-capture': null | undefined; | ||||||||||||||||
| 'background-capture-loaded': null | undefined; | ||||||||||||||||
| 'background-capture-complete': { [key: string]: string | null }; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| export interface Message<A extends Action> { | ||||||||||||||||
|
|
@@ -155,7 +167,8 @@ export class WindowMessenger implements Messenger { | |||||||||||||||
| this.endpoint = endpoint; | ||||||||||||||||
| } | ||||||||||||||||
| let amplitudeVisualTaggingSelectorInstance: any = null; | ||||||||||||||||
|
|
||||||||||||||||
| let amplitudeBackgroundCaptureInstance: any = null; | ||||||||||||||||
| this.logger?.debug?.('Setting up messenger'); | ||||||||||||||||
| // Attach Event Listener to listen for messages from the parent window | ||||||||||||||||
| window.addEventListener('message', (event) => { | ||||||||||||||||
| this.logger?.debug?.('Message received: ', JSON.stringify(event)); | ||||||||||||||||
|
|
@@ -214,9 +227,27 @@ export class WindowMessenger implements Messenger { | |||||||||||||||
| .catch(() => { | ||||||||||||||||
| this.logger?.warn('Failed to initialize visual tagging selector'); | ||||||||||||||||
| }); | ||||||||||||||||
| } else if (action === 'initialize-background-capture') { | ||||||||||||||||
| this.logger?.debug?.('Initializing background capture (external script)'); | ||||||||||||||||
| asyncLoadScript(new URL(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, this.endpoint).toString()) | ||||||||||||||||
|
Contributor
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. Re-initializing
Suggested change
Contributor
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.
Suggested change
|
||||||||||||||||
| .then(() => { | ||||||||||||||||
| this.logger?.debug?.('Background capture script loaded (external)'); | ||||||||||||||||
| // eslint-disable-next-line | ||||||||||||||||
| amplitudeBackgroundCaptureInstance = (window as any)?.amplitudeBackgroundCapture?.({ | ||||||||||||||||
| messenger: this, | ||||||||||||||||
| onBackgroundCapture: this.onBackgroundCapture, | ||||||||||||||||
| }); | ||||||||||||||||
| this.notify({ action: 'background-capture-loaded' }); | ||||||||||||||||
| }) | ||||||||||||||||
| .catch(() => { | ||||||||||||||||
| this.logger?.warn('Failed to initialize background capture'); | ||||||||||||||||
| }); | ||||||||||||||||
| } else if (action === 'close-visual-tagging-selector') { | ||||||||||||||||
| // eslint-disable-next-line | ||||||||||||||||
| amplitudeVisualTaggingSelectorInstance?.close?.(); | ||||||||||||||||
| } else if (action === 'close-background-capture') { | ||||||||||||||||
| // eslint-disable-next-line | ||||||||||||||||
| amplitudeBackgroundCaptureInstance?.close?.(); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -236,4 +267,11 @@ export class WindowMessenger implements Messenger { | |||||||||||||||
| this.notify({ action: 'track-selector-moved', data: properties }); | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| private onBackgroundCapture = (type: string, backgroundCaptureData: { [key: string]: string | number | null }) => { | ||||||||||||||||
| if (type === 'background-capture-complete') { | ||||||||||||||||
| this.logger?.debug?.('Background capture complete'); | ||||||||||||||||
| this.notify({ action: 'background-capture-complete', data: backgroundCaptureData }); | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
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.
Repeated inits leak/duplicate listeners/observers. Consider an explicit lifecycle: store handler refs; teardown removes listeners and closes any existing instance before re‑init (e.g.,
WindowMessenger.setup,initialize-background-capture).