UNPKG

@amplitude/plugin-session-replay-react-native

Version:

Amplitude Session Replay plugin for React Native

89 lines (83 loc) 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionReplayPlugin = void 0; var _nativeModule = require("./native-module"); var _version = require("./version"); var _sessionReplayConfig = require("./session-replay-config"); var _analyticsTypes = require("@amplitude/analytics-types"); /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable @typescript-eslint/no-unsafe-return */ class SessionReplayPlugin { name = '@amplitude/plugin-session-replay-react-native'; type = 'enrichment'; // this.config is defined in setup() which will always be called first // @ts-ignore isInitialized = false; constructor(config = {}) { this.sessionReplayConfig = { ...(0, _sessionReplayConfig.getDefaultConfig)(), ...config }; console.log('Initializing SessionReplayPlugin with config: ', this.sessionReplayConfig); } async setup(config, _) { this.config = config; console.log(`Installing @amplitude/plugin-session-replay-react-native, version ${_version.VERSION}.`); await _nativeModule.PluginSessionReplayReactNative.setup(config.apiKey, config.deviceId, config.sessionId, config.serverZone, this.sessionReplayConfig.sampleRate ?? 1, this.sessionReplayConfig.enableRemoteConfig ?? true, this.sessionReplayConfig.logLevel ?? _analyticsTypes.LogLevel.Warn, this.sessionReplayConfig.autoStart ?? true); this.isInitialized = true; } async execute(event) { if (!this.isInitialized) { return Promise.resolve(event); } // On event, synchronize the session id to the what's on the browserConfig (source of truth) // Choosing not to read from event object here, concerned about offline/delayed events messing up the state stored // in SR. if (this.config.sessionId && this.config.sessionId !== (await _nativeModule.PluginSessionReplayReactNative.getSessionId())) { await _nativeModule.PluginSessionReplayReactNative.setSessionId(this.config.sessionId); } // Treating config.sessionId as source of truth, if the event's session id doesn't match, the // event is not of the current session (offline/late events). In that case, don't tag the events if (this.config.sessionId && this.config.sessionId === event.session_id) { const sessionRecordingProperties = await _nativeModule.PluginSessionReplayReactNative.getSessionReplayProperties(); event.event_properties = { ...event.event_properties, ...sessionRecordingProperties }; } return Promise.resolve(event); } async start() { if (this.isInitialized) { await _nativeModule.PluginSessionReplayReactNative.start(); } } async stop() { if (this.isInitialized) { await _nativeModule.PluginSessionReplayReactNative.stop(); } } async teardown() { if (this.isInitialized) { await _nativeModule.PluginSessionReplayReactNative.teardown(); } // the following are initialized in setup() which will always be called first // here we reset them to null to prevent memory leaks // @ts-ignore this.config = null; this.isInitialized = false; } async getSessionReplayProperties() { if (!this.isInitialized) { return {}; } return _nativeModule.PluginSessionReplayReactNative.getSessionReplayProperties(); } } exports.SessionReplayPlugin = SessionReplayPlugin; //# sourceMappingURL=session-replay.js.map