UNPKG

ssr-web-avo-inspector

Version:

Avo Inspector for web with SSR and web workers support

124 lines (123 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AvoStreamId = void 0; var AvoGuid_1 = require("./AvoGuid"); var AvoInspector_1 = require("./AvoInspector"); var FOUR_HOURS_MS = 4 * 60 * 60 * 1000; var TWO_HOURS_MS = 2 * 60 * 60 * 1000; var AvoStreamId = /** @class */ (function () { function AvoStreamId() { } /** * Returns the anonymous ID using Model B (time-window client). * Resets when BOTH: * - age > 4 hours (time since creation) * - idle > 2 hours (time since last activity) * * Returns null if AvoInspector.avoStorage is not initialized (caller * should omit streamId so the event is treated as a "wild event"). */ AvoStreamId.getAnonymousId = function () { var now = Date.now(); // If we have a cached in-memory ID, check if it needs reset if (AvoStreamId._anonymousId !== null) { if (AvoStreamId.storageAvailable() && AvoStreamId.shouldReset(now)) { AvoStreamId.createNewId(now); } else if (AvoStreamId.storageAvailable()) { AvoStreamId.updateLastActivity(now); } return AvoStreamId._anonymousId; } // Storage not ready — return null so callers omit streamId (wild event) if (!AvoStreamId.storageAvailable()) { return null; } // Try to load from storage var storedId = null; try { storedId = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoStreamId.streamIdKey); } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } if (storedId !== null && storedId !== undefined) { // Check if the stored ID needs to be reset if (AvoStreamId.shouldReset(now)) { AvoStreamId.createNewId(now); } else { AvoStreamId._anonymousId = storedId; AvoStreamId.updateLastActivity(now); } } else { AvoStreamId.createNewId(now); } return AvoStreamId._anonymousId; }; AvoStreamId.storageAvailable = function () { return !!AvoInspector_1.AvoInspector.avoStorage && AvoInspector_1.AvoInspector.avoStorage.isInitialized(); }; AvoStreamId.shouldReset = function (now) { var createdAt = null; var lastActivity = null; try { createdAt = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoStreamId.createdAtKey); lastActivity = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoStreamId.lastActivityKey); } catch (e) { // If we can't read timestamps, don't reset return false; } if (createdAt === null || createdAt === undefined || lastActivity === null || lastActivity === undefined) { return false; } var age = now - createdAt; var idle = now - lastActivity; return age > FOUR_HOURS_MS && idle > TWO_HOURS_MS; }; AvoStreamId.createNewId = function (now) { AvoStreamId._anonymousId = AvoGuid_1.default.newGuid(); try { AvoInspector_1.AvoInspector.avoStorage.setItem(AvoStreamId.streamIdKey, AvoStreamId._anonymousId); AvoInspector_1.AvoInspector.avoStorage.setItem(AvoStreamId.createdAtKey, now); AvoInspector_1.AvoInspector.avoStorage.setItem(AvoStreamId.lastActivityKey, now); } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } }; AvoStreamId.updateLastActivity = function (now) { try { AvoInspector_1.AvoInspector.avoStorage.setItem(AvoStreamId.lastActivityKey, now); } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } }; Object.defineProperty(AvoStreamId, "streamIdKey", { get: function () { return "AvoInspectorStreamId"; }, enumerable: false, configurable: true }); Object.defineProperty(AvoStreamId, "createdAtKey", { get: function () { return "AvoInspectorStreamIdCreatedAt"; }, enumerable: false, configurable: true }); Object.defineProperty(AvoStreamId, "lastActivityKey", { get: function () { return "AvoInspectorStreamIdLastActivityAt"; }, enumerable: false, configurable: true }); AvoStreamId._anonymousId = null; return AvoStreamId; }()); exports.AvoStreamId = AvoStreamId;