@tracelog/lib 2.1.2 → 2.2.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.
package/README.md CHANGED
@@ -81,24 +81,29 @@ tracelog.setTransformer('beforeSend', (event) => {
81
81
  return { ...event, custom_metadata: { app: 'v1' } };
82
82
  });
83
83
 
84
- // 4. Initialize FOURTH (starts tracking immediately)
84
+ // 4. Configure custom headers FOURTH (if using custom backend with auth)
85
+ tracelog.setCustomHeaders(() => ({
86
+ 'Authorization': `Bearer ${getAuthToken()}`
87
+ }));
88
+
89
+ // 5. Initialize FIFTH (starts tracking immediately)
85
90
  await tracelog.init({
86
91
  integrations: {
87
92
  custom: { collectApiUrl: 'https://api.example.com' }
88
93
  }
89
94
  });
90
95
 
91
- // 5. Track custom events (after init)
96
+ // 6. Track custom events (after init)
92
97
  tracelog.event('button_clicked', {
93
98
  buttonId: 'signup-cta',
94
99
  source: 'homepage'
95
100
  });
96
101
 
97
- // 6. Cleanup (on consent revoke or app unmount)
102
+ // 7. Cleanup (on consent revoke or app unmount)
98
103
  tracelog.destroy();
99
104
  ```
100
105
 
101
- **Why this order?** You must obtain user consent before initializing. Events like `SESSION_START` and `PAGE_VIEW` fire during initialization. Registering listeners and transformers before init ensures you don't miss them.
106
+ **Why this order?** You must obtain user consent before initializing. Events like `SESSION_START` and `PAGE_VIEW` fire during initialization. Registering listeners, transformers, and custom headers before init ensures you capture, transform, and send these initial events with proper authentication.
102
107
 
103
108
  **That's it!** TraceLog now automatically tracks:
104
109
  - Page views & navigation (including SPA routes)
@@ -122,6 +127,8 @@ tracelog.destroy();
122
127
  | `off(event, callback)` | Unsubscribe from events |
123
128
  | `setTransformer(hook, fn)` | Transform events before sending (see [Transformers](#transformers)) |
124
129
  | `removeTransformer(hook)` | Remove a previously set transformer |
130
+ | `setCustomHeaders(provider)` | Add custom HTTP headers to requests (see [Custom Headers](#custom-headers)) |
131
+ | `removeCustomHeaders()` | Remove custom headers provider |
125
132
  | `isInitialized()` | Check initialization status |
126
133
  | `setQaMode(enabled)` | Enable/disable QA mode (console logging) |
127
134
  | `destroy()` | Stop tracking and cleanup |
@@ -592,6 +599,92 @@ tracelog.setTransformer('beforeSend', (data) => {
592
599
 
593
600
  ---
594
601
 
602
+ ## Custom Headers
603
+
604
+ Add custom HTTP headers to requests sent to custom backends. Useful for authentication, tenant identification, or API versioning.
605
+
606
+ **Important**: Custom headers **only apply to custom backend integrations**. TraceLog SaaS always receives requests without custom headers.
607
+
608
+ ### Static Headers (Config)
609
+
610
+ Set fixed headers in configuration:
611
+
612
+ ```typescript
613
+ await tracelog.init({
614
+ integrations: {
615
+ custom: {
616
+ collectApiUrl: 'https://api.example.com/collect',
617
+ headers: {
618
+ 'X-Tenant-Id': 'tenant-123',
619
+ 'X-Brand': 'my-brand',
620
+ 'X-API-Version': '2.0'
621
+ }
622
+ }
623
+ }
624
+ });
625
+ ```
626
+
627
+ ### Dynamic Headers (Provider)
628
+
629
+ Set headers dynamically at runtime (e.g., auth tokens that expire):
630
+
631
+ ```typescript
632
+ // Set before or after init
633
+ tracelog.setCustomHeaders(() => ({
634
+ 'Authorization': `Bearer ${getAuthToken()}`,
635
+ 'X-Request-ID': crypto.randomUUID()
636
+ }));
637
+
638
+ await tracelog.init({
639
+ integrations: {
640
+ custom: { collectApiUrl: 'https://api.example.com/collect' }
641
+ }
642
+ });
643
+ ```
644
+
645
+ ### Static + Dynamic Headers
646
+
647
+ Combine both approaches. Dynamic headers override static on key collision:
648
+
649
+ ```typescript
650
+ await tracelog.init({
651
+ integrations: {
652
+ custom: {
653
+ collectApiUrl: 'https://api.example.com/collect',
654
+ headers: {
655
+ 'X-Brand': 'static-brand', // Static
656
+ 'X-Tenant-Id': 'tenant-123' // Static
657
+ }
658
+ }
659
+ }
660
+ });
661
+
662
+ // Dynamic provider overrides 'X-Brand'
663
+ tracelog.setCustomHeaders(() => ({
664
+ 'X-Brand': 'dynamic-brand', // Overrides static
665
+ 'Authorization': 'Bearer token' // New header
666
+ }));
667
+
668
+ // Result: { 'X-Tenant-Id': 'tenant-123', 'X-Brand': 'dynamic-brand', 'Authorization': 'Bearer token' }
669
+ ```
670
+
671
+ ### Removing Headers
672
+
673
+ ```typescript
674
+ // Remove dynamic provider (static headers from config remain)
675
+ tracelog.removeCustomHeaders();
676
+ ```
677
+
678
+ ### sendBeacon Limitation
679
+
680
+ ⚠️ Custom headers are **NOT applied** to `sendBeacon()` requests (page unload). The browser API doesn't support custom headers. For scenarios requiring headers on all requests:
681
+ - Ensure async sends complete before page unload
682
+ - Use short-lived tokens that don't require refresh per request
683
+
684
+ **→ [Custom Headers API Reference](./API_REFERENCE.md#setcustomheadersprovider-customheadersprovider-void)**
685
+
686
+ ---
687
+
595
688
  ## Integration Modes
596
689
 
597
690
  TraceLog supports multiple integration modes. Choose what fits your needs: