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