@datalyr/web 1.6.2 → 1.6.3

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @datalyr/web v1.6.2
2
+ * @datalyr/web v1.6.3
3
3
  * Datalyr Web SDK - Modern attribution tracking for web applications
4
4
  * (c) 2026 Datalyr Inc.
5
5
  * Released under the MIT License
@@ -582,6 +582,37 @@ const cookies = new CookieStorage();
582
582
  /**
583
583
  * Utility Functions
584
584
  */
585
+ /**
586
+ * SHA-256 hex digest aligned with the worker's CAPI hashing (cloudflare/
587
+ * postback/core/utils.js `sha256()`). Always lowercases + trims the input
588
+ * before hashing — Meta's matching requires byte-for-byte identical hashes
589
+ * on the Pixel side and the CAPI server side, and the worker uniformly
590
+ * normalizes everything it hashes (em, ph, fn, ln, external_id, etc).
591
+ *
592
+ * Pixel-side hashes that don't match CAPI hashes silently fail to dedupe,
593
+ * which is why this helper does the normalization for callers — easy to
594
+ * forget, hard to debug after release. Returns null when Web Crypto isn't
595
+ * available (very old browsers, non-secure contexts) so callers can
596
+ * gracefully skip advanced matching rather than throwing.
597
+ */
598
+ function sha256Hex(input) {
599
+ return __awaiter(this, void 0, void 0, function* () {
600
+ if (typeof crypto === 'undefined' || !crypto.subtle)
601
+ return null;
602
+ if (!input)
603
+ return null;
604
+ try {
605
+ const bytes = new TextEncoder().encode(input.toLowerCase().trim());
606
+ const digest = yield crypto.subtle.digest('SHA-256', bytes);
607
+ return Array.from(new Uint8Array(digest))
608
+ .map((b) => b.toString(16).padStart(2, '0'))
609
+ .join('');
610
+ }
611
+ catch (_a) {
612
+ return null;
613
+ }
614
+ });
615
+ }
585
616
  /**
586
617
  * Generate UUID v4
587
618
  */
@@ -2311,6 +2342,7 @@ class ContainerManager {
2311
2342
  // Container scripts use the same endpoint as tracking (ingest)
2312
2343
  this.endpoint = options.endpoint || 'https://ingest.datalyr.com';
2313
2344
  this.debug = options.debug || false;
2345
+ this.getIdentity = options.getIdentity;
2314
2346
  // Load session scripts from storage
2315
2347
  const sessionScripts = storage.get('dl_session_scripts', []);
2316
2348
  this.sessionLoadedScripts = new Set(sessionScripts);
@@ -2354,9 +2386,11 @@ class ContainerManager {
2354
2386
  // Store scripts and pixels
2355
2387
  this.scripts = data.scripts || [];
2356
2388
  this.pixels = data.pixels || null;
2357
- // Initialize pixels if configured
2389
+ // Initialize pixels if configured. Awaited so advanced-matching hashes
2390
+ // are resolved before the first dl.track() flushes through trackToPixels
2391
+ // (otherwise fbq('init') would lag fbq('track') in fast-path tracks).
2358
2392
  if (this.pixels) {
2359
- this.initializePixels();
2393
+ yield this.initializePixels();
2360
2394
  }
2361
2395
  // Load scripts based on trigger
2362
2396
  this.loadScriptsByTrigger('page_load');
@@ -2648,57 +2682,92 @@ class ContainerManager {
2648
2682
  * Initialize third-party pixels (Meta, Google, TikTok)
2649
2683
  */
2650
2684
  initializePixels() {
2651
- var _a, _b, _c;
2652
- if (!this.pixels)
2653
- return;
2654
- // Initialize Meta Pixel
2655
- if (((_a = this.pixels.meta) === null || _a === void 0 ? void 0 : _a.enabled) && this.pixels.meta.pixel_id) {
2656
- this.initializeMetaPixel(this.pixels.meta);
2657
- }
2658
- // Initialize Google Tag
2659
- if (((_b = this.pixels.google) === null || _b === void 0 ? void 0 : _b.enabled) && this.pixels.google.tag_id) {
2660
- this.initializeGoogleTag(this.pixels.google);
2661
- }
2662
- // Initialize TikTok Pixel
2663
- if (((_c = this.pixels.tiktok) === null || _c === void 0 ? void 0 : _c.enabled) && this.pixels.tiktok.pixel_id) {
2664
- this.initializeTikTokPixel(this.pixels.tiktok);
2665
- }
2685
+ return __awaiter(this, void 0, void 0, function* () {
2686
+ var _a, _b, _c;
2687
+ if (!this.pixels)
2688
+ return;
2689
+ // Initialize Meta Pixel
2690
+ if (((_a = this.pixels.meta) === null || _a === void 0 ? void 0 : _a.enabled) && this.pixels.meta.pixel_id) {
2691
+ yield this.initializeMetaPixel(this.pixels.meta);
2692
+ }
2693
+ // Initialize Google Tag
2694
+ if (((_b = this.pixels.google) === null || _b === void 0 ? void 0 : _b.enabled) && this.pixels.google.tag_id) {
2695
+ this.initializeGoogleTag(this.pixels.google);
2696
+ }
2697
+ // Initialize TikTok Pixel
2698
+ if (((_c = this.pixels.tiktok) === null || _c === void 0 ? void 0 : _c.enabled) && this.pixels.tiktok.pixel_id) {
2699
+ this.initializeTikTokPixel(this.pixels.tiktok);
2700
+ }
2701
+ });
2666
2702
  }
2667
2703
  /**
2668
2704
  * Initialize Meta (Facebook) Pixel
2705
+ *
2706
+ * Async because we resolve SHA-256 hashes for advanced matching (Meta's
2707
+ * `external_id` / `em`) before calling fbq('init'). Aligning the hashes the
2708
+ * browser Pixel sends with what CAPI sends (meta.js shovels user_id /
2709
+ * visitor_id / anonymous_id into external_id[], and `em` is sha256 of the
2710
+ * lowercased email) is the dedup-quality lift the CAPI side can't fix alone.
2669
2711
  */
2670
2712
  initializeMetaPixel(config) {
2671
- try {
2672
- // Load Meta Pixel script
2673
- (function (f, b, e, v, n, t, s) {
2674
- if (f.fbq)
2675
- return;
2676
- n = f.fbq = function () {
2677
- n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments);
2678
- };
2679
- if (!f._fbq)
2680
- f._fbq = n;
2681
- n.push = n;
2682
- n.loaded = !0;
2683
- n.version = '2.0';
2684
- n.queue = [];
2685
- t = b.createElement(e);
2686
- t.async = !0;
2687
- t.src = v;
2688
- s = b.getElementsByTagName(e)[0];
2689
- s.parentNode.insertBefore(t, s);
2690
- })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
2691
- // Initialize pixel only — do NOT fire PageView here. The SDK's own pageview
2692
- // tracking (track('pageview')) routes through trackToPixels and fires a single
2693
- // mapped PageView with a shared eventID. Firing it again here produced TWO
2694
- // PageViews per load (one un-deduped). Note: if the host app disables pageview
2695
- // tracking entirely, no PageView is sent which is the correct outcome.
2696
- window.fbq('init', config.pixel_id);
2697
- this.log('Meta Pixel initialized:', config.pixel_id);
2698
- }
2699
- catch (error) {
2700
- this.log('Error initializing Meta Pixel:', error);
2701
- }
2713
+ return __awaiter(this, void 0, void 0, function* () {
2714
+ var _a;
2715
+ try {
2716
+ // Load Meta Pixel script
2717
+ (function (f, b, e, v, n, t, s) {
2718
+ if (f.fbq)
2719
+ return;
2720
+ n = f.fbq = function () {
2721
+ n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments);
2722
+ };
2723
+ if (!f._fbq)
2724
+ f._fbq = n;
2725
+ n.push = n;
2726
+ n.loaded = !0;
2727
+ n.version = '2.0';
2728
+ n.queue = [];
2729
+ t = b.createElement(e);
2730
+ t.async = !0;
2731
+ t.src = v;
2732
+ s = b.getElementsByTagName(e)[0];
2733
+ s.parentNode.insertBefore(t, s);
2734
+ })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
2735
+ // Build advanced-matching object. Skipped silently if Web Crypto isn't
2736
+ // available Pixel still initializes, just without advanced matching.
2737
+ // Anonymous_id is stable across the session and always present, so we
2738
+ // never need to re-init on identify(): CAPI carries both anonymous_id
2739
+ // AND user_id in its external_id[] array, so either side matching one
2740
+ // hash slot is enough to dedupe.
2741
+ const advancedMatching = {};
2742
+ const identity = (_a = this.getIdentity) === null || _a === void 0 ? void 0 : _a.call(this);
2743
+ if (identity === null || identity === void 0 ? void 0 : identity.externalId) {
2744
+ const hash = yield sha256Hex(String(identity.externalId));
2745
+ if (hash)
2746
+ advancedMatching.external_id = hash;
2747
+ }
2748
+ if (identity === null || identity === void 0 ? void 0 : identity.email) {
2749
+ const hash = yield sha256Hex(String(identity.email).toLowerCase().trim());
2750
+ if (hash)
2751
+ advancedMatching.em = hash;
2752
+ }
2753
+ // Initialize pixel only — do NOT fire PageView here. The SDK's own pageview
2754
+ // tracking (track('pageview')) routes through trackToPixels and fires a single
2755
+ // mapped PageView with a shared eventID. Firing it again here produced TWO
2756
+ // PageViews per load (one un-deduped). Note: if the host app disables pageview
2757
+ // tracking entirely, no PageView is sent — which is the correct outcome.
2758
+ if (Object.keys(advancedMatching).length > 0) {
2759
+ window.fbq('init', config.pixel_id, advancedMatching);
2760
+ this.log('Meta Pixel initialized with advanced matching:', config.pixel_id, Object.keys(advancedMatching));
2761
+ }
2762
+ else {
2763
+ window.fbq('init', config.pixel_id);
2764
+ this.log('Meta Pixel initialized (no advanced matching):', config.pixel_id);
2765
+ }
2766
+ }
2767
+ catch (error) {
2768
+ this.log('Error initializing Meta Pixel:', error);
2769
+ }
2770
+ });
2702
2771
  }
2703
2772
  /**
2704
2773
  * Initialize Google Tag
@@ -3518,7 +3587,19 @@ class Datalyr {
3518
3587
  this.container = new ContainerManager({
3519
3588
  workspaceId: this.config.workspaceId,
3520
3589
  endpoint: this.config.endpoint,
3521
- debug: this.config.debug
3590
+ debug: this.config.debug,
3591
+ // Lazy: invoked at the moment a third-party pixel inits, AFTER the
3592
+ // /container-scripts roundtrip resolves — so a pre-init identify()
3593
+ // already updated this.identity / this.userProperties. distinctId
3594
+ // mirrors what CAPI puts in external_id[] (user_id || anonymous_id);
3595
+ // email lets the Pixel match on em with the same hash CAPI sends.
3596
+ getIdentity: () => {
3597
+ var _a, _b;
3598
+ return ({
3599
+ externalId: (_a = this.identity) === null || _a === void 0 ? void 0 : _a.getDistinctId(),
3600
+ email: (_b = this.userProperties) === null || _b === void 0 ? void 0 : _b.email,
3601
+ });
3602
+ }
3522
3603
  });
3523
3604
  // Initialize container asynchronously
3524
3605
  yield this.container.init().catch(error => {
@@ -4112,7 +4193,7 @@ class Datalyr {
4112
4193
  resolution_method: 'browser_sdk',
4113
4194
  resolution_confidence: 1.0,
4114
4195
  // SDK metadata (keep in sync with package.json version)
4115
- sdk_version: '1.6.0',
4196
+ sdk_version: '1.6.3',
4116
4197
  sdk_name: 'datalyr-web-sdk'
4117
4198
  };
4118
4199
  return payload;