@tracelog/lib 2.2.1 → 2.3.0

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.
@@ -62,6 +62,21 @@ type MetadataNestedObject = Record<string, MetadataNestedValue>;
62
62
  * ```
63
63
  */
64
64
  type MetadataType = MetadataPrimitive | string[] | MetadataNestedObject | MetadataNestedObject[];
65
+ /**
66
+ * Result returned by tracelog.init()
67
+ *
68
+ * Contains the session identifier generated during initialization.
69
+ * The sessionId is empty string in edge cases (SSR, disabled, race condition).
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const { sessionId } = await tracelog.init();
74
+ * console.log('Session started:', sessionId);
75
+ * ```
76
+ */
77
+ interface InitResult {
78
+ sessionId: string;
79
+ }
65
80
 
66
81
  /**
67
82
  * Element configuration for viewport tracking with optional identifiers
@@ -2287,7 +2302,7 @@ type CustomHeadersProvider = () => Record<string, string>;
2287
2302
  */
2288
2303
  interface TraceLogTestBridge {
2289
2304
  readonly initialized: boolean;
2290
- init(config?: Config): Promise<void>;
2305
+ init(config?: Config): Promise<InitResult>;
2291
2306
  destroy(force?: boolean): void;
2292
2307
  sendCustomEvent(name: string, data?: Record<string, unknown> | Record<string, unknown>[]): void;
2293
2308
  event(name: string, metadata?: Record<string, unknown> | Record<string, unknown>[]): void;
@@ -2474,7 +2489,7 @@ declare const DEFAULT_WEB_VITALS_MODE: WebVitalsMode;
2474
2489
  declare const getWebVitalsThresholds: (mode?: WebVitalsMode) => Record<WebVitalType, number>;
2475
2490
 
2476
2491
  declare const tracelog: {
2477
- init: (config?: Config) => Promise<void>;
2492
+ init: (config?: Config) => Promise<InitResult>;
2478
2493
  event: (name: string, metadata?: Record<string, MetadataType> | Record<string, MetadataType>[]) => void;
2479
2494
  on: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
2480
2495
  off: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
@@ -2483,10 +2498,11 @@ declare const tracelog: {
2483
2498
  setCustomHeaders: (provider: CustomHeadersProvider) => void;
2484
2499
  removeCustomHeaders: () => void;
2485
2500
  isInitialized: () => boolean;
2501
+ getSessionId: () => string | null;
2486
2502
  destroy: () => void;
2487
2503
  setQaMode: (enabled: boolean) => void;
2488
2504
  updateGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2489
2505
  mergeGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2490
2506
  };
2491
2507
 
2492
- export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, type CustomHeadersProvider, DEFAULT_SESSION_TIMEOUT, DEFAULT_WEB_VITALS_MODE, type DeviceInfo, DeviceType, type EmitterCallback, EmitterEvent, type EmitterMap, type ErrorData, ErrorType, type EventData, EventType, type EventTypeName, type EventsQueue, InitializationTimeoutError, IntegrationValidationError, MAX_ARRAY_LENGTH, MAX_CUSTOM_EVENT_ARRAY_SIZE, MAX_CUSTOM_EVENT_KEYS, MAX_CUSTOM_EVENT_NAME_LENGTH, MAX_CUSTOM_EVENT_STRING_SIZE, MAX_NESTED_OBJECT_KEYS, MAX_STRING_LENGTH, MAX_STRING_LENGTH_IN_ARRAY, type MetadataType, Mode, PII_PATTERNS, type PageViewData, PermanentError, type PersistedEventsQueue, type PrimaryScrollEvent, SamplingRateValidationError, type ScrollData, ScrollDirection, type SecondaryScrollEvent, type SessionEventCounts, SessionTimeoutValidationError, SpecialApiUrl, type State, type TraceLogTestBridge, TraceLogValidationError, type TransformerHook, type TransformerMap, type UTM, type ViewportConfig, type ViewportElement, type ViewportEventData, WEB_VITALS_GOOD_THRESHOLDS, WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS, WEB_VITALS_POOR_THRESHOLDS, type WebVitalType, type WebVitalsData, type WebVitalsMode, getWebVitalsThresholds, isPrimaryScrollEvent, isSecondaryScrollEvent, tracelog };
2508
+ export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, type CustomHeadersProvider, DEFAULT_SESSION_TIMEOUT, DEFAULT_WEB_VITALS_MODE, type DeviceInfo, DeviceType, type EmitterCallback, EmitterEvent, type EmitterMap, type ErrorData, ErrorType, type EventData, EventType, type EventTypeName, type EventsQueue, type InitResult, InitializationTimeoutError, IntegrationValidationError, MAX_ARRAY_LENGTH, MAX_CUSTOM_EVENT_ARRAY_SIZE, MAX_CUSTOM_EVENT_KEYS, MAX_CUSTOM_EVENT_NAME_LENGTH, MAX_CUSTOM_EVENT_STRING_SIZE, MAX_NESTED_OBJECT_KEYS, MAX_STRING_LENGTH, MAX_STRING_LENGTH_IN_ARRAY, type MetadataType, Mode, PII_PATTERNS, type PageViewData, PermanentError, type PersistedEventsQueue, type PrimaryScrollEvent, SamplingRateValidationError, type ScrollData, ScrollDirection, type SecondaryScrollEvent, type SessionEventCounts, SessionTimeoutValidationError, SpecialApiUrl, type State, type TraceLogTestBridge, TraceLogValidationError, type TransformerHook, type TransformerMap, type UTM, type ViewportConfig, type ViewportElement, type ViewportEventData, WEB_VITALS_GOOD_THRESHOLDS, WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS, WEB_VITALS_POOR_THRESHOLDS, type WebVitalType, type WebVitalsData, type WebVitalsMode, getWebVitalsThresholds, isPrimaryScrollEvent, isSecondaryScrollEvent, tracelog };
@@ -62,6 +62,21 @@ type MetadataNestedObject = Record<string, MetadataNestedValue>;
62
62
  * ```
63
63
  */
64
64
  type MetadataType = MetadataPrimitive | string[] | MetadataNestedObject | MetadataNestedObject[];
65
+ /**
66
+ * Result returned by tracelog.init()
67
+ *
68
+ * Contains the session identifier generated during initialization.
69
+ * The sessionId is empty string in edge cases (SSR, disabled, race condition).
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * const { sessionId } = await tracelog.init();
74
+ * console.log('Session started:', sessionId);
75
+ * ```
76
+ */
77
+ interface InitResult {
78
+ sessionId: string;
79
+ }
65
80
 
66
81
  /**
67
82
  * Element configuration for viewport tracking with optional identifiers
@@ -2287,7 +2302,7 @@ type CustomHeadersProvider = () => Record<string, string>;
2287
2302
  */
2288
2303
  interface TraceLogTestBridge {
2289
2304
  readonly initialized: boolean;
2290
- init(config?: Config): Promise<void>;
2305
+ init(config?: Config): Promise<InitResult>;
2291
2306
  destroy(force?: boolean): void;
2292
2307
  sendCustomEvent(name: string, data?: Record<string, unknown> | Record<string, unknown>[]): void;
2293
2308
  event(name: string, metadata?: Record<string, unknown> | Record<string, unknown>[]): void;
@@ -2474,7 +2489,7 @@ declare const DEFAULT_WEB_VITALS_MODE: WebVitalsMode;
2474
2489
  declare const getWebVitalsThresholds: (mode?: WebVitalsMode) => Record<WebVitalType, number>;
2475
2490
 
2476
2491
  declare const tracelog: {
2477
- init: (config?: Config) => Promise<void>;
2492
+ init: (config?: Config) => Promise<InitResult>;
2478
2493
  event: (name: string, metadata?: Record<string, MetadataType> | Record<string, MetadataType>[]) => void;
2479
2494
  on: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
2480
2495
  off: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
@@ -2483,10 +2498,11 @@ declare const tracelog: {
2483
2498
  setCustomHeaders: (provider: CustomHeadersProvider) => void;
2484
2499
  removeCustomHeaders: () => void;
2485
2500
  isInitialized: () => boolean;
2501
+ getSessionId: () => string | null;
2486
2502
  destroy: () => void;
2487
2503
  setQaMode: (enabled: boolean) => void;
2488
2504
  updateGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2489
2505
  mergeGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2490
2506
  };
2491
2507
 
2492
- export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, type CustomHeadersProvider, DEFAULT_SESSION_TIMEOUT, DEFAULT_WEB_VITALS_MODE, type DeviceInfo, DeviceType, type EmitterCallback, EmitterEvent, type EmitterMap, type ErrorData, ErrorType, type EventData, EventType, type EventTypeName, type EventsQueue, InitializationTimeoutError, IntegrationValidationError, MAX_ARRAY_LENGTH, MAX_CUSTOM_EVENT_ARRAY_SIZE, MAX_CUSTOM_EVENT_KEYS, MAX_CUSTOM_EVENT_NAME_LENGTH, MAX_CUSTOM_EVENT_STRING_SIZE, MAX_NESTED_OBJECT_KEYS, MAX_STRING_LENGTH, MAX_STRING_LENGTH_IN_ARRAY, type MetadataType, Mode, PII_PATTERNS, type PageViewData, PermanentError, type PersistedEventsQueue, type PrimaryScrollEvent, SamplingRateValidationError, type ScrollData, ScrollDirection, type SecondaryScrollEvent, type SessionEventCounts, SessionTimeoutValidationError, SpecialApiUrl, type State, type TraceLogTestBridge, TraceLogValidationError, type TransformerHook, type TransformerMap, type UTM, type ViewportConfig, type ViewportElement, type ViewportEventData, WEB_VITALS_GOOD_THRESHOLDS, WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS, WEB_VITALS_POOR_THRESHOLDS, type WebVitalType, type WebVitalsData, type WebVitalsMode, getWebVitalsThresholds, isPrimaryScrollEvent, isSecondaryScrollEvent, tracelog };
2508
+ export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, type CustomHeadersProvider, DEFAULT_SESSION_TIMEOUT, DEFAULT_WEB_VITALS_MODE, type DeviceInfo, DeviceType, type EmitterCallback, EmitterEvent, type EmitterMap, type ErrorData, ErrorType, type EventData, EventType, type EventTypeName, type EventsQueue, type InitResult, InitializationTimeoutError, IntegrationValidationError, MAX_ARRAY_LENGTH, MAX_CUSTOM_EVENT_ARRAY_SIZE, MAX_CUSTOM_EVENT_KEYS, MAX_CUSTOM_EVENT_NAME_LENGTH, MAX_CUSTOM_EVENT_STRING_SIZE, MAX_NESTED_OBJECT_KEYS, MAX_STRING_LENGTH, MAX_STRING_LENGTH_IN_ARRAY, type MetadataType, Mode, PII_PATTERNS, type PageViewData, PermanentError, type PersistedEventsQueue, type PrimaryScrollEvent, SamplingRateValidationError, type ScrollData, ScrollDirection, type SecondaryScrollEvent, type SessionEventCounts, SessionTimeoutValidationError, SpecialApiUrl, type State, type TraceLogTestBridge, TraceLogValidationError, type TransformerHook, type TransformerMap, type UTM, type ViewportConfig, type ViewportElement, type ViewportEventData, WEB_VITALS_GOOD_THRESHOLDS, WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS, WEB_VITALS_POOR_THRESHOLDS, type WebVitalType, type WebVitalsData, type WebVitalsMode, getWebVitalsThresholds, isPrimaryScrollEvent, isSecondaryScrollEvent, tracelog };
@@ -555,7 +555,7 @@ var LONG_TASK_THROTTLE_MS = 1e3;
555
555
  var MAX_NAVIGATION_HISTORY = 50;
556
556
 
557
557
  // package.json
558
- var version = "2.2.0";
558
+ var version = "2.2.1";
559
559
 
560
560
  // src/constants/version.constants.ts
561
561
  var LIB_VERSION = version;
@@ -846,7 +846,6 @@ var sanitizeString = (value) => {
846
846
  }
847
847
  });
848
848
  }
849
- sanitized = sanitized.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#x27;").replaceAll("/", "&#x2F;");
850
849
  const result = sanitized.trim();
851
850
  return result;
852
851
  };
@@ -6120,7 +6119,7 @@ var App = class extends StateManager {
6120
6119
  */
6121
6120
  async init(config = {}) {
6122
6121
  if (this.isInitialized) {
6123
- return;
6122
+ return { sessionId: this.get("sessionId") ?? "" };
6124
6123
  }
6125
6124
  this.managers.storage = new StorageManager();
6126
6125
  try {
@@ -6138,6 +6137,7 @@ var App = class extends StateManager {
6138
6137
  log("warn", "Failed to recover persisted events", { error });
6139
6138
  });
6140
6139
  this.isInitialized = true;
6140
+ return { sessionId: this.get("sessionId") ?? "" };
6141
6141
  } catch (error) {
6142
6142
  this.destroy(true);
6143
6143
  const errorMessage = error instanceof Error ? error.message : String(error);
@@ -6297,6 +6297,15 @@ var App = class extends StateManager {
6297
6297
  getEventManager() {
6298
6298
  return this.managers.event;
6299
6299
  }
6300
+ /**
6301
+ * Returns the current session ID.
6302
+ *
6303
+ * @returns The session ID string, or null if not yet initialized
6304
+ * @internal Used by api.getSessionId()
6305
+ */
6306
+ getSessionId() {
6307
+ return this.get("sessionId");
6308
+ }
6300
6309
  /**
6301
6310
  * Validates metadata object structure and values.
6302
6311
  *
@@ -6409,17 +6418,17 @@ var isInitializing = false;
6409
6418
  var isDestroying = false;
6410
6419
  var init = async (config) => {
6411
6420
  if (typeof window === "undefined" || typeof document === "undefined") {
6412
- return;
6421
+ return { sessionId: "" };
6413
6422
  }
6414
6423
  isDestroying = false;
6415
6424
  if (window.__traceLogDisabled === true) {
6416
- return;
6425
+ return { sessionId: "" };
6417
6426
  }
6418
6427
  if (app) {
6419
- return;
6428
+ return { sessionId: app.getSessionId() ?? "" };
6420
6429
  }
6421
6430
  if (isInitializing) {
6422
- return;
6431
+ return { sessionId: "" };
6423
6432
  }
6424
6433
  isInitializing = true;
6425
6434
  try {
@@ -6448,8 +6457,9 @@ var init = async (config) => {
6448
6457
  reject(new Error(`[TraceLog] Initialization timeout after ${INITIALIZATION_TIMEOUT_MS}ms`));
6449
6458
  }, INITIALIZATION_TIMEOUT_MS);
6450
6459
  });
6451
- await Promise.race([initPromise, timeoutPromise]);
6460
+ const result = await Promise.race([initPromise, timeoutPromise]);
6452
6461
  app = instance;
6462
+ return result;
6453
6463
  } catch (error) {
6454
6464
  try {
6455
6465
  instance.destroy(true);
@@ -6575,6 +6585,15 @@ var isInitialized = () => {
6575
6585
  }
6576
6586
  return app !== null;
6577
6587
  };
6588
+ var getSessionId = () => {
6589
+ if (typeof window === "undefined" || typeof document === "undefined") {
6590
+ return null;
6591
+ }
6592
+ if (!app) {
6593
+ return null;
6594
+ }
6595
+ return app.getSessionId();
6596
+ };
6578
6597
  var destroy = () => {
6579
6598
  if (typeof window === "undefined" || typeof document === "undefined") {
6580
6599
  return;
@@ -6648,6 +6667,7 @@ var tracelog = {
6648
6667
  setCustomHeaders,
6649
6668
  removeCustomHeaders,
6650
6669
  isInitialized,
6670
+ getSessionId,
6651
6671
  destroy,
6652
6672
  setQaMode: setQaMode2,
6653
6673
  updateGlobalMetadata,