Skip to content

Conversation

@jxiwang
Copy link
Collaborator

@jxiwang jxiwang commented Jan 20, 2026

Summary

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?:

@jxiwang jxiwang changed the base branch from main to feat-zoning-010526 January 20, 2026 23:15
@macroscopeapp
Copy link
Contributor

macroscopeapp bot commented Jan 20, 2026

Add background capture setup to autocapturePlugin in autocapture-plugin.ts when window.opener is truthy and backgroundCaptureOptions.enabled is true

Introduce backgroundCaptureOptions to element interaction types and wire background capture initialization and messaging in WindowMessenger, including loading https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.0.js.gz and emitting background-capture-loaded and background-capture-complete. Add @amplitude/rrweb-snapshot@2.0.0-alpha.35 and update tests for setup behavior.

📍Where to Start

Start with the setup logic in autocapturePlugin in autocapture-plugin.ts, then review message handling in messenger.ts.


Macroscope summarized 5986830.

// 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({
Copy link
Contributor

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).

🚀 Want me to fix this? Reply ex: "fix it for me".

@macroscopeapp
Copy link
Contributor

macroscopeapp bot commented Jan 20, 2026

Add default background capture initialization to autocapturePlugin in autocapture-plugin.ts for zoning 010526

Introduce backgroundCaptureOptions in element interaction types, add @amplitude/rrweb-snapshot@2.0.0-alpha.35, load https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.js.gz via messenger.WindowMessenger, and emit background-capture-loaded and background-capture-complete events.

📍Where to Start

Start with the autocapturePlugin setup path in autocapture-plugin.ts, then review message handling in messenger.WindowMessenger in messenger.ts.


📊 Macroscope summarized d0480e0. 6 files reviewed, 4 issues evaluated, 2 issues filtered, 1 comment posted. View details

Comment on lines +450 to +453
dataExtractor: dataExtractor,
logger: config?.loggerProvider,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Background capture setup omits the endpoint derived from serverZone, so WindowMessenger defaults to US origin and rejects messages from EU (origin mismatch). Consider passing the same endpoint here as in the visual tagging path.

Suggested change
dataExtractor: dataExtractor,
logger: config?.loggerProvider,
});
}
backgroundCaptureOptions.messenger?.setup({
dataExtractor: dataExtractor,
logger: config?.loggerProvider,
...(config?.serverZone && { endpoint: constants.AMPLITUDE_ORIGINS_MAP[config.serverZone] }),
});

🚀 Want me to fix this? Reply ex: "fix it for me".

});
} 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-initializing background-capture overwrites amplitudeBackgroundCaptureInstance without closing the previous instance. Consider closing any existing instance before creating a new one to avoid leaked listeners and duplicate capture.

Suggested change
asyncLoadScript(new URL(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, this.endpoint).toString())
// eslint-disable-next-line
amplitudeBackgroundCaptureInstance?.close?.();
amplitudeBackgroundCaptureInstance = null;
asyncLoadScript(new URL(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, this.endpoint).toString())

🚀 Want me to fix this? Reply ex: "fix it for me".

});
} 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new URL(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, this.endpoint) will throw if this.endpoint is '*'. Consider passing the absolute AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL directly to avoid crashing the listener.

Suggested change
asyncLoadScript(new URL(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL, this.endpoint).toString())
asyncLoadScript(AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL)

🚀 Want me to fix this? Reply ex: "fix it for me".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants