Skip to content

React Native Session Replay

Prerequisites

  • Make sure you have set up and initialized the @truewatchtech/react-native-mobile RUM configuration and enabled View monitoring collection.
  • React Native Session Replay requires @truewatchtech/react-native-mobile@0.4.0 or later.
  • Starting from @truewatchtech/react-native-mobile@0.4.1, Session Replay is provided by @truewatchtech/react-native-session-replay. Install both @truewatchtech/react-native-mobile and @truewatchtech/react-native-session-replay, and keep the versions of the two libraries consistent.

  • We recommend using the official stable version shown in the badge at the top of this page, rather than pre-release versions such as alpha or beta.

  • If you want to try new capabilities early, follow the latest features, or keep track of unreleased changes, you can follow the React Native SDK repository and its changelogs on GitHub: TrueWatchTech/datakit-react-native.

Configuration

npm install @truewatchtech/react-native-mobile@last_version @truewatchtech/react-native-session-replay@last_version
# or
yarn add @truewatchtech/react-native-mobile@last_version @truewatchtech/react-native-session-replay@last_version

Code Sample

The following example applies to version 0.4.1 and later. Session Replay APIs are imported from @truewatchtech/react-native-session-replay.

import {
  FTReactNativeSessionReplay,
  FTSessionReplayConfig,
  TextAndInputPrivacyLevel,
} from '@truewatchtech/react-native-session-replay';
 let sessionReplayConfig: FTSessionReplayConfig = {
-  sampleRate: 1,
+  sessionReplaySamplingRate: 100, // Supported since SDK 0.4.3
   textAndInputPrivacy: TextAndInputPrivacyLevel.MASK_SENSITIVE_INPUTS,
 };
 await FTReactNativeSessionReplay.sessionReplayConfig(sessionReplayConfig);
Property Type Required Description
sessionReplaySamplingRate number No Session Replay sampling percentage, range [0,100], decimals supported, default 100. This sampling rate is applied on top of RUM sampling and takes precedence over sampleRate. Supported since SDK 0.4.3.
sessionReplayOnErrorSamplingRate number No Error replay sampling percentage used when a session is not selected by regular replay sampling, range [0,100], decimals supported, default 0, takes precedence over sessionReplayOnErrorSampleRate. Supported since SDK 0.4.3.
sampleRate number No Deprecated since SDK 0.4.3, retained for compatibility. We recommend using sessionReplaySamplingRate. Value range [0,1]; 0 means no collection and 1 means full collection; default is 1. This sampling rate is applied on top of RUM sampling.
sessionReplayOnErrorSampleRate number No Deprecated since SDK 0.4.3, retained for compatibility. We recommend using sessionReplayOnErrorSamplingRate. Value range [0,1]; 0 means no collection and 1 means full collection; default is 0.
privacy SessionReplayPrivacy No Sets the privacy level for content masking in Session Replay. This parameter is deprecated and retained for compatibility with older versions; we recommend using touchPrivacy, textAndInputPrivacy, and imagePrivacy for fine-grained privacy configuration.
SessionReplayPrivacy.ALLOW: Does not mask private data except for sensitive input controls, such as password input fields.
SessionReplayPrivacy.MASK_USER_INPUT: Masks part of the user input data, including text in input fields, Switch, and so on.
SessionReplayPrivacy.MASK: Masks private data, including text, Switch, and so on. Will be deprecated; still supported for compatibility. We recommend using touchPrivacy, textAndInputPrivacy, and imagePrivacy for privacy settings.
touchPrivacy TouchPrivacyLevel No Sets the privacy level for touch interactions in Session Replay.
TouchPrivacyLevel.SHOW: Shows touch interactions.
TouchPrivacyLevel.HIDE: Hides touch interactions. Overrides the privacy setting once configured. Supported in SDK 0.4.1 and later.
textAndInputPrivacy TextAndInputPrivacyLevel No Sets the privacy level for text and input content in Session Replay.
TextAndInputPrivacyLevel.MASK_SENSITIVE_INPUTS: Masks sensitive inputs only.
TextAndInputPrivacyLevel.MASK_ALL_INPUTS: Masks all input content.
TextAndInputPrivacyLevel.MASK_ALL: Masks all text and input content. Overrides the privacy setting once configured. Supported in SDK 0.4.1 and later.
imagePrivacy ImagePrivacyLevel No Sets the privacy level for image content in Session Replay.
ImagePrivacyLevel.MASK_NON_BUNDLED_ONLY: Masks non-bundled images only.
ImagePrivacyLevel.MASK_ALL: Masks all images.
ImagePrivacyLevel.MASK_NONE: Does not mask images. Overrides the privacy setting once configured. Supported in SDK 0.4.1 and later.

View Privacy Overrides

Supported in SDK 0.4.1 and later.

FTSessionReplayView is used to set Session Replay privacy overrides for a partial view tree. This component behaves the same as React Native View. It can wrap components that need their own privacy policy, or directly replace an existing View. Privacy overrides apply to its child views; when overrides are nested, a child view uses the privacy configuration of the nearest parent.

import {
  FTSessionReplayView,
  ImagePrivacyLevel,
  TextAndInputPrivacyLevel,
  TouchPrivacyLevel,
} from '@truewatchtech/react-native-session-replay';
Component Purpose Properties
FTSessionReplayView.Privacy Customizes the text, input, image, and touch privacy levels for child views, and can also hide the entire child view. textAndInputPrivacy?: TextAndInputPrivacyLevel
imagePrivacy?: ImagePrivacyLevel
touchPrivacy?: TouchPrivacyLevel
hide?: boolean
FTSessionReplayView.MaskAll Applies the strictest masking policy to child views, masking text, inputs, and images. showTouch?: boolean; when set to true, touch interactions are shown.
FTSessionReplayView.MaskNone Applies a lower masking policy to child views, so non-sensitive content and images are displayed as-is as much as possible. No additional properties.
FTSessionReplayView.Hide Completely hides the child view content in Session Replay. No additional properties.

Wrapping a Local Component

import { TextInput, View } from 'react-native';
import { FTSessionReplayView } from '@truewatchtech/react-native-session-replay';
export function UserForm() {
  return (
    <View>
      <FTSessionReplayView.MaskAll showTouch={true}>
        <TextInput placeholder="First Name" value="Data" />
        <TextInput placeholder="Last Name" value="Flux" />
      </FTSessionReplayView.MaskAll>
    </View>
  );
}

Replacing an Existing View

FTSessionReplayView inherits the properties of React Native View, so the style on the original View can be passed in directly.

import { StyleSheet, Text, TextInput } from 'react-native';
import { FTSessionReplayView } from '@truewatchtech/react-native-session-replay';
export function ProfileCard() {
  return (
    <FTSessionReplayView.MaskNone style={styles.card}>
      <Text>Public display name</Text>
      <TextInput placeholder="Email" value="user@example.com" />
    </FTSessionReplayView.MaskNone>
  );
}

const styles = StyleSheet.create({
  card: {
    padding: 12,
    borderRadius: 8,
    backgroundColor: '#FFFFFF',
  },
});

Combining Different Privacy Policies

import {
  FTSessionReplayView,
  ImagePrivacyLevel,
  TextAndInputPrivacyLevel,
  TouchPrivacyLevel,
} from '@truewatchtech/react-native-session-replay';
export function Checkout() {
  return (
    <FTSessionReplayView.Privacy
      textAndInputPrivacy={TextAndInputPrivacyLevel.MASK_SENSITIVE_INPUTS}
      imagePrivacy={ImagePrivacyLevel.MASK_NONE}
      touchPrivacy={TouchPrivacyLevel.SHOW}>
      {/* Regular content */}
      <FTSessionReplayView.MaskAll showTouch={true}>
        {/* Form content that requires strict masking */}
      </FTSessionReplayView.MaskAll>
      <FTSessionReplayView.Hide>
        {/* Content you do not want to show in replay */}
      </FTSessionReplayView.Hide>
    </FTSessionReplayView.Privacy>
  );
}

When you use FTSessionReplayView.Hide, child views are hidden in Session Replay.

WebView Session Replay

Supported since SDK 0.4.1, for both Android and iOS.

When you use React Native WebView to load web pages, first complete the React Native RUM and the Session Replay configuration on this page, and enable View monitoring collection. On Android, you also need to follow Apply Plugin to complete the ft-plugin configuration, apply the plugin in the app's main module, then rebuild and install the app.

Complete the app-side configuration according to WebView Data Monitoring, confirm that FTRUMConfig.enableTraceWebView is true (the default), and that the WebView host configuration allows collecting the target page. Load the WebView only after app-side initialization is complete, and integrate the page according to WebView Page Initialization.

In the page loaded by the WebView, start recording after the Web SDK RUM initialization is complete:

window.DATAFLUX_RUM.startSessionReplayRecording();

For WebViews, we recommend using the Web SDK's privacy configuration to manage content within the page; the view privacy overrides on the React Native side mainly apply to the native view tree.

Code and Configuration Reference