> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flashcat.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK Configuration

> Master Flashduty RUM's session replay feature to quickly locate issues and optimize user experience by recreating user operation paths.

Flashduty RUM's session replay feature is integrated into the RUM SDK. By simply configuring sampling rates and privacy rules, you can quickly enable the replay feature.

## Enable Collection

<Tabs>
  <Tab title="Automatic Collection">
    The replay SDK is integrated into the RUM SDK. Configure the sampling rate to enable the replay feature:

    ```javascript theme={null}
    window.FC_RUM.init({
      applicationId: "YOUR_APPLICATION_ID",
      clientToken: "YOUR_CLIENT_TOKEN",
      // ...
      sessionReplaySampleRate: 10, // Default session replay sampling rate 10%
      // ...
    });
    ```

    <Note>
      **Sampling Method**: When initializing a session in the client SDK, a random number between 0-1 is generated and compared with `rate/100`. If it falls within the range, the session becomes a sample and replay data is collected and reported throughout the session lifecycle.

      Based on the session being sampled, the session replay sampling rate (sessionReplaySampleRate) is calculated and sampled a second time.
    </Note>
  </Tab>

  <Tab title="Manual Collection">
    After configuring the default sampling rate, automatic collection starts after `RUM.init()` executes. If you want to manually control collection timing (such as collecting data only after user login), you can enable the manual collection switch and then manually call the record method:

    ```javascript theme={null}
    window.FC_RUM.init({
      applicationId: "YOUR_APPLICATION_ID",
      clientToken: "YOUR_CLIENT_TOKEN",
      // ...
      sessionReplaySampleRate: 10, // Sampling rate 10%
      startSessionReplayRecordingManually: true, // Enable manual collection switch
      // ...
    });

    if (userIsShouldRecord()) {
      // If certain conditions are met, enable replay
      window.FC_RUM.startSessionReplayRecording(); // Start data collection when called
    }
    ```

    <Tip>
      After enabling collection, you can stop collection using the `stopSessionReplayRecording()` method.
    </Tip>
  </Tab>

  <Tab title="Force Enable">
    In some scenarios, even if the sampling rate doesn't hit, you may want to collect data for that session (such as closely monitoring newly launched features, or capturing subsequent operations after catching an exception). You can force enable replay by calling `startSessionReplayRecording({ force: true })`.

    <Warning>
      Force enable only works when the current session is sampled but sessionReplay is not sampled. If the session itself is not sampled, force enabling replay will have no effect.
    </Warning>
  </Tab>
</Tabs>

## Disable Collection

If you need to disable the collection feature, set the corresponding replay sampling rate to 0 or simply remove the configuration item:

```javascript theme={null}
window.FC_RUM.init({
  applicationId: "YOUR_APPLICATION_ID",
  clientToken: "YOUR_CLIENT_TOKEN",
  // ...
  sessionReplaySampleRate: 0, // Disable replay feature
  // ...
});
```

## How It Works

The session replay SDK is based on [rrweb](https://www.rrweb.io/).

<Steps>
  <Step title="Recording Phase">
    The recording SDK takes snapshots of the current DOM and CSS styles, and collects corresponding events when user behaviors occur (DOM changes, mouse movement, clicks, form input, etc.). Data is reported after serialization, compression, and sensitive information removal.
  </Step>

  <Step title="Replay Phase">
    The playback SDK reconstructs the DOM based on snapshots and converts event behaviors into DOM changes for display at appropriate times.
  </Step>
</Steps>

<Info>
  * Before data reporting, the SDK compresses data in advance and executes this CPU-intensive operation in a Web Worker, without affecting main thread rendering
  * SDK compatibility is consistent with RUM SDK, supporting IE11 and above browsers
</Info>

## Next Steps

<CardGroup cols={2}>
  <Card title="View Session Replay" icon="play" href="./session-viewing">
    Learn how to view replay records
  </Card>

  <Card title="Privacy Protection" icon="shield" href="./privacy-protection">
    Learn about privacy protection settings
  </Card>
</CardGroup>
