Issue
I want to set the replay sample rate based on my own conditions, so Session Replay records only when those conditions are met.
Applies To
All SaaS Customers and Self-Hosted Users
Session Replay
Resolution
You can control Session Replay conditionally in two ways.
To decide at initialization, evaluate your condition and set the sample rates when you call Sentry.init:
const enableSessionReplay = /* your condition */ true;
Sentry.init({
// ...
integrations: [Sentry.replayIntegration()],
replaysOnErrorSampleRate: enableSessionReplay ? 1.0 : 0,
replaysSessionSampleRate: enableSessionReplay ? 0.1 : 0,
});
You cannot change Sentry.init options after initialization. Changing them would require shutting down the SDK and initializing it again, which we do not recommend.
To control replay at runtime, set both sample rates to 0 and start or stop recording manually based on your conditions:
const replay = Sentry.getReplay();
// Start recording in buffer mode
replay.startBuffering();
// Or start a full session recording
replay.start();
// Stop recording
replay.stop();
For per-error decisions in buffer mode, use the beforeErrorSampling option in replayIntegration() to choose which errors trigger a replay. For more detail, see the Session Replay documentation.
