@amplitude/session-replay-react-native
Version:
Amplitude Session Replay for React Native
226 lines (215 loc) • 6.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.flush = flush;
exports.getSessionId = getSessionId;
exports.getSessionReplayProperties = getSessionReplayProperties;
exports.init = init;
exports.privateInit = privateInit;
exports.setDeviceId = setDeviceId;
exports.setSessionId = setSessionId;
exports.start = start;
exports.stop = stop;
var _nativeModule = require("./native-module");
var _sessionReplayConfig = require("./session-replay-config");
var _logger = require("./logger");
var _version = require("./version");
// @refresh reset
let fullConfig = null;
let isInitialized = false;
let logger = (0, _logger.createSessionReplayLogger)();
/**
* Configure the SDK and begin collecting replays.
* This function must be called before any other session replay operations.
*
* @param config - Configuration object containing API key, device ID, session ID, and other options
* @returns Promise that resolves when initialization is complete
* @throws Error if initialization fails
*
* @example
* ```typescript
* await init({
* apiKey: 'YOUR_API_KEY',
* deviceId: 'user-device-id',
* sessionId: Date.now(),
* sampleRate: 0.1
* });
* ```
*/
async function init(config) {
if (isInitialized) {
logger.warn('SessionReplay is already initialized');
return;
}
fullConfig = {
...(0, _sessionReplayConfig.getDefaultConfig)(),
...config
};
logger.setLogLevel(fullConfig.logLevel);
logger.log(`initializing @amplitude/session-replay-react-native version: ${_version.VERSION} with config: `, fullConfig);
try {
await _nativeModule.NativeSessionReplay.setup(nativeConfig(fullConfig));
logger.log('SessionReplay initialized');
isInitialized = true;
} catch (error) {
logger.error('Error initializing SessionReplay', error);
}
}
/**
* Call whenever the session ID changes.
* The Session ID you pass to the SDK must match the Session ID sent as event properties to Amplitude.
*
* @param sessionId - The new session identifier number
* @returns Promise that resolves when the session ID is updated
*
* @example
* ```typescript
* await setSessionId(Date.now());
* ```
*/
async function setSessionId(sessionId) {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return;
}
await _nativeModule.NativeSessionReplay.setSessionId(sessionId);
}
/**
* Update the device ID used for session replay tracking.
* The Device ID you pass to the SDK must match the Device ID sent as event properties to Amplitude.
*
* @param deviceId - The device identifier string, or null to clear the device ID
* @returns Promise that resolves when the device ID is updated
*
* @example
* ```typescript
* await setDeviceId('user-device-id');
* // or clear device ID
* await setDeviceId(null);
* ```
*/
async function setDeviceId(deviceId) {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return;
}
await _nativeModule.NativeSessionReplay.setDeviceId(deviceId);
}
/**
* Get the current session identifier from the session replay SDK.
*
* @returns Promise that resolves to the current session ID number, or null if not initialized
*
* @example
* ```typescript
* const sessionId = await getSessionId();
* if (sessionId !== null) {
* console.log('Current session ID:', sessionId);
* }
* ```
*/
async function getSessionId() {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return null;
}
return await _nativeModule.NativeSessionReplay.getSessionId();
}
/**
* Interface for session replay properties that should be attached to Amplitude events.
* These properties help correlate events with session recordings.
*/
/**
* When you send events to Amplitude, call this function to get the most up-to-date session replay properties for the event.
* Collect Session Replay properties to send with other event properties.
*
* @returns Promise that resolves to an object containing session replay metadata
*
* @example
* ```typescript
* const sessionReplayProperties = await getSessionReplayProperties();
* analytics.track('Button Clicked', {
* buttonName: 'submit',
* ...sessionReplayProperties // Merge session replay properties
* });
* ```
*/
async function getSessionReplayProperties() {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return {};
}
const properties = await _nativeModule.NativeSessionReplay.getSessionReplayProperties();
return properties;
}
/**
* Flush any pending session replay data to the server.
* Forces immediate upload of recorded session data that may be buffered locally.
*
* @returns Promise that resolves when the flush operation is complete
*
* @example
* ```typescript
* // Flush data before app termination
* await flush();
* ```
*/
async function flush() {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return;
}
await _nativeModule.NativeSessionReplay.flush();
}
/**
* Start session replay recording.
* Begins capturing user interactions and screen content for replay.
* If autoStart is enabled in the configuration, this is called automatically during initialization.
*
* @returns Promise that resolves when recording starts
*
* @example
* ```typescript
* // Start recording manually if autoStart is false
* await start();
* ```
*/
async function start() {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return;
}
await _nativeModule.NativeSessionReplay.start();
}
/**
* Stop session replay recording.
* Ends the current recording session and processes any captured data.
*
* @returns Promise that resolves when recording stops
*
* @example
* ```typescript
* // Stop recording when user logs out or app goes to background
* await stop();
* ```
*/
async function stop() {
if (!isInitialized) {
logger.warn('SessionReplay is not initialized');
return;
}
await _nativeModule.NativeSessionReplay.stop();
}
function nativeConfig(config) {
return {
...config,
logLevel: config.logLevel,
maskLevel: config.maskLevel.toString()
};
}
async function privateInit(config, newLogger) {
logger = newLogger;
return init(config);
}
//# sourceMappingURL=session-replay.js.map