|
| 1 | +# sentry-replay |
| 2 | + |
| 3 | +Note: Session Replay is currently in beta. |
| 4 | + |
| 5 | +## Pre-requisites |
| 6 | + |
| 7 | +For the sentry-replay integration to work, you must have the [Sentry browser SDK package](https://www.npmjs.com/package/@sentry/browser), or an equivalent framework SDK (e.g. [@sentry/react](https://www.npmjs.com/package/@sentry/react)) installed. The minimum version required for the SDK is `7.x`. |
| 8 | + |
| 9 | + |
| 10 | +`@sentry/replay` requires Node 12+, and browsers newer than IE11. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +with npm: |
| 15 | + |
| 16 | +```shell |
| 17 | +npm install --save @sentry/browser @sentry/replay |
| 18 | +``` |
| 19 | + |
| 20 | +with yarn: |
| 21 | + |
| 22 | +```shell |
| 23 | +yarn add @sentry/browser @sentry/replay |
| 24 | +``` |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +To set up the integration, add the following to your Sentry initialization. Several options are supported and passable via the integration constructor. |
| 29 | +See the [configuration section](#configuration) below for more details. |
| 30 | + |
| 31 | +```javascript |
| 32 | +import * as Sentry from '@sentry/browser'; |
| 33 | +import { Replay } from '@sentry/replay'; |
| 34 | + |
| 35 | +Sentry.init({ |
| 36 | + dsn: '__DSN__', |
| 37 | + integrations: [ |
| 38 | + new Replay({ |
| 39 | + // This sets the sample rate to be 10%. You may want this to be 100% while |
| 40 | + // in development and sample at a lower rate in production |
| 41 | + sessionSampleRate: 0.1, |
| 42 | + |
| 43 | + // If the entire session is not sampled, use the below sample rate to sample |
| 44 | + // sessions when an error occurs. |
| 45 | + errorSampleRate: 1.0, |
| 46 | + |
| 47 | + // Mask all text content with asterisks (*). Passes text |
| 48 | + // content through to `maskTextFn` before sending to server. |
| 49 | + // |
| 50 | + // Defaults to true, uncomment to change |
| 51 | + // maskAllText: true, |
| 52 | + |
| 53 | + // Block all media elements (img, svg, video, object, |
| 54 | + // picture, embed, map, audio) |
| 55 | + // |
| 56 | + // Defaults to true, uncomment to change |
| 57 | + // blockAllMedia: true, |
| 58 | + }) |
| 59 | + ], |
| 60 | + // ... |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +### Identifying Users |
| 65 | + |
| 66 | +If you have only followed the above instructions to setup session replays, you will only see IP addresses in Sentry's UI. In order to associate a user identity to a session replay, use [`setUser`](https://docs.sentry.io/platforms/javascript/enriching-events/identify-user/). |
| 67 | + |
| 68 | +```javascript |
| 69 | +import * as Sentry from "@sentry/browser"; |
| 70 | +Sentry. setUser({ email : "[email protected]" }); |
| 71 | +``` |
| 72 | + |
| 73 | +### Start and Stop Recording |
| 74 | + |
| 75 | +Replay recording only starts automatically when it is included in the `integrations` key when calling `Sentry.init`. Otherwise you can initialize the plugin and manually call the `start()` method on the integration instance. To stop recording you can call the `stop()`. |
| 76 | + |
| 77 | +```javascript |
| 78 | +const replay = new Replay(); // This will *NOT* begin recording replays |
| 79 | + |
| 80 | +replay.start(); // Start recording |
| 81 | + |
| 82 | +replay.stop(); // Stop recording |
| 83 | +``` |
| 84 | + |
| 85 | +## Sessions |
| 86 | + |
| 87 | +A session starts when the Session Replay SDK is first loaded and initialized. The session will continue until 5 minutes passes without any user interactions[^1] with the application *OR* until a maximum of 30 minutes have elapsed. Closing the browser tab will end the session immediately according to the rules for [SessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage). |
| 88 | + |
| 89 | +[^1]: An 'interaction' refers to either a mouse click or a browser navigation event. |
| 90 | + |
| 91 | +### Replay Captures Only on Errors |
| 92 | + |
| 93 | +Alternatively, rather than recording an entire session, you can capture a replay only when an error occurs. In this case, the integration will buffer up to one minute worth of events prior to the error being thrown. It will continue to record the session following the rules above regarding session life and activity. Read the [sampling](#Sampling) section for configuration options. |
| 94 | + |
| 95 | +## Sampling |
| 96 | + |
| 97 | +Sampling allows you to control how much of your website's traffic will result in a Session Replay. There are two sample rates you can adjust to get the replays more relevant to your interests: |
| 98 | + |
| 99 | +- `sessionSampleRate` - The sample rate for replays that begin recording immediately and last the entirety of the user's session. |
| 100 | +- `errorSampleRate` - The sample rate for replays that are recorded when an error happens. This type of replay will record up to a minute of events prior to the error and continue recording until the session ends. |
| 101 | + |
| 102 | +Sampling occurs when the session is first started. `sessionSampleRate` is evaluated first. If it is sampled, then the replay recording begins. Otherwise, `errorSampleRate` is evaluated and if it is sampled, the integration will begin buffering the replay and will only upload a replay to Sentry when an error occurs. The remainder of the replay will behave similarly to a whole-session replay. |
| 103 | + |
| 104 | + |
| 105 | +## Configuration |
| 106 | + |
| 107 | +### General Configuration |
| 108 | + |
| 109 | +| key | type | default | description | |
| 110 | +| ------------------- | ------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 111 | +| sessionSampleRate | number | `0.1` | The sample rate for all sessions, which will capture the entirety from when a user begins a session until the session ends. (1.0 will collect all replays, 0 will collect no replays) | |
| 112 | +| errorSampleRate | number | `1.0` | If a session isn't already being recorded via `sessionSampleRate`, based on `errorSampleRate` the SDK will send the captured replay when an error occurs. (1.0 capturing all sessions with an error, and 0 capturing none). | |
| 113 | +| stickySession | boolean | `true` | Keep track of the user across page loads. Note a single user using multiple tabs will result in multiple sessions. Closing a tab will result in the session being closed as well. | |
| 114 | + |
| 115 | +### Privacy Configuration |
| 116 | + |
| 117 | +| key | type | default | description | |
| 118 | +| ---------------- | ------------------------ | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 119 | +| maskAllText | boolean | `true` | Mask _all_ text content. Will pass text content through `maskTextFn` before sending to server. | |
| 120 | +| blockAllMedia | boolean | `true` | Block _all_ media elements (`img, svg, video, object, picture, embed, map, audio`) |
| 121 | +| maskTextFn | (text: string) => string | `(text) => '*'.repeat(text.length)` | Function to customize how text content is masked before sending to server. By default, masks text with `*`. | |
| 122 | +| maskAllInputs | boolean | `true` | Mask values of `<input>` elements. Passes input values through `maskInputFn` before sending to server. | |
| 123 | +| maskInputOptions | Record<string, boolean> | `{ password: true }` | Customize which inputs `type` to mask. <br /> Available `<input>` types: `color, date, datetime-local, email, month, number, range, search, tel, text, time, url, week, textarea, select, password`. | |
| 124 | +| maskInputFn | (text: string) => string | `(text) => '*'.repeat(text.length)` | Function to customize how form input values are masked before sending to server. By default, masks values with `*`. | |
| 125 | +| blockClass | string \| RegExp | `'sentry-block'` | Redact all elements that match the class name. See [privacy](#blocking) section for an example. | |
| 126 | +| blockSelector | string | `'[data-sentry-block]'` | Redact all elements that match the DOM selector. See [privacy](#blocking) section for an example. | |
| 127 | +| ignoreClass | string \| RegExp | `'sentry-ignore'` | Ignores all events on the matching input field. See [privacy](#ignoring) section for an example. | |
| 128 | +| maskTextClass | string \| RegExp | `'sentry-mask'` | Mask all elements that match the class name. See [privacy](#masking) section for an example. | |
| 129 | + |
| 130 | +### Optimization Configuration |
| 131 | + |
| 132 | +| key | type | default | description | |
| 133 | +| ---------------- | ----------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 134 | +| collectFonts | boolean | `false` | Should collect fonts used on the website | |
| 135 | +| inlineImages | boolean | `false` | Should inline `<image>` content | |
| 136 | +| inlineStylesheet | boolean | `true` | Should inline stylesheets used in the recording | |
| 137 | +| recordCanvas | boolean | `false` | Should record `<canvas>` elements | |
| 138 | +| slimDOMOptions | Record<string, boolean> | `{}` | Remove unnecessary parts of the DOM <br /> Available keys: `script, comment, headFavicon, headWhitespace, headMetaDescKeywords, headMetaSocial, headMetaRobots, headMetaHttpEquiv, headMetaAuthorship, headMetaVerification` | |
| 139 | + |
| 140 | +## Privacy |
| 141 | +There are several ways to deal with PII. By default, the integration will mask all text content with `*` and block all media elements (`img, svg, video, object, picture, embed, map, audio`). This can be disabled by setting `maskAllText` to `false`. It is also possible to add the following CSS classes to specific DOM elements to prevent recording its contents: `sentry-block`, `sentry-ignore`, and `sentry-mask`. The following sections will show examples of how content is handled by the differing methods. |
| 142 | + |
| 143 | +### Masking |
| 144 | +Masking replaces the text content with something else. The default masking behavior is to replace each character with a `*`. In this example the relevant html code is: `<table class="sentry-mask">...</table>`. |
| 145 | + |
| 146 | + |
| 147 | +### Blocking |
| 148 | +Blocking replaces the element with a placeholder that has the same dimensions. The recording will show an empty space where the content was. In this example the relevant html code is: `<table data-sentry-block>...</table>`. |
| 149 | + |
| 150 | + |
| 151 | +### Ignoring |
| 152 | +Ignoring only applies to form inputs. Events will be ignored on the input element so that the replay does not show what occurs inside of the input. In the below example, notice how the results in the table below the input changes, but no text is visible in the input. |
| 153 | + |
| 154 | +https://user-images.githubusercontent.com/79684/192815134-a6451c3f-d3cb-455f-a699-7c3fe04d0a2e.mov |
| 155 | + |
0 commit comments