@datalyr/web 1.6.3 → 1.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"attribution.d.ts","sourceRoot":"","sources":["../src/attribution.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEvD,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,iBAAiB,CAAS;IAClC,OAAO,CAAC,aAAa,CAAW;IAChC,OAAO,CAAC,gBAAgB,CAAuC;IAC/D,OAAO,CAAC,UAAU,CAA2E;IAE7F,OAAO,CAAC,SAAS,CAgBf;IAEF,OAAO,CAAC,sBAAsB,CAO5B;gBAEU,OAAO,GAAE;QACnB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB;IAMN;;;OAGG;IACH,UAAU,IAAI,IAAI;IAIlB;;OAEG;IACH,kBAAkB,IAAI,WAAW;IA8DjC;;;;;OAKG;IACH,eAAe,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;IAwB/C;;;OAGG;IACH,aAAa,IAAI,WAAW,GAAG,IAAI;IASnC;;OAEG;IACH,cAAc,CAAC,WAAW,EAAE,WAAW,GAAG,IAAI;IAQ9C;;;OAGG;IACH,YAAY,IAAI,WAAW,GAAG,IAAI;IASlC;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,GAAG,IAAI;IAqBhE;;OAEG;IACH,UAAU,IAAI,UAAU,EAAE;IAI1B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAiDxB;;;OAGG;IACH,OAAO,CAAC,UAAU;IAKlB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAKxB;;OAEG;IACH,kBAAkB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IA0EzC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OAAO,CAAC,gBAAgB;IAKxB;;OAEG;IACH,kBAAkB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IA0EzC;;OAEG;IACH,OAAO,CAAC,eAAe;IA6CvB;;OAEG;IACH,OAAO,CAAC,eAAe;IA2BvB;;OAEG;IACH,OAAO,CAAC,eAAe;IAQvB;;OAEG;IACH,oBAAoB,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO;IAKvD;;OAEG;IACH,uBAAuB,IAAI,IAAI;CAYhC"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @datalyr/web v1.6.3
2
+ * @datalyr/web v1.6.4
3
3
  * Datalyr Web SDK - Modern attribution tracking for web applications
4
4
  * (c) 2026 Datalyr Inc.
5
5
  * Released under the MIT License
@@ -1528,6 +1528,7 @@ class AttributionManager {
1528
1528
  * Capture advertising platform cookies
1529
1529
  */
1530
1530
  captureAdCookies() {
1531
+ var _a, _b;
1531
1532
  const adCookies = {};
1532
1533
  // Facebook/Meta cookies
1533
1534
  adCookies._fbp = cookies.get('_fbp');
@@ -1552,13 +1553,29 @@ class AttributionManager {
1552
1553
  // Optionally set the cookie for future use
1553
1554
  cookies.set('_fbp', adCookies._fbp, 90);
1554
1555
  }
1556
+ // Persist the click time the FIRST time we see fbclid/gclid in URL so we can
1557
+ // rebuild fbc (and stamp real click time on server-side events) later even
1558
+ // when _fbc/_gclid cookies get evicted. Once-only: never overwrite.
1559
+ const fbclid = this.getCurrentFbclid();
1560
+ if (fbclid && !cookies.get('_dl_fbclid_at')) {
1561
+ cookies.set('_dl_fbclid_at', String(Date.now()), 90);
1562
+ }
1563
+ const gclid = this.hasClickId('gclid') ? ((_b = (_a = this.queryParamsCache) === null || _a === void 0 ? void 0 : _a.gclid) !== null && _b !== void 0 ? _b : null) : null;
1564
+ if (gclid && !cookies.get('_dl_gclid_at')) {
1565
+ cookies.set('_dl_gclid_at', String(Date.now()), 90);
1566
+ }
1555
1567
  // Generate _fbc if we have fbclid but no _fbc.
1556
1568
  // Meta's fbc format is `fb.{subdomainIndex}.{creationTime}.{fbclid}` where
1557
1569
  // creationTime is UNIX time in MILLISECONDS (matches the _fbp generation above
1558
1570
  // and the real _fbc cookie the Meta Pixel writes). Do NOT use seconds here.
1559
- const fbclid = this.getCurrentFbclid();
1560
1571
  if (fbclid && !adCookies._fbc) {
1561
- const timestamp = Date.now();
1572
+ // Prefer the persisted click time when valid; fall back to now if the
1573
+ // cookie is missing or corrupted (e.g. user edited it to garbage).
1574
+ // Without this guard, Number("abc") = NaN and Meta would reject
1575
+ // `fb.1.NaN.{fbclid}`.
1576
+ const stored = cookies.get('_dl_fbclid_at');
1577
+ const parsed = stored ? Number(stored) : NaN;
1578
+ const timestamp = Number.isFinite(parsed) && parsed > 0 ? parsed : Date.now();
1562
1579
  adCookies._fbc = `fb.1.${timestamp}.${fbclid}`;
1563
1580
  // Optionally set the cookie for future use
1564
1581
  cookies.set('_fbc', adCookies._fbc, 90);
@@ -3515,6 +3532,14 @@ class Datalyr {
3515
3532
  }
3516
3533
  // Set default config values
3517
3534
  this.config = Object.assign({ endpoint: 'https://ingest.datalyr.com', debug: false, batchSize: 10, flushInterval: 5000, flushAt: 10, criticalEvents: undefined, highPriorityEvents: undefined, sessionTimeout: 60 * 60 * 1000, trackSessions: true, attributionWindow: 90 * 24 * 60 * 60 * 1000, trackedParams: [], respectDoNotTrack: false, respectGlobalPrivacyControl: true, privacyMode: 'standard', cookieDomain: 'auto', cookieExpires: 365, secureCookie: 'auto', sameSite: 'Lax', cookiePrefix: '__dl_', enablePerformanceTracking: true, enableFingerprinting: true, maxRetries: 5, retryDelay: 1000, maxOfflineQueueSize: 100, trackSPA: true, trackPageViews: true, fallbackEndpoints: [], plugins: [] }, config);
3535
+ // platform:'shopify' implies cart-attribute sync: stamp visitor_id + Meta
3536
+ // click signals into note_attributes so server-side order webhooks can
3537
+ // attribute guest checkouts. Lets the Shopify theme app-embed enable it via
3538
+ // data-platform="shopify" alone. `=== undefined` so an explicit
3539
+ // shopifyCartAttributes:false still wins.
3540
+ if (this.config.platform === 'shopify' && this.config.shopifyCartAttributes === undefined) {
3541
+ this.config.shopifyCartAttributes = true;
3542
+ }
3518
3543
  // Initialize cookie storage with config
3519
3544
  this.cookies = new CookieStorage({
3520
3545
  domain: this.config.cookieDomain,
@@ -3526,6 +3551,12 @@ class Datalyr {
3526
3551
  storage.migrateFromLegacyPrefix();
3527
3552
  // Check opt-out AFTER cookies configured (Issue #14)
3528
3553
  this.optedOut = this.cookies.get('__dl_opt_out') === 'true';
3554
+ // CC funnel page entry: restore the _dl_* URL bridge BEFORE IdentityManager
3555
+ // initializes so the storefront's visitor_id is the one used here (instead
3556
+ // of auto-generating a fresh one on this domain).
3557
+ if (this.config.platform === 'checkoutchamp') {
3558
+ this.restoreFromURL();
3559
+ }
3529
3560
  // Initialize modules
3530
3561
  this.identity = new IdentityManager();
3531
3562
  this.session = new SessionManager(this.config.sessionTimeout);
@@ -3610,6 +3641,16 @@ class Datalyr {
3610
3641
  this.log('Container initialization failed:', error);
3611
3642
  });
3612
3643
  }
3644
+ // autoIdentify default for the CC bridge:
3645
+ // - platform === 'checkoutchamp' (CC funnel pages) OR
3646
+ // - checkoutChampDomains set (Shopify storefronts feeding a CC funnel)
3647
+ // turn autoIdentify on unless the caller explicitly set it to false.
3648
+ // The 95% claim relies on email capture on BOTH ends of the bridge.
3649
+ const ccBridgeActive = this.config.platform === 'checkoutchamp'
3650
+ || (Array.isArray(this.config.checkoutChampDomains) && this.config.checkoutChampDomains.length > 0);
3651
+ if (ccBridgeActive && this.config.autoIdentify === undefined) {
3652
+ this.config.autoIdentify = true;
3653
+ }
3613
3654
  // Initialize auto-identify if explicitly enabled (opt-in)
3614
3655
  if (this.config.autoIdentify === true) {
3615
3656
  this.autoIdentify = new AutoIdentifyManager({
@@ -3642,6 +3683,12 @@ class Datalyr {
3642
3683
  this.log('Shopify cart attribute sync failed:', error);
3643
3684
  });
3644
3685
  }
3686
+ // Storefront → CC bridge: stamp _dl_* params on outbound links to the
3687
+ // configured CC domains so visitor_id + click signals cross the domain
3688
+ // boundary. Inert unless checkoutChampDomains is set.
3689
+ if (Array.isArray(this.config.checkoutChampDomains) && this.config.checkoutChampDomains.length > 0) {
3690
+ this.syncOutboundLinkParams(this.config.checkoutChampDomains);
3691
+ }
3645
3692
  // Track initial page view if enabled (AFTER encryption ready)
3646
3693
  if (this.config.trackPageViews) {
3647
3694
  this.page();
@@ -4112,6 +4159,7 @@ class Datalyr {
4112
4159
  const visitorId = this.identity.getAnonymousId();
4113
4160
  const fbc = this.cookies.get("_fbc") || attribution._fbc;
4114
4161
  const fbp = this.cookies.get("_fbp") || attribution._fbp;
4162
+ const fbclidAt = this.cookies.get("_dl_fbclid_at");
4115
4163
  if (visitorId)
4116
4164
  attributes._datalyr_visitor_id = visitorId;
4117
4165
  if (fbc)
@@ -4120,6 +4168,8 @@ class Datalyr {
4120
4168
  attributes._datalyr_fbp = String(fbp);
4121
4169
  if (fbclid)
4122
4170
  attributes._datalyr_fbclid = String(fbclid);
4171
+ if (fbclidAt)
4172
+ attributes._datalyr_fbclid_at = String(fbclidAt);
4123
4173
  if (Object.keys(attributes).length === 0)
4124
4174
  return;
4125
4175
  try {
@@ -4137,6 +4187,218 @@ class Datalyr {
4137
4187
  }
4138
4188
  });
4139
4189
  }
4190
+ /**
4191
+ * Restore _dl_* URL bridge params on a Checkout Champ funnel page.
4192
+ * Runs BEFORE IdentityManager so the storefront's visitor_id wins over a
4193
+ * freshly auto-generated one. Also restores _fbc / _fbp cookies and the
4194
+ * fbclid click time so server-side rebuilds carry the real click moment.
4195
+ *
4196
+ * Strategy: stamp matching cookies (without clobbering pre-existing values),
4197
+ * then rewrite the URL so `_dl_fbclid` becomes `fbclid` — that way the rest
4198
+ * of the SDK's attribution layer (which reads `params.fbclid`) works
4199
+ * unchanged, and any merchant analytics also see the canonical click ID.
4200
+ */
4201
+ restoreFromURL() {
4202
+ var _a;
4203
+ if (typeof window === "undefined" || typeof document === "undefined")
4204
+ return;
4205
+ try {
4206
+ const params = new URLSearchParams(window.location.search);
4207
+ const get = (k) => params.get(k);
4208
+ const vid = get("_dl_vid");
4209
+ const fbc = get("_dl_fbc");
4210
+ const fbp = get("_dl_fbp");
4211
+ const fbclid = get("_dl_fbclid");
4212
+ const fbclidAt = get("_dl_fbclid_at");
4213
+ const gclid = get("_dl_gclid");
4214
+ const gclidAt = get("_dl_gclid_at");
4215
+ // visitor_id: bridge wins. The whole point of `_dl_vid` is to unify the
4216
+ // storefront's session with the CC funnel session. A pre-existing local
4217
+ // cookie from a prior direct CC visit would silently sink the integration
4218
+ // (CC events stay on the local id; storefront events use the bridged id;
4219
+ // the two never link). Overwrite — orphaned local events are fine.
4220
+ if (vid)
4221
+ this.cookies.set("__dl_visitor_id", vid, 365);
4222
+ // Meta cookies + click-time cookies: existing wins. _fbc / _fbp may have
4223
+ // been written by Meta Pixel on the CC funnel page itself (more recent
4224
+ // than the bridged value); _dl_fbclid_at should record first-touch click
4225
+ // time per device, not get reset by a bridge from a new campaign.
4226
+ const setIfMissing = (name, value) => {
4227
+ if (!value)
4228
+ return;
4229
+ if (this.cookies.get(name))
4230
+ return;
4231
+ this.cookies.set(name, value, 365);
4232
+ };
4233
+ setIfMissing("_fbc", fbc);
4234
+ setIfMissing("_fbp", fbp);
4235
+ setIfMissing("_dl_fbclid_at", fbclidAt);
4236
+ setIfMissing("_dl_gclid_at", gclidAt);
4237
+ // Rewrite URL: _dl_fbclid → fbclid (etc.) so captureAttribution() picks
4238
+ // them up via its existing `params.fbclid` path. Strip the _dl_* params
4239
+ // either way so they don't leak into downstream analytics URLs.
4240
+ let rewrote = false;
4241
+ const mappings = [
4242
+ ["fbclid", fbclid],
4243
+ ["gclid", gclid]
4244
+ ];
4245
+ for (const [canonical, value] of mappings) {
4246
+ if (value && !params.get(canonical)) {
4247
+ params.set(canonical, value);
4248
+ rewrote = true;
4249
+ }
4250
+ }
4251
+ for (const k of ["_dl_vid", "_dl_fbc", "_dl_fbp", "_dl_fbclid", "_dl_fbclid_at", "_dl_gclid", "_dl_gclid_at"]) {
4252
+ if (params.has(k)) {
4253
+ params.delete(k);
4254
+ rewrote = true;
4255
+ }
4256
+ }
4257
+ if (rewrote && typeof ((_a = window.history) === null || _a === void 0 ? void 0 : _a.replaceState) === "function") {
4258
+ const newSearch = params.toString();
4259
+ const newUrl = window.location.pathname +
4260
+ (newSearch ? "?" + newSearch : "") +
4261
+ window.location.hash;
4262
+ window.history.replaceState(window.history.state, "", newUrl);
4263
+ }
4264
+ this.log("Checkout Champ bridge restored:", {
4265
+ had_vid: !!vid,
4266
+ had_fbc: !!fbc,
4267
+ had_fbp: !!fbp,
4268
+ had_fbclid: !!fbclid,
4269
+ had_gclid: !!gclid
4270
+ });
4271
+ }
4272
+ catch (error) {
4273
+ // Don't let bridge restoration block init — fall through to normal SDK
4274
+ // behavior (a fresh visitor_id, no restored click signals).
4275
+ this.log("restoreFromURL failed:", error);
4276
+ }
4277
+ }
4278
+ /**
4279
+ * Storefront → Checkout Champ link stamping. Finds every `<a href>` whose host
4280
+ * matches the configured CC domain list and appends `?_dl_vid=…&_dl_fbc=…&
4281
+ * _dl_fbp=…&_dl_fbclid=…&_dl_fbclid_at=…&_dl_gclid=…` so the user's
4282
+ * visitor_id + Meta click signals cross the domain. MutationObserver watches
4283
+ * for dynamically-injected links. On click, force-flush the event queue so
4284
+ * any pending track() events land before the browser navigates away.
4285
+ */
4286
+ syncOutboundLinkParams(domains) {
4287
+ if (typeof window === "undefined" || typeof document === "undefined")
4288
+ return;
4289
+ const lowerDomains = domains.map((d) => d.toLowerCase());
4290
+ const matchesCcDomain = (href) => {
4291
+ try {
4292
+ const host = new URL(href, window.location.href).hostname.toLowerCase();
4293
+ return lowerDomains.some((d) => host === d || host.endsWith("." + d));
4294
+ }
4295
+ catch (_a) {
4296
+ return false;
4297
+ }
4298
+ };
4299
+ const buildBridgeParams = () => {
4300
+ const attribution = this.attribution.getAttributionData();
4301
+ const out = {};
4302
+ const vid = this.identity.getAnonymousId();
4303
+ const fbc = this.cookies.get("_fbc") || attribution._fbc;
4304
+ const fbp = this.cookies.get("_fbp") || attribution._fbp;
4305
+ const fbclid = attribution.clickIdType === "fbclid" ? attribution.clickId : null;
4306
+ const fbclidAt = this.cookies.get("_dl_fbclid_at");
4307
+ const gclid = attribution.clickIdType === "gclid" ? attribution.clickId : null;
4308
+ const gclidAt = this.cookies.get("_dl_gclid_at");
4309
+ if (vid)
4310
+ out._dl_vid = vid;
4311
+ if (fbc)
4312
+ out._dl_fbc = String(fbc);
4313
+ if (fbp)
4314
+ out._dl_fbp = String(fbp);
4315
+ if (fbclid)
4316
+ out._dl_fbclid = String(fbclid);
4317
+ if (fbclidAt)
4318
+ out._dl_fbclid_at = String(fbclidAt);
4319
+ if (gclid)
4320
+ out._dl_gclid = String(gclid);
4321
+ if (gclidAt)
4322
+ out._dl_gclid_at = String(gclidAt);
4323
+ return out;
4324
+ };
4325
+ const stampLink = (anchor) => {
4326
+ if (!anchor.href || !matchesCcDomain(anchor.href))
4327
+ return;
4328
+ try {
4329
+ const u = new URL(anchor.href, window.location.href);
4330
+ const bridge = buildBridgeParams();
4331
+ let mutated = false;
4332
+ for (const [k, v] of Object.entries(bridge)) {
4333
+ if (!u.searchParams.get(k)) {
4334
+ u.searchParams.set(k, v);
4335
+ mutated = true;
4336
+ }
4337
+ }
4338
+ if (mutated)
4339
+ anchor.href = u.toString();
4340
+ }
4341
+ catch (_a) {
4342
+ // Ignore malformed URLs — don't break the page.
4343
+ }
4344
+ };
4345
+ const stampAll = () => {
4346
+ document.querySelectorAll("a[href]").forEach((el) => stampLink(el));
4347
+ };
4348
+ const onClick = (e) => {
4349
+ var _a, _b;
4350
+ const target = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest("a[href]");
4351
+ if (!target)
4352
+ return;
4353
+ const anchor = target;
4354
+ if (!matchesCcDomain(anchor.href))
4355
+ return;
4356
+ stampLink(anchor); // re-stamp in case attribution changed since DOMReady
4357
+ try {
4358
+ (_b = this.queue) === null || _b === void 0 ? void 0 : _b.flush();
4359
+ }
4360
+ catch (_c) {
4361
+ // Best-effort — never block navigation.
4362
+ }
4363
+ };
4364
+ stampAll();
4365
+ document.addEventListener("click", onClick, true);
4366
+ try {
4367
+ // Debounce re-stamping. A busy SPA (infinite scroll, animations,
4368
+ // React/Vue updates) can fire thousands of mutations per second; a naive
4369
+ // re-stamp on every notification would burn CPU pointlessly when
4370
+ // outbound link sets only change occasionally. 150ms is small enough to
4371
+ // catch links before the user can click them, large enough to coalesce
4372
+ // bursts.
4373
+ let restampTimer = null;
4374
+ const scheduleRestamp = () => {
4375
+ if (restampTimer != null)
4376
+ return;
4377
+ restampTimer = setTimeout(() => {
4378
+ restampTimer = null;
4379
+ stampAll();
4380
+ }, 150);
4381
+ };
4382
+ const observer = new MutationObserver(scheduleRestamp);
4383
+ observer.observe(document.documentElement, { childList: true, subtree: true });
4384
+ // Observer + click listener live for the session; pagehide cleans up on
4385
+ // full-page unload. SPA route changes are fine — we WANT stamping to keep
4386
+ // working across virtual navigations.
4387
+ window.addEventListener("pagehide", () => {
4388
+ try {
4389
+ observer.disconnect();
4390
+ }
4391
+ catch ( /* idempotent */_a) { /* idempotent */ }
4392
+ if (restampTimer != null)
4393
+ clearTimeout(restampTimer);
4394
+ document.removeEventListener("click", onClick, true);
4395
+ }, { once: true });
4396
+ }
4397
+ catch (error) {
4398
+ this.log("MutationObserver setup failed (CC link sync):", error);
4399
+ }
4400
+ this.log("Checkout Champ outbound-link sync active for:", lowerDomains);
4401
+ }
4140
4402
  /**
4141
4403
  * Create event payload
4142
4404
  */
@@ -4197,7 +4459,7 @@ class Datalyr {
4197
4459
  resolution_method: 'browser_sdk',
4198
4460
  resolution_confidence: 1.0,
4199
4461
  // SDK metadata (keep in sync with package.json version)
4200
- sdk_version: '1.6.3',
4462
+ sdk_version: '1.6.4',
4201
4463
  sdk_name: 'datalyr-web-sdk'
4202
4464
  };
4203
4465
  return payload;