JavaScript SDK - Initializing the SDK
Installation
Section titled InstallationThe following line will install the tracker and with it the SDK for you to take advantage of all tracker features.
npm i @openreplay/tracker
Initialization
Section titled InitializationWhen instantiating OpenReplay’s tracker, there are several configuration options you can provide to customize many aspects of the recording and the recording experience.
You must set the projectKey option in the constructor. You can get this value from your OpenReplay dashboard under ‘Preferences > Projects’.
Single Page Applications (SPA)
Section titled Single Page Applications (SPA)If your website is a Single Page Application (SPA) use the below code:
import OpenReplay from '@openreplay/tracker';
//...
const tracker = new OpenReplay({
projectKey: PROJECT_KEY
});
tracker.start(); // returns a promise with session info (sessionID, sessionHash, userUUID)
Server-side Rendered (SSR)
Section titled Server-side Rendered (SSR)Otherwise, if your web app is Server-Side-Rendered (SSR) (i.e. NextJS, NuxtJS) use the below snippet. Ensure tracker.start() is called once the app is started (in useEffect or componentDidMount).
import OpenReplay from '@openreplay/tracker/cjs';
//...
const tracker = new OpenReplay({
projectKey: PROJECT_KEY,
});
//...
function MyApp() {
useEffect(() => { // use componentDidMount in case of React Class Component
tracker.start(); // returns a promise with session info (sessionID, sessionHash, userUUID)
}, [])
}
Example
Section titled Exampleimport OpenReplay from '@openreplay/tracker/cjs';
//...
const tracker = new OpenReplay({
projectKey: PROJECT_KEY,
ingestPoint: "https://openreplay.mydomain.com/ingest", // when dealing with the self-hosted version of OpenReplay
capturePerformance: true,
__DISABLE_SECURE_MODE: true // for local testing
});
Initialization Options
Section titled Initialization OptionsThere are a set of options you can pass to the constructor. Only projectKey is required.
General Purpose
Section titled General PurposeprojectKey: stringThe ID of the project you’re tracking.sessionHash?: stringThe hash of the initial session. This is useful when sessions traverse different subdomains on your web app but you want to stitch them into a single recording. In case it’s not possible to continue the session (doesn’t exist or is finished), the tracker will automatically start a new one. It’s also returned onstop(). More details on this here.ingestPoint?: stringYour OpenReplay domain (i.e. https://openreplay.mydomain.com/ingest), to which the tracker will be sending events. This is optional for OpenReplay Cloud users. Default:https://api.openreplay.com/ingest(which points to OpenReplay Cloud).revID?: stringThe revision ID of your web app. Useful when searching for issues happening on a specific release version.resourceBaseHref?: stringRefers to the publicly accessible domain where assets (styles, fonts and icons) could be fetched by OpenReplay. Since they’re required for proper session replay, this option is useful to get around the limitation of having your site (and therefore assets) hosted in a private domain. Example:https://mypublicsite.com/assets/.captureIFrames?: booleanFor capturing all of the same-domain iFrames in your web app. If you wish to track a specific iFrame, then instead simply add thedata-openreplay-captureHTML attribute to the<iframe>tag. Default:true.heatmaps?: booleanFor disabling click maps. Default:true.verbose?: booleanFor enabling logs. Default:false.autoResetOnWindowOpen?: booleanEnable this option to reset the sessionID when opening a new tab from your application. This overwrites thewindow.openmethod to avoid duplicate sessionIDs due to shared session storage between browser tabs. Default:false.
Privacy
Section titled PrivacyrespectDoNotTrack?: booleanDo not start tracker if the do-not-track flag is enabled in the user’s browser. Default:false.obscureTextEmails?: booleanObscures emails in text elements. Emails will be converted to a random chain of asterisks. Default:true.obscureTextNumbers?: booleanObscures numbers in text elements. Numbers will be converted to a random chain of asterisks. Default:false.obscureInputEmails?: booleanObscures emails in input fields. Email values will be converted to a random chain of asterisks. Default:true.defaultInputMode?: 0 | 1 | 2Default capture mode for input values. Respectively: plain, obscured or ignored. Default:0(plain).
Note that excluded data is obscured or suppressed before sending the data to OpenReplay servers. Changes applied to the above options cannot be retroactive and will only apply to newly collected data. See Sanitize Data for more details.
Console
Section titled ConsoleconsoleMethods?: Array<'log' | 'info' | 'warn' | 'error' 'debug' | 'assert'> | nullSpecifies the list of console methods to capture. Default:['log', 'info', 'warn', 'error', 'debug', 'assert']consoleThrottling?: numberMax number of captured console entries per second. Default:30.
Exceptions
Section titled ExceptionscaptureExceptions?: booleanCaptures JavaScript exceptions and stacktraces. Default:true.
Timings
Section titled TimingscaptureResourceTimings?: booleanLogs resource timings. Default:true.capturePageLoadTimings?: booleanLogs page load timings. Default:true.capturePageRenderTimings?: booleanComputes page rendering metrics such as Speed Index, Visually Complete or Time To Interactive. RequirescaptureResourceTimings = true. Default:true.
Performance
Section titled PerformancecapturePerformance?: booleanFor capturing performance metrics such as framerate, CPU and memory consumption. Default:true.
Network
Section titled NetworkThe network option relates to capturing network requests and payloads for fetch and XHR.
network?: {
failuresOnly: boolean;
sessionTokenHeader: string;
ignoreHeaders: Array<string> | boolean;
capturePayload: boolean; // make sure you sanitize your data before enabling it
sanitizer: (RequestResponseData) => RequestResponseData | null;
}
See Network Options for more details and examples.
Reconnections
Section titled ReconnectionsconnAttemptCount?: numberMax number of retries when tracker’s HTTP requests fail to reach the backend. Default:10.connAttemptGap?: numberDuration between each retry attempt (expressed in ms). Default:8000.
Security
Section titled Security__DISABLE_SECURE_MODE?: booleanFor allowing insecure connection between tracker and backend on sites without SSL. This should be used for development purposes only. Default:false.