@zaplier/sdk 1.7.6 → 1.7.8

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/dist/index.d.ts CHANGED
@@ -766,6 +766,10 @@ declare class ZaplierSDK implements ZaplierSDK$1 {
766
766
  * Generate session ID using standardized format
767
767
  */
768
768
  private generateSessionId;
769
+ /**
770
+ * Update tracking engines with new session ID to maintain consistency
771
+ */
772
+ private updateTrackingEnginesSessionId;
769
773
  /**
770
774
  * Initialize Anti-Adblock Manager
771
775
  */
@@ -779,6 +783,10 @@ declare class ZaplierSDK implements ZaplierSDK$1 {
779
783
  * Check if running on localhost
780
784
  */
781
785
  private isLocalhost;
786
+ /**
787
+ * Extract UTM parameters from current URL
788
+ */
789
+ private extractUTMParameters;
782
790
  /**
783
791
  * Send event to backend
784
792
  */
package/dist/index.esm.js CHANGED
@@ -21513,6 +21513,52 @@ class ZaplierSDK {
21513
21513
  const tempVisitorId = this.visitorId || 'temp-' + Date.now().toString(36);
21514
21514
  return generateCurrentSessionId(tempVisitorId);
21515
21515
  }
21516
+ /**
21517
+ * Update tracking engines with new session ID to maintain consistency
21518
+ */
21519
+ updateTrackingEnginesSessionId(newSessionId) {
21520
+ try {
21521
+ // Update replay engine session ID if it exists and is recording
21522
+ if (this.replayEngine && this.replayEngine.isRecording()) {
21523
+ if (this.config.debug) {
21524
+ console.log("[Zaplier] Updating replay engine session ID:", newSessionId);
21525
+ }
21526
+ // Stop current recording and restart with new session ID
21527
+ this.replayEngine.stop();
21528
+ this.replayEngine = new SessionReplayEngine(newSessionId, this.backendVisitorId || 'unknown', {
21529
+ sampleRate: 1.0, // Keep recording active
21530
+ inactivityTimeout: 30000,
21531
+ pauseOnInactive: true,
21532
+ });
21533
+ this.replayEngine.setSDKInstance(this);
21534
+ this.replayEngine.start();
21535
+ }
21536
+ // Update heatmap engine session ID if it exists and is recording
21537
+ if (this.heatmapEngine && this.heatmapEngine.isRecording()) {
21538
+ if (this.config.debug) {
21539
+ console.log("[Zaplier] Updating heatmap engine session ID:", newSessionId);
21540
+ }
21541
+ // Stop current recording and restart with new session ID
21542
+ this.heatmapEngine.stop();
21543
+ this.heatmapEngine = new HeatmapEngine(newSessionId, {
21544
+ trackClicks: true,
21545
+ trackScrollDepth: true,
21546
+ trackRageClicks: true,
21547
+ trackMouseMoves: false,
21548
+ });
21549
+ if (this.antiAdblockManager) {
21550
+ this.heatmapEngine.setAntiAdblockManager(this.antiAdblockManager);
21551
+ }
21552
+ this.heatmapEngine.start();
21553
+ }
21554
+ if (this.config.debug) {
21555
+ console.log("[Zaplier] Tracking engines updated with session ID:", newSessionId);
21556
+ }
21557
+ }
21558
+ catch (error) {
21559
+ console.error("[Zaplier] Failed to update tracking engines session ID:", error);
21560
+ }
21561
+ }
21516
21562
  /**
21517
21563
  * Initialize Anti-Adblock Manager
21518
21564
  */
@@ -21681,6 +21727,70 @@ class ZaplierSDK {
21681
21727
  window.location.hostname.startsWith("10.") ||
21682
21728
  window.location.hostname.includes("local"));
21683
21729
  }
21730
+ /**
21731
+ * Extract UTM parameters from current URL
21732
+ */
21733
+ extractUTMParameters() {
21734
+ const utmParams = {};
21735
+ if (typeof window === "undefined" || !window.location) {
21736
+ return utmParams;
21737
+ }
21738
+ try {
21739
+ const urlParams = new URLSearchParams(window.location.search);
21740
+ // Extract UTM parameters (camelCase for consistency with backend)
21741
+ const utmSource = urlParams.get('utm_source');
21742
+ const utmMedium = urlParams.get('utm_medium');
21743
+ const utmCampaign = urlParams.get('utm_campaign');
21744
+ const utmContent = urlParams.get('utm_content');
21745
+ const utmTerm = urlParams.get('utm_term');
21746
+ // Extract click IDs
21747
+ const fbclid = urlParams.get('fbclid');
21748
+ const gclid = urlParams.get('gclid');
21749
+ const ttclid = urlParams.get('ttclid');
21750
+ // Extract ad platform parameters
21751
+ const creativeId = urlParams.get('creative_id');
21752
+ const adId = urlParams.get('ad_id');
21753
+ const adsetId = urlParams.get('adset_id');
21754
+ const campaignId = urlParams.get('campaign_id');
21755
+ const platform = urlParams.get('platform');
21756
+ // Add to result object (only non-null values)
21757
+ if (utmSource)
21758
+ utmParams.utmSource = utmSource;
21759
+ if (utmMedium)
21760
+ utmParams.utmMedium = utmMedium;
21761
+ if (utmCampaign)
21762
+ utmParams.utmCampaign = utmCampaign;
21763
+ if (utmContent)
21764
+ utmParams.utmContent = utmContent;
21765
+ if (utmTerm)
21766
+ utmParams.utmTerm = utmTerm;
21767
+ if (fbclid)
21768
+ utmParams.fbclid = fbclid;
21769
+ if (gclid)
21770
+ utmParams.gclid = gclid;
21771
+ if (ttclid)
21772
+ utmParams.ttclid = ttclid;
21773
+ if (creativeId)
21774
+ utmParams.creativeId = creativeId;
21775
+ if (adId)
21776
+ utmParams.adId = adId;
21777
+ if (adsetId)
21778
+ utmParams.adsetId = adsetId;
21779
+ if (campaignId)
21780
+ utmParams.campaignId = campaignId;
21781
+ if (platform)
21782
+ utmParams.platform = platform;
21783
+ if (this.config.debug && Object.keys(utmParams).length > 0) {
21784
+ console.log("[Zaplier] UTM parameters extracted from URL:", utmParams);
21785
+ }
21786
+ }
21787
+ catch (error) {
21788
+ if (this.config.debug) {
21789
+ console.warn("[Zaplier] Failed to extract UTM parameters:", error);
21790
+ }
21791
+ }
21792
+ return utmParams;
21793
+ }
21684
21794
  /**
21685
21795
  * Send event to backend
21686
21796
  */
@@ -21722,7 +21832,9 @@ class ZaplierSDK {
21722
21832
  }
21723
21833
  : undefined,
21724
21834
  fingerprintComponents: this.fingerprint?.components || {},
21725
- ...eventData,
21835
+ // CRITICAL: Auto-extract UTM parameters from URL, but allow eventData to override
21836
+ ...this.extractUTMParameters(), // Auto-extracted UTM parameters (background)
21837
+ ...eventData, // User-provided event data (can override UTM parameters)
21726
21838
  timestamp: new Date().toISOString(),
21727
21839
  url: typeof window !== "undefined" && window.location
21728
21840
  ? window.location.href
@@ -21767,12 +21879,25 @@ class ZaplierSDK {
21767
21879
  console.log("[Zaplier] Backend visitor ID received and processed:", response.visitorId);
21768
21880
  }
21769
21881
  }
21770
- if (response.sessionId) {
21882
+ // CRITICAL: Only update session ID if we don't have one yet or if it's temporary
21883
+ // Once a session ID is established and tracking engines are started, keep it consistent
21884
+ // to prevent replay/event session ID mismatches
21885
+ if (response.sessionId && (!this.sessionId || this.sessionId.includes('temp-'))) {
21886
+ const oldSessionId = this.sessionId;
21771
21887
  this.sessionId = response.sessionId;
21888
+ // Update tracking engines with new session ID if they exist
21889
+ if (oldSessionId !== this.sessionId && this.sessionId) {
21890
+ this.updateTrackingEnginesSessionId(this.sessionId);
21891
+ }
21772
21892
  }
21773
21893
  else if (this.backendVisitorId && (!this.sessionId || this.sessionId.includes('temp-'))) {
21774
21894
  // Generate new session ID now that we have the proper visitor ID
21895
+ const oldSessionId = this.sessionId;
21775
21896
  this.sessionId = generateCurrentSessionId(this.backendVisitorId);
21897
+ // Update tracking engines with new session ID if they exist
21898
+ if (oldSessionId !== this.sessionId && this.sessionId) {
21899
+ this.updateTrackingEnginesSessionId(this.sessionId);
21900
+ }
21776
21901
  }
21777
21902
  return response;
21778
21903
  }