Skip to main content

[JavaScript] How can I delay Session Replay recording until a user consents?

Issue

I want Session Replay, but recording must not start until the user has consented.

Applies To

  • All SaaS Customers and Self-Hosted Users

  • JavaScript SDK (browser)

  • Session Replay

  • Loader Script

Resolution

Do not add Replay until after consent (npm), or delay loading the Loader Script until after consent.

If you use the npm package

Initialize Sentry without replayIntegration(), then add Replay after consent and set your sample rates:

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // Do not add replayIntegration() here yet
});function onUserConsent() {
  const client = Sentry.getClient();
  const options = client.getOptions();
  options.replaysSessionSampleRate = 0.1;
  options.replaysOnErrorSampleRate = 1.0;  client.addIntegration(
    Sentry.replayIntegration({
      maskAllText: true,
    }),
  );
}

Recording follows your sample rates after the integration is added. See Manually add Replay integration.

If you use the Loader Script

Delay loading the entire Loader Script until after consent. The Loader does not support starting with errors only and turning Replay on later in the same page load when Replay is enabled on the Loader.

Also review Session Replay privacy so masking and blocking match your consent and data-privacy requirements.

For other runtime start/stop patterns, see How to conditionally enable Session Replay.

Did this answer your question?