@tracelog/lib 2.1.1 → 2.1.2-rc.78.4

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.

Potentially problematic release.


This version of @tracelog/lib might be problematic. Click here for more details.

@@ -443,6 +443,12 @@ interface Config {
443
443
  collectApiUrl: string;
444
444
  /** Allow HTTP URLs (not recommended for production). @default false */
445
445
  allowHttp?: boolean;
446
+ /**
447
+ * Static HTTP headers to include in every request.
448
+ * For dynamic headers, use `setCustomHeaders()` instead.
449
+ * @example { 'X-Brand': 'my-brand', 'X-Tenant-Id': 'tenant-123' }
450
+ */
451
+ headers?: Record<string, string>;
446
452
  };
447
453
  };
448
454
  }
@@ -688,6 +694,8 @@ interface State {
688
694
  hasStartSession: boolean;
689
695
  suppressNextScroll: boolean;
690
696
  scrollEventCount?: number;
697
+ sessionReferrer?: string;
698
+ sessionUtm?: UTM;
691
699
  }
692
700
 
693
701
  /**
@@ -1236,8 +1244,10 @@ declare class EventManager extends StateManager {
1236
1244
  * @param storeManager - Storage manager for persistence
1237
1245
  * @param emitter - Optional event emitter for local event consumption
1238
1246
  * @param transformers - Optional event transformation hooks
1247
+ * @param staticHeaders - Optional static HTTP headers for custom backend (from config)
1248
+ * @param customHeadersProvider - Optional callback for dynamic headers
1239
1249
  */
1240
- constructor(storeManager: StorageManager, emitter?: Emitter | null, transformers?: TransformerMap);
1250
+ constructor(storeManager: StorageManager, emitter?: Emitter | null, transformers?: TransformerMap, staticHeaders?: Record<string, string>, customHeadersProvider?: CustomHeadersProvider);
1241
1251
  /**
1242
1252
  * Recovers persisted events from localStorage after a crash or page reload.
1243
1253
  *
@@ -1428,6 +1438,17 @@ declare class EventManager extends StateManager {
1428
1438
  * @see src/managers/README.md (lines 5-75) for flush details
1429
1439
  */
1430
1440
  flushImmediatelySync(): boolean;
1441
+ /**
1442
+ * Sets the custom headers provider callback for the custom integration.
1443
+ * Only affects requests to custom backend (not TraceLog SaaS).
1444
+ *
1445
+ * @param provider - Callback function that returns custom headers
1446
+ */
1447
+ setCustomHeadersProvider(provider: CustomHeadersProvider): void;
1448
+ /**
1449
+ * Removes the custom headers provider callback from the custom integration.
1450
+ */
1451
+ removeCustomHeadersProvider(): void;
1431
1452
  /**
1432
1453
  * Returns the current number of events in the main queue.
1433
1454
  *
@@ -1613,58 +1634,6 @@ declare class EventManager extends StateManager {
1613
1634
  * @internal
1614
1635
  */
1615
1636
  private cleanupExpiredSessionCounts;
1616
- /**
1617
- * Returns the referrer if it's external, or 'Direct' if internal/empty.
1618
- *
1619
- * **Purpose**: Filter out internal referrers (same domain) to ensure
1620
- * accurate traffic source attribution. Internal referrers occur when:
1621
- * - Session expires and user navigates within the same site
1622
- * - User opens new tab from an internal link
1623
- * - Page refresh after session timeout
1624
- *
1625
- * **Logic**:
1626
- * - Empty referrer → 'Direct'
1627
- * - Referrer from same domain or subdomain → 'Direct' (internal navigation)
1628
- * - External referrer → Returns original referrer
1629
- *
1630
- * **Subdomain Detection**:
1631
- * - `www.example.com` → `example.com` ✓ (internal)
1632
- * - `blog.example.com` → `example.com` ✓ (internal)
1633
- * - `example.com` → `www.example.com` ✓ (internal)
1634
- *
1635
- * @returns External referrer URL or 'Direct'
1636
- *
1637
- * @internal
1638
- */
1639
- private getExternalReferrer;
1640
- /**
1641
- * Checks if two hostnames belong to the same domain (including subdomains).
1642
- * Extracts root domain and compares to handle cross-subdomain navigation.
1643
- *
1644
- * @example
1645
- * isSameDomain('www.example.com', 'example.com') // true
1646
- * isSameDomain('app.example.com', 'www.example.com') // true
1647
- * isSameDomain('example.co.uk', 'app.example.co.uk') // true
1648
- *
1649
- * @param hostname1 - First hostname (e.g., 'www.example.com')
1650
- * @param hostname2 - Second hostname (e.g., 'app.example.com')
1651
- * @returns true if same root domain
1652
- *
1653
- * @internal
1654
- */
1655
- private isSameDomain;
1656
- /**
1657
- * Extracts the root (registrable) domain from a hostname.
1658
- * Handles both standard TLDs (.com, .org) and compound TLDs (.co.uk, .com.br).
1659
- *
1660
- * @example
1661
- * getRootDomain('www.example.com') // 'example.com'
1662
- * getRootDomain('app.blog.example.com') // 'example.com'
1663
- * getRootDomain('shop.example.co.uk') // 'example.co.uk'
1664
- *
1665
- * @internal
1666
- */
1667
- private getRootDomain;
1668
1637
  /**
1669
1638
  * Persists current session event counts to localStorage (debounced).
1670
1639
  *
@@ -2286,6 +2255,26 @@ interface TransformerMap {
2286
2255
  beforeSend?: BeforeSendTransformer;
2287
2256
  beforeBatch?: BeforeBatchTransformer;
2288
2257
  }
2258
+ /**
2259
+ * Callback function for providing dynamic HTTP headers.
2260
+ *
2261
+ * Called synchronously before each fetch request to custom backends.
2262
+ * Return empty object {} to send no custom headers.
2263
+ *
2264
+ * **Note**: Only applies to `custom` integration (not TraceLog SaaS).
2265
+ * Headers are NOT applied to sendBeacon requests (page unload).
2266
+ *
2267
+ * @returns Record of header names to values
2268
+ *
2269
+ * @example
2270
+ * ```typescript
2271
+ * tracelog.setCustomHeaders(() => ({
2272
+ * 'Authorization': `Bearer ${getAuthToken()}`,
2273
+ * 'X-Request-ID': crypto.randomUUID()
2274
+ * }));
2275
+ * ```
2276
+ */
2277
+ type CustomHeadersProvider = () => Record<string, string>;
2289
2278
 
2290
2279
  /**
2291
2280
  * Testing bridge interface for E2E and integration tests
@@ -2317,6 +2306,8 @@ interface TraceLogTestBridge {
2317
2306
  setTransformer(hook: 'beforeBatch', fn: BeforeBatchTransformer): void;
2318
2307
  setTransformer(hook: TransformerHook, fn: BeforeSendTransformer | BeforeBatchTransformer): void;
2319
2308
  removeTransformer(hook: TransformerHook): void;
2309
+ setCustomHeaders(provider: CustomHeadersProvider): void;
2310
+ removeCustomHeaders(): void;
2320
2311
  getEventManager(): EventManager | undefined;
2321
2312
  getStorageManager(): StorageManager | null;
2322
2313
  getPerformanceHandler(): PerformanceHandler | null;
@@ -2490,6 +2481,8 @@ declare const tracelog: {
2490
2481
  off: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
2491
2482
  setTransformer: typeof setTransformer;
2492
2483
  removeTransformer: (hook: TransformerHook) => void;
2484
+ setCustomHeaders: (provider: CustomHeadersProvider) => void;
2485
+ removeCustomHeaders: () => void;
2493
2486
  isInitialized: () => boolean;
2494
2487
  destroy: () => void;
2495
2488
  setQaMode: (enabled: boolean) => void;
@@ -2497,4 +2490,4 @@ declare const tracelog: {
2497
2490
  mergeGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2498
2491
  };
2499
2492
 
2500
- export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, 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_METADATA_NESTING_DEPTH, 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 };
2493
+ 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_METADATA_NESTING_DEPTH, 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 };
@@ -443,6 +443,12 @@ interface Config {
443
443
  collectApiUrl: string;
444
444
  /** Allow HTTP URLs (not recommended for production). @default false */
445
445
  allowHttp?: boolean;
446
+ /**
447
+ * Static HTTP headers to include in every request.
448
+ * For dynamic headers, use `setCustomHeaders()` instead.
449
+ * @example { 'X-Brand': 'my-brand', 'X-Tenant-Id': 'tenant-123' }
450
+ */
451
+ headers?: Record<string, string>;
446
452
  };
447
453
  };
448
454
  }
@@ -688,6 +694,8 @@ interface State {
688
694
  hasStartSession: boolean;
689
695
  suppressNextScroll: boolean;
690
696
  scrollEventCount?: number;
697
+ sessionReferrer?: string;
698
+ sessionUtm?: UTM;
691
699
  }
692
700
 
693
701
  /**
@@ -1236,8 +1244,10 @@ declare class EventManager extends StateManager {
1236
1244
  * @param storeManager - Storage manager for persistence
1237
1245
  * @param emitter - Optional event emitter for local event consumption
1238
1246
  * @param transformers - Optional event transformation hooks
1247
+ * @param staticHeaders - Optional static HTTP headers for custom backend (from config)
1248
+ * @param customHeadersProvider - Optional callback for dynamic headers
1239
1249
  */
1240
- constructor(storeManager: StorageManager, emitter?: Emitter | null, transformers?: TransformerMap);
1250
+ constructor(storeManager: StorageManager, emitter?: Emitter | null, transformers?: TransformerMap, staticHeaders?: Record<string, string>, customHeadersProvider?: CustomHeadersProvider);
1241
1251
  /**
1242
1252
  * Recovers persisted events from localStorage after a crash or page reload.
1243
1253
  *
@@ -1428,6 +1438,17 @@ declare class EventManager extends StateManager {
1428
1438
  * @see src/managers/README.md (lines 5-75) for flush details
1429
1439
  */
1430
1440
  flushImmediatelySync(): boolean;
1441
+ /**
1442
+ * Sets the custom headers provider callback for the custom integration.
1443
+ * Only affects requests to custom backend (not TraceLog SaaS).
1444
+ *
1445
+ * @param provider - Callback function that returns custom headers
1446
+ */
1447
+ setCustomHeadersProvider(provider: CustomHeadersProvider): void;
1448
+ /**
1449
+ * Removes the custom headers provider callback from the custom integration.
1450
+ */
1451
+ removeCustomHeadersProvider(): void;
1431
1452
  /**
1432
1453
  * Returns the current number of events in the main queue.
1433
1454
  *
@@ -1613,58 +1634,6 @@ declare class EventManager extends StateManager {
1613
1634
  * @internal
1614
1635
  */
1615
1636
  private cleanupExpiredSessionCounts;
1616
- /**
1617
- * Returns the referrer if it's external, or 'Direct' if internal/empty.
1618
- *
1619
- * **Purpose**: Filter out internal referrers (same domain) to ensure
1620
- * accurate traffic source attribution. Internal referrers occur when:
1621
- * - Session expires and user navigates within the same site
1622
- * - User opens new tab from an internal link
1623
- * - Page refresh after session timeout
1624
- *
1625
- * **Logic**:
1626
- * - Empty referrer → 'Direct'
1627
- * - Referrer from same domain or subdomain → 'Direct' (internal navigation)
1628
- * - External referrer → Returns original referrer
1629
- *
1630
- * **Subdomain Detection**:
1631
- * - `www.example.com` → `example.com` ✓ (internal)
1632
- * - `blog.example.com` → `example.com` ✓ (internal)
1633
- * - `example.com` → `www.example.com` ✓ (internal)
1634
- *
1635
- * @returns External referrer URL or 'Direct'
1636
- *
1637
- * @internal
1638
- */
1639
- private getExternalReferrer;
1640
- /**
1641
- * Checks if two hostnames belong to the same domain (including subdomains).
1642
- * Extracts root domain and compares to handle cross-subdomain navigation.
1643
- *
1644
- * @example
1645
- * isSameDomain('www.example.com', 'example.com') // true
1646
- * isSameDomain('app.example.com', 'www.example.com') // true
1647
- * isSameDomain('example.co.uk', 'app.example.co.uk') // true
1648
- *
1649
- * @param hostname1 - First hostname (e.g., 'www.example.com')
1650
- * @param hostname2 - Second hostname (e.g., 'app.example.com')
1651
- * @returns true if same root domain
1652
- *
1653
- * @internal
1654
- */
1655
- private isSameDomain;
1656
- /**
1657
- * Extracts the root (registrable) domain from a hostname.
1658
- * Handles both standard TLDs (.com, .org) and compound TLDs (.co.uk, .com.br).
1659
- *
1660
- * @example
1661
- * getRootDomain('www.example.com') // 'example.com'
1662
- * getRootDomain('app.blog.example.com') // 'example.com'
1663
- * getRootDomain('shop.example.co.uk') // 'example.co.uk'
1664
- *
1665
- * @internal
1666
- */
1667
- private getRootDomain;
1668
1637
  /**
1669
1638
  * Persists current session event counts to localStorage (debounced).
1670
1639
  *
@@ -2286,6 +2255,26 @@ interface TransformerMap {
2286
2255
  beforeSend?: BeforeSendTransformer;
2287
2256
  beforeBatch?: BeforeBatchTransformer;
2288
2257
  }
2258
+ /**
2259
+ * Callback function for providing dynamic HTTP headers.
2260
+ *
2261
+ * Called synchronously before each fetch request to custom backends.
2262
+ * Return empty object {} to send no custom headers.
2263
+ *
2264
+ * **Note**: Only applies to `custom` integration (not TraceLog SaaS).
2265
+ * Headers are NOT applied to sendBeacon requests (page unload).
2266
+ *
2267
+ * @returns Record of header names to values
2268
+ *
2269
+ * @example
2270
+ * ```typescript
2271
+ * tracelog.setCustomHeaders(() => ({
2272
+ * 'Authorization': `Bearer ${getAuthToken()}`,
2273
+ * 'X-Request-ID': crypto.randomUUID()
2274
+ * }));
2275
+ * ```
2276
+ */
2277
+ type CustomHeadersProvider = () => Record<string, string>;
2289
2278
 
2290
2279
  /**
2291
2280
  * Testing bridge interface for E2E and integration tests
@@ -2317,6 +2306,8 @@ interface TraceLogTestBridge {
2317
2306
  setTransformer(hook: 'beforeBatch', fn: BeforeBatchTransformer): void;
2318
2307
  setTransformer(hook: TransformerHook, fn: BeforeSendTransformer | BeforeBatchTransformer): void;
2319
2308
  removeTransformer(hook: TransformerHook): void;
2309
+ setCustomHeaders(provider: CustomHeadersProvider): void;
2310
+ removeCustomHeaders(): void;
2320
2311
  getEventManager(): EventManager | undefined;
2321
2312
  getStorageManager(): StorageManager | null;
2322
2313
  getPerformanceHandler(): PerformanceHandler | null;
@@ -2490,6 +2481,8 @@ declare const tracelog: {
2490
2481
  off: <K extends keyof EmitterMap>(event: K, callback: EmitterCallback<EmitterMap[K]>) => void;
2491
2482
  setTransformer: typeof setTransformer;
2492
2483
  removeTransformer: (hook: TransformerHook) => void;
2484
+ setCustomHeaders: (provider: CustomHeadersProvider) => void;
2485
+ removeCustomHeaders: () => void;
2493
2486
  isInitialized: () => boolean;
2494
2487
  destroy: () => void;
2495
2488
  setQaMode: (enabled: boolean) => void;
@@ -2497,4 +2490,4 @@ declare const tracelog: {
2497
2490
  mergeGlobalMetadata: (metadata: Record<string, MetadataType>) => void;
2498
2491
  };
2499
2492
 
2500
- export { AppConfigValidationError, type BeforeBatchTransformer, type BeforeSendTransformer, type ClickCoordinates, type ClickData, type ClickTrackingElementData, type Config, type CustomEventData, 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_METADATA_NESTING_DEPTH, 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 };
2493
+ 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_METADATA_NESTING_DEPTH, 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 };