Session Replay React Native Segment Integration
This article covers how to install Session Replay using the Session Replay React Native Segment plugin. If your React Native app already uses Segment's Analytics React Native library and Amplitude (Actions) destination, use this option.
If you instrument your app with an Amplitude React Native SDK, use the Session Replay React Native SDK Plugin.
If you use Segment with other options, use the standalone implementation.
Report issues
To report issues with Session Replay for React Native, go to the Amplitude-TypeScript GitHub repository.
Amplitude Session Plugin required
This plugin requires the @segment/analytics-react-native-plugin-amplitude-session plugin to extract session IDs from Amplitude integration data. Add this plugin to your Segment client before you add the session replay plugin.
API key matching
The Amplitude API key in the session replay plugin configuration must match the API key in your Segment Amplitude (Actions) destination.
Before you begin
Use the latest version of the Session Replay React Native Segment Plugin above 0.0.1-beta.2.
The Session Replay React Native Segment Plugin requires that:
- You build your application with React Native.
- You use Segment's Analytics React Native library for ingestion.
- You use Segment's Amplitude (Actions) destination.
- You use Segment's Amplitude Session Plugin to extract session IDs.
Quickstart
Install the plugin and its dependencies:
npm install @amplitude/segment-session-replay-plugin-react-native
npm install @amplitude/session-replay-react-native @segment/analytics-react-native
Configure your application code:
import { createSegmentSessionReplayPlugin } from '@amplitude/segment-session-replay-plugin-react-native';
import { createClient } from '@segment/analytics-react-native';
import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-amplitude-session';
// Initialize Segment client
const segmentClient = createClient({
writeKey: 'YOUR_SEGMENT_WRITE_KEY',
});
// Configure session replay plugin
const sessionReplayConfig = {
apiKey: 'YOUR_AMPLITUDE_API_KEY',
deviceId: 'YOUR_DEVICE_ID'
};
// Add the Amplitude session plugin first (required for session ID extraction)
await segmentClient.add({ plugin: new AmplitudeSessionPlugin() });
// Add the session replay plugin to Segment
await segmentClient.add({ plugin: createSegmentSessionReplayPlugin(sessionReplayConfig) });
Configuration
The plugin accepts a SessionReplayConfig object with the following options:
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | - | Your Amplitude API key. This must match the API key for your Segment Amplitude destination. |
deviceId | string | No | - | The device ID for session replay. If not provided, the plugin extracts it from the Segment event context. |
sampleRate | number | No | 0 | Controls how many sessions Amplitude selects for replay collection. Use a decimal between 0 and 1, for example 0.4, to represent the fraction of sessions Amplitude randomly selects for replay collection. |
enableRemoteConfig | boolean | No | true | Enables remote configuration. |
logLevel | LogLevel | No | LogLevel.Warn | Sets the log level for the Session Replay plugin. |
autoStart | boolean | No | true | Controls whether Session Replay starts automatically after initialization. If set to false, manually call the start() method to begin capture. |
How it works
The Segment Session Replay Plugin automatically:
- Initializes Session Replay: sets up Amplitude Session Replay with your configuration when you add the plugin to Segment.
- Syncs session data: updates the session ID and device ID for each Segment event by extracting them from event properties or Amplitude integration data.
- Enriches events: adds session replay properties to
trackandscreenevents before sending them to Segment. - Manages lifecycle: handles start and stop operations for session replay based on the plugin lifecycle.
Event processing
The plugin processes the following Segment event types:
TrackEvent: adds session replay properties to track events.ScreenEvent: adds session replay properties to screen events.
For these events, the plugin:
- Extracts the session ID from event properties or Amplitude integration data.
- Extracts the device ID from the event context or anonymous ID.
- Adds session replay properties to the event before sending it to Segment.
Session ID extraction
The plugin extracts session IDs in the following order of priority:
- From Amplitude integration data:
event.integrations['Actions Amplitude'].session_id. - From event properties:
event.properties.session_id. - Defaults to
-1if Amplitude finds no session ID.
Device ID extraction
The plugin extracts device IDs in the following order of priority:
- From event context:
event.context.device.id. - From anonymous ID:
event.anonymousId. - Defaults to
nullif Amplitude finds no device ID.
Advanced topics
This section provides examples for more advanced use cases.
Manual control
Manually control the session replay plugin after initialization:
import { createSegmentSessionReplayPlugin } from '@amplitude/segment-session-replay-plugin-react-native';
// Create the plugin
const sessionReplayPlugin = createSegmentSessionReplayPlugin(sessionReplayConfig);
// Add to Segment
await segmentClient.add(sessionReplayPlugin);
// Later, manually control recording
await sessionReplayPlugin.start();
await sessionReplayPlugin.stop();
Custom configuration
For more advanced configurations, pass additional session replay options:
const sessionReplayConfig = {
apiKey: 'YOUR_AMPLITUDE_API_KEY',
sampleRate: 0.1, // Sample 10% of sessions
enableRemoteConfig: true,
logLevel: 4, // Debug level
autoStart: false, // Don't start automatically
};
const sessionReplayPlugin = createSegmentSessionReplayPlugin(sessionReplayConfig);
Troubleshooting
Session replay properties not added
If the plugin doesn't add session replay properties to your events:
- Verify that the Amplitude Session Plugin appears before the Session Replay Plugin.
- Check that your Segment events are of type
trackorscreen. - Confirm that the session replay plugin initializes.
Session ID not found
If the plugin can't extract session IDs:
- Verify that you configured the Amplitude Session Plugin correctly.
- Check that you configured the Segment Amplitude destination correctly.
- Confirm that events include the required Amplitude integration data.
Device ID issues
If device ID extraction fails:
- Check that your Segment events include device information in the context.
- Verify that the anonymous ID is set in Segment.
- Consider manually setting the device ID in the plugin configuration.
For additional troubleshooting, go to the Session Replay React Native SDK Plugin troubleshooting guide.
Was this helpful?