@statsig/session-replay 0.0.1-beta.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # session-replay
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build session-replay` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test session-replay` to execute the unit tests via [Jest](https://jestjs.io).
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@statsig/session-replay",
3
+ "version": "0.0.1-beta.23",
4
+ "dependencies": {
5
+ "rrweb": "2.0.0-alpha.12",
6
+ "@statsig/client-core": "0.0.1-beta.23"
7
+ },
8
+ "devDependencies": {
9
+ "@rrweb/types": "2.0.0-alpha.12"
10
+ },
11
+ "type": "commonjs",
12
+ "main": "./src/index.js",
13
+ "typings": "./src/index.d.ts"
14
+ }
@@ -0,0 +1,12 @@
1
+ import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
+ export declare class SessionReplay {
3
+ private _client;
4
+ private _replayer;
5
+ private _sessionData;
6
+ private _events;
7
+ constructor(_client: PrecomputedEvaluationsInterface);
8
+ private _onRecordingEvent;
9
+ private _attemptToStartRecording;
10
+ private _shutdown;
11
+ private _flush;
12
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionReplay = void 0;
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const SessionReplayClient_1 = require("./SessionReplayClient");
6
+ const MAX_REPLAY_PAYLOAD_BYTES = 2048;
7
+ class SessionReplay {
8
+ constructor(_client) {
9
+ this._client = _client;
10
+ this._sessionData = null;
11
+ this._events = [];
12
+ this._replayer = new SessionReplayClient_1.SessionReplayClient();
13
+ this._client.on('pre_shutdown', () => this._shutdown());
14
+ this._client.on('values_updated', () => this._attemptToStartRecording());
15
+ this._attemptToStartRecording();
16
+ }
17
+ _onRecordingEvent(event, data) {
18
+ this._sessionData = data;
19
+ this._events.push(event);
20
+ const payload = JSON.stringify(this._events);
21
+ if (payload.length > MAX_REPLAY_PAYLOAD_BYTES) {
22
+ this._flush(payload, data);
23
+ }
24
+ }
25
+ _attemptToStartRecording() {
26
+ var _a;
27
+ const values = this._client.getContext().values;
28
+ if ((values === null || values === void 0 ? void 0 : values.can_record_session) !== true) {
29
+ this._shutdown();
30
+ return;
31
+ }
32
+ const sampling = (_a = values.session_recording_rate) !== null && _a !== void 0 ? _a : 0;
33
+ if (Math.random() >= sampling) {
34
+ this._shutdown();
35
+ return;
36
+ }
37
+ client_core_1.StatsigMetadataProvider.add({ isRecordingSession: 'true' });
38
+ this._replayer.record((e, d) => this._onRecordingEvent(e, d));
39
+ }
40
+ _shutdown() {
41
+ this._replayer.stop();
42
+ client_core_1.StatsigMetadataProvider.add({ isRecordingSession: 'false' });
43
+ if (this._events.length === 0 || this._sessionData == null) {
44
+ return;
45
+ }
46
+ const payload = JSON.stringify(this._events);
47
+ this._flush(payload, this._sessionData);
48
+ }
49
+ _flush(payload, data) {
50
+ // prevent blowing up the log queue
51
+ this._client.flush().catch((err) => {
52
+ client_core_1.Log.error(err);
53
+ });
54
+ const { sdkVersion } = client_core_1.StatsigMetadataProvider.get();
55
+ this._client.logEvent({
56
+ eventName: 'statsig::session_recording',
57
+ value: this._client.getContext().sessionID,
58
+ metadata: {
59
+ session_start_ts: String(data.startTime),
60
+ session_end_ts: String(data.endTime),
61
+ clicks_captured_cumulative: String(data.clickCount),
62
+ rrweb_events: payload,
63
+ session_replay_sdk_version: sdkVersion,
64
+ },
65
+ });
66
+ this._events = [];
67
+ }
68
+ }
69
+ exports.SessionReplay = SessionReplay;
@@ -0,0 +1,17 @@
1
+ import { eventWithTime } from '@rrweb/types';
2
+ import { Flatten } from '@statsig/client-core';
3
+ export type ReplayEvent = Flatten<eventWithTime>;
4
+ export type ReplaySessionData = {
5
+ startTime: number;
6
+ endTime: number;
7
+ clickCount: number;
8
+ };
9
+ export declare class SessionReplayClient {
10
+ private _stopFn;
11
+ private _stopCallback?;
12
+ private _startTimestamp;
13
+ private _endTimestamp;
14
+ private _clickCount;
15
+ record(callback: (latest: ReplayEvent, data: ReplaySessionData) => void, stopCallback?: () => void): void;
16
+ stop(): void;
17
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionReplayClient = void 0;
4
+ const rrweb_1 = require("rrweb");
5
+ const TIMEOUT_MS = 1000 * 60 * 30;
6
+ class SessionReplayClient {
7
+ constructor() {
8
+ this._startTimestamp = null;
9
+ this._endTimestamp = null;
10
+ this._clickCount = 0;
11
+ }
12
+ record(callback, stopCallback) {
13
+ if (typeof window === 'undefined') {
14
+ return;
15
+ }
16
+ // Always reset session id and tracking fields for a new recording
17
+ this._startTimestamp = null;
18
+ this._endTimestamp = null;
19
+ this._clickCount = 0;
20
+ this._stopCallback = stopCallback;
21
+ if (this._stopFn) {
22
+ return;
23
+ }
24
+ const emit = (event) => {
25
+ var _a, _b;
26
+ // Reset start only for the first event
27
+ (_a = this._startTimestamp) !== null && _a !== void 0 ? _a : (this._startTimestamp = event.timestamp);
28
+ // Always keep a running end timestamp
29
+ this._endTimestamp = event.timestamp;
30
+ // Count clicks only for events representing a click
31
+ if (_isClickEvent(event)) {
32
+ this._clickCount++;
33
+ }
34
+ callback(event, {
35
+ startTime: this._startTimestamp,
36
+ endTime: this._endTimestamp,
37
+ clickCount: this._clickCount,
38
+ });
39
+ if (this._endTimestamp - this._startTimestamp > TIMEOUT_MS) {
40
+ (_b = this._stopFn) === null || _b === void 0 ? void 0 : _b.call(this);
41
+ if (this._stopCallback) {
42
+ this._stopCallback();
43
+ }
44
+ }
45
+ };
46
+ this._stopFn = (0, rrweb_1.record)({ emit });
47
+ }
48
+ stop() {
49
+ if (this._stopFn) {
50
+ this._stopFn();
51
+ this._stopFn = undefined;
52
+ }
53
+ }
54
+ }
55
+ exports.SessionReplayClient = SessionReplayClient;
56
+ function _isClickEvent(event) {
57
+ return (event.type === rrweb_1.EventType.IncrementalSnapshot &&
58
+ event.data.source === rrweb_1.IncrementalSource.MouseInteraction &&
59
+ event.data.type === rrweb_1.MouseInteractions.Click);
60
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { SessionReplay } from './SessionReplay';
2
+ import { SessionReplayClient } from './SessionReplayClient';
3
+ export type { ReplaySessionData as ReplayData, ReplayEvent, } from './SessionReplayClient';
4
+ export { SessionReplayClient, SessionReplay };
package/src/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionReplay = exports.SessionReplayClient = void 0;
4
+ const SessionReplay_1 = require("./SessionReplay");
5
+ Object.defineProperty(exports, "SessionReplay", { enumerable: true, get: function () { return SessionReplay_1.SessionReplay; } });
6
+ const SessionReplayClient_1 = require("./SessionReplayClient");
7
+ Object.defineProperty(exports, "SessionReplayClient", { enumerable: true, get: function () { return SessionReplayClient_1.SessionReplayClient; } });
8
+ __STATSIG__ = Object.assign(Object.assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { SessionReplayClient: SessionReplayClient_1.SessionReplayClient,
9
+ SessionReplay: SessionReplay_1.SessionReplay });