@datalyr/web 1.7.3 → 1.7.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,5 +1,5 @@
1
1
  /**
2
- * @datalyr/web v1.7.3
2
+ * @datalyr/web v1.7.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
@@ -4340,7 +4340,7 @@ class Datalyr {
4340
4340
  return this.initializationPromise;
4341
4341
  }
4342
4342
  this.initializationPromise = (() => __awaiter(this, void 0, void 0, function* () {
4343
- var _a;
4343
+ var _a, _b;
4344
4344
  try {
4345
4345
  // SEC-03: encryption is for PII-AT-REST only — it must NOT gate event delivery,
4346
4346
  // pageviews, or pixels. On a non-secure context (http://) or an old browser,
@@ -4463,6 +4463,15 @@ class Datalyr {
4463
4463
  // opted-out / DNT / GPC visitor's id + click-ids aren't stamped on outbound links.
4464
4464
  this.syncOutboundLinkParams(this.config.checkoutChampDomains);
4465
4465
  }
4466
+ // Stripe Payment Link auto-decoration (D1, DEFAULT ON): stamp
4467
+ // client_reference_id=<visitor_id> (+ prefilled_email when identified) onto
4468
+ // buy.stripe.com / custom payment-link domain anchors and Stripe embeds, so a
4469
+ // snippet-only merchant's checkout.session.completed webhooks attribute
4470
+ // deterministically with zero server work. Gated on shouldTrack() so
4471
+ // opted-out / DNT / GPC visitors never get an id stamped into outbound links.
4472
+ if (this.config.stripePaymentLinks !== false && this.shouldTrack()) {
4473
+ this.syncStripePaymentLinks((_b = this.config.stripeLinkDomains) !== null && _b !== void 0 ? _b : []);
4474
+ }
4466
4475
  // Track initial page view if enabled (AFTER encryption ready)
4467
4476
  if (this.config.trackPageViews) {
4468
4477
  this.page();
@@ -5383,6 +5392,159 @@ class Datalyr {
5383
5392
  }
5384
5393
  this.log("Checkout Champ outbound-link sync active for:", lowerDomains);
5385
5394
  }
5395
+ /**
5396
+ * Stripe Payment Link auto-decoration (D1). Structurally mirrors
5397
+ * syncOutboundLinkParams: stampAll on init, debounced MutationObserver for
5398
+ * SPA-rendered anchors, capture-phase click re-stamp + queue flush, disposer
5399
+ * wired into destroy()/pagehide.
5400
+ *
5401
+ * Match: exact-host equality against buy.stripe.com + config.stripeLinkDomains
5402
+ * via the URL API — NEVER substring (a[href*=...] would match
5403
+ * evil.com/buy.stripe.com paths) and NEVER checkout.stripe.com (Checkout
5404
+ * Session URLs ignore these params — the session is already created).
5405
+ *
5406
+ * Stamp (URL API preserves existing query + hash):
5407
+ * - client_reference_id=<visitorId> only if the param is absent (merchant-set
5408
+ * wins) and the id passes the Stripe format guard (Stripe silently DROPS
5409
+ * invalid values — don't write garbage into merchant links).
5410
+ * - prefilled_email=<email> only if identified email exists, the param is
5411
+ * absent, locked_prefilled_email is absent, and privacyMode !== 'strict'.
5412
+ * - <stripe-pricing-table>/<stripe-buy-button>: client-reference-id attribute
5413
+ * if absent.
5414
+ *
5415
+ * The decorated link's checkout.session.completed arrives with
5416
+ * client_reference_id, which stripe-connect.js already reads — zero server
5417
+ * changes. window.open(paymentLink) is not covered (use getVisitorId()
5418
+ * manually); iframe/shadow-DOM links are unreachable from document.
5419
+ */
5420
+ syncStripePaymentLinks(extraDomains) {
5421
+ if (typeof window === "undefined" || typeof document === "undefined")
5422
+ return;
5423
+ const hosts = new Set(["buy.stripe.com"]);
5424
+ for (const d of extraDomains) {
5425
+ const h = String(d).trim().toLowerCase();
5426
+ if (h)
5427
+ hosts.add(h);
5428
+ }
5429
+ const matchesPaymentLinkHost = (href) => {
5430
+ try {
5431
+ const host = new URL(href, window.location.href).hostname.toLowerCase();
5432
+ return hosts.has(host); // exact-host equality only
5433
+ }
5434
+ catch (_a) {
5435
+ return false;
5436
+ }
5437
+ };
5438
+ // Stripe accepts alphanumeric + `-` + `_`, max 200 chars, and silently drops
5439
+ // invalid values. Legacy/foreign persisted cookie ids are unconstrained —
5440
+ // skip silently rather than stamp a value Stripe would discard.
5441
+ const CLIENT_REFERENCE_ID_RE = /^[A-Za-z0-9_-]{1,200}$/;
5442
+ const getClientReferenceId = () => {
5443
+ const vid = this.identity.getAnonymousId();
5444
+ return vid && CLIENT_REFERENCE_ID_RE.test(vid) ? vid : null;
5445
+ };
5446
+ const getPrefilledEmail = () => {
5447
+ var _a;
5448
+ if (this.config.privacyMode === "strict")
5449
+ return null;
5450
+ const email = (_a = this.userProperties) === null || _a === void 0 ? void 0 : _a.email;
5451
+ return typeof email === "string" && email ? email : null;
5452
+ };
5453
+ const stampLink = (anchor) => {
5454
+ if (!anchor.href || !matchesPaymentLinkHost(anchor.href))
5455
+ return;
5456
+ try {
5457
+ const u = new URL(anchor.href, window.location.href);
5458
+ let mutated = false;
5459
+ const vid = getClientReferenceId();
5460
+ if (vid && !u.searchParams.get("client_reference_id")) {
5461
+ u.searchParams.set("client_reference_id", vid);
5462
+ mutated = true;
5463
+ }
5464
+ const email = getPrefilledEmail();
5465
+ if (email &&
5466
+ !u.searchParams.get("prefilled_email") &&
5467
+ !u.searchParams.get("locked_prefilled_email")) {
5468
+ // URL API percent-encodes automatically.
5469
+ u.searchParams.set("prefilled_email", email);
5470
+ mutated = true;
5471
+ }
5472
+ if (mutated)
5473
+ anchor.href = u.toString();
5474
+ }
5475
+ catch (_a) {
5476
+ // Ignore malformed URLs — don't break the page.
5477
+ }
5478
+ };
5479
+ const stampEmbeds = () => {
5480
+ const vid = getClientReferenceId();
5481
+ if (!vid)
5482
+ return;
5483
+ document.querySelectorAll("stripe-pricing-table, stripe-buy-button").forEach((el) => {
5484
+ if (!el.getAttribute("client-reference-id")) {
5485
+ el.setAttribute("client-reference-id", vid);
5486
+ }
5487
+ });
5488
+ };
5489
+ const stampAll = () => {
5490
+ document.querySelectorAll("a[href]").forEach((el) => stampLink(el));
5491
+ stampEmbeds();
5492
+ };
5493
+ const onClick = (e) => {
5494
+ var _a, _b;
5495
+ const target = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest("a[href]");
5496
+ if (!target)
5497
+ return;
5498
+ const anchor = target;
5499
+ if (!matchesPaymentLinkHost(anchor.href))
5500
+ return;
5501
+ stampLink(anchor); // re-stamp in case identify() happened since DOMReady
5502
+ try {
5503
+ (_b = this.queue) === null || _b === void 0 ? void 0 : _b.flush();
5504
+ }
5505
+ catch (_c) {
5506
+ // Best-effort — never block navigation.
5507
+ }
5508
+ };
5509
+ stampAll();
5510
+ document.addEventListener("click", onClick, true);
5511
+ try {
5512
+ // Debounce re-stamping (same rationale as the CC sync): a busy SPA can fire
5513
+ // thousands of mutations per second; 150ms is small enough to catch links
5514
+ // before the user can click them, large enough to coalesce bursts. The
5515
+ // click-time re-stamp is the final safety net — no pushState hook needed.
5516
+ let restampTimer = null;
5517
+ const scheduleRestamp = () => {
5518
+ if (restampTimer != null)
5519
+ return;
5520
+ restampTimer = setTimeout(() => {
5521
+ restampTimer = null;
5522
+ stampAll();
5523
+ }, 150);
5524
+ };
5525
+ const observer = new MutationObserver(scheduleRestamp);
5526
+ observer.observe(document.documentElement, { childList: true, subtree: true });
5527
+ // Observer + click listener live for the session; pagehide cleans up on
5528
+ // full-page unload, and destroy() tears them down early via this disposer
5529
+ // (otherwise destroy()+re-init() leaks the observer + capturing listener).
5530
+ this.stripeLinksDisposer = () => {
5531
+ try {
5532
+ observer.disconnect();
5533
+ }
5534
+ catch ( /* idempotent */_a) { /* idempotent */ }
5535
+ if (restampTimer != null) {
5536
+ clearTimeout(restampTimer);
5537
+ restampTimer = null;
5538
+ }
5539
+ document.removeEventListener("click", onClick, true);
5540
+ };
5541
+ window.addEventListener("pagehide", () => { var _a; return (_a = this.stripeLinksDisposer) === null || _a === void 0 ? void 0 : _a.call(this); }, { once: true });
5542
+ }
5543
+ catch (error) {
5544
+ this.log("MutationObserver setup failed (Stripe Payment Link sync):", error);
5545
+ }
5546
+ this.log("Stripe Payment Link auto-decoration active for:", Array.from(hosts));
5547
+ }
5386
5548
  /**
5387
5549
  * Create event payload
5388
5550
  */
@@ -5465,7 +5627,7 @@ class Datalyr {
5465
5627
  // SDK metadata. MUST stay in sync with package.json "version" — the build guard
5466
5628
  // (scripts/check-bundle.js, run by build:check) fails the build if the bundle's
5467
5629
  // sdk_version doesn't match package.json. (FSR-103)
5468
- sdk_version: '1.7.3',
5630
+ sdk_version: '1.7.4',
5469
5631
  sdk_name: 'datalyr-web-sdk'
5470
5632
  };
5471
5633
  return payload;
@@ -5686,6 +5848,10 @@ class Datalyr {
5686
5848
  this.outboundDisposer();
5687
5849
  this.outboundDisposer = undefined;
5688
5850
  }
5851
+ if (this.stripeLinksDisposer) {
5852
+ this.stripeLinksDisposer();
5853
+ this.stripeLinksDisposer = undefined;
5854
+ }
5689
5855
  // Clean up queue
5690
5856
  if (this.queue) {
5691
5857
  this.queue.destroy();