@datalyr/web 1.7.3 → 1.7.5

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.5
3
3
  * Datalyr Web SDK - Modern attribution tracking for web applications
4
4
  * (c) 2026 Datalyr Inc.
5
5
  * Released under the MIT License
@@ -2302,16 +2302,22 @@ class EventQueue {
2302
2302
  // so only request it when the body is safely under the cap; oversized batches go via
2303
2303
  // a normal fetch (no cap) and succeed.
2304
2304
  const useKeepalive = new Blob([body]).size <= 60000;
2305
+ // First-party endpoint (the customer's own tracking host, never *.datalyr.com — set
2306
+ // by the script-origin auto-derive): send the cookie cross-subdomain so the edge can
2307
+ // read/refresh the server-set, ITP-durable visitor_id. Legacy datalyr.com endpoints
2308
+ // stay credential-less — unchanged behavior for every existing install.
2309
+ let firstPartyEndpoint = false;
2305
2310
  try {
2306
- const response = yield fetch(currentEndpoint, {
2307
- method: 'POST',
2308
- headers: {
2311
+ firstPartyEndpoint = !/(^|\.)datalyr\.com$/i.test(new URL(currentEndpoint).hostname);
2312
+ }
2313
+ catch (_a) {
2314
+ /* malformed endpoint — leave false */
2315
+ }
2316
+ try {
2317
+ const response = yield fetch(currentEndpoint, Object.assign({ method: 'POST', headers: {
2309
2318
  'Content-Type': 'application/json',
2310
2319
  'X-Batch-Size': events.length.toString()
2311
- },
2312
- body,
2313
- keepalive: useKeepalive
2314
- });
2320
+ }, body, keepalive: useKeepalive }, (firstPartyEndpoint ? { credentials: 'include' } : {})));
2315
2321
  if (!response.ok) {
2316
2322
  // Handle rate limiting
2317
2323
  if (response.status === 429) {
@@ -4340,7 +4346,7 @@ class Datalyr {
4340
4346
  return this.initializationPromise;
4341
4347
  }
4342
4348
  this.initializationPromise = (() => __awaiter(this, void 0, void 0, function* () {
4343
- var _a;
4349
+ var _a, _b;
4344
4350
  try {
4345
4351
  // SEC-03: encryption is for PII-AT-REST only — it must NOT gate event delivery,
4346
4352
  // pageviews, or pixels. On a non-secure context (http://) or an old browser,
@@ -4463,6 +4469,15 @@ class Datalyr {
4463
4469
  // opted-out / DNT / GPC visitor's id + click-ids aren't stamped on outbound links.
4464
4470
  this.syncOutboundLinkParams(this.config.checkoutChampDomains);
4465
4471
  }
4472
+ // Stripe Payment Link auto-decoration (D1, DEFAULT ON): stamp
4473
+ // client_reference_id=<visitor_id> (+ prefilled_email when identified) onto
4474
+ // buy.stripe.com / custom payment-link domain anchors and Stripe embeds, so a
4475
+ // snippet-only merchant's checkout.session.completed webhooks attribute
4476
+ // deterministically with zero server work. Gated on shouldTrack() so
4477
+ // opted-out / DNT / GPC visitors never get an id stamped into outbound links.
4478
+ if (this.config.stripePaymentLinks !== false && this.shouldTrack()) {
4479
+ this.syncStripePaymentLinks((_b = this.config.stripeLinkDomains) !== null && _b !== void 0 ? _b : []);
4480
+ }
4466
4481
  // Track initial page view if enabled (AFTER encryption ready)
4467
4482
  if (this.config.trackPageViews) {
4468
4483
  this.page();
@@ -5383,6 +5398,159 @@ class Datalyr {
5383
5398
  }
5384
5399
  this.log("Checkout Champ outbound-link sync active for:", lowerDomains);
5385
5400
  }
5401
+ /**
5402
+ * Stripe Payment Link auto-decoration (D1). Structurally mirrors
5403
+ * syncOutboundLinkParams: stampAll on init, debounced MutationObserver for
5404
+ * SPA-rendered anchors, capture-phase click re-stamp + queue flush, disposer
5405
+ * wired into destroy()/pagehide.
5406
+ *
5407
+ * Match: exact-host equality against buy.stripe.com + config.stripeLinkDomains
5408
+ * via the URL API — NEVER substring (a[href*=...] would match
5409
+ * evil.com/buy.stripe.com paths) and NEVER checkout.stripe.com (Checkout
5410
+ * Session URLs ignore these params — the session is already created).
5411
+ *
5412
+ * Stamp (URL API preserves existing query + hash):
5413
+ * - client_reference_id=<visitorId> only if the param is absent (merchant-set
5414
+ * wins) and the id passes the Stripe format guard (Stripe silently DROPS
5415
+ * invalid values — don't write garbage into merchant links).
5416
+ * - prefilled_email=<email> only if identified email exists, the param is
5417
+ * absent, locked_prefilled_email is absent, and privacyMode !== 'strict'.
5418
+ * - <stripe-pricing-table>/<stripe-buy-button>: client-reference-id attribute
5419
+ * if absent.
5420
+ *
5421
+ * The decorated link's checkout.session.completed arrives with
5422
+ * client_reference_id, which stripe-connect.js already reads — zero server
5423
+ * changes. window.open(paymentLink) is not covered (use getVisitorId()
5424
+ * manually); iframe/shadow-DOM links are unreachable from document.
5425
+ */
5426
+ syncStripePaymentLinks(extraDomains) {
5427
+ if (typeof window === "undefined" || typeof document === "undefined")
5428
+ return;
5429
+ const hosts = new Set(["buy.stripe.com"]);
5430
+ for (const d of extraDomains) {
5431
+ const h = String(d).trim().toLowerCase();
5432
+ if (h)
5433
+ hosts.add(h);
5434
+ }
5435
+ const matchesPaymentLinkHost = (href) => {
5436
+ try {
5437
+ const host = new URL(href, window.location.href).hostname.toLowerCase();
5438
+ return hosts.has(host); // exact-host equality only
5439
+ }
5440
+ catch (_a) {
5441
+ return false;
5442
+ }
5443
+ };
5444
+ // Stripe accepts alphanumeric + `-` + `_`, max 200 chars, and silently drops
5445
+ // invalid values. Legacy/foreign persisted cookie ids are unconstrained —
5446
+ // skip silently rather than stamp a value Stripe would discard.
5447
+ const CLIENT_REFERENCE_ID_RE = /^[A-Za-z0-9_-]{1,200}$/;
5448
+ const getClientReferenceId = () => {
5449
+ const vid = this.identity.getAnonymousId();
5450
+ return vid && CLIENT_REFERENCE_ID_RE.test(vid) ? vid : null;
5451
+ };
5452
+ const getPrefilledEmail = () => {
5453
+ var _a;
5454
+ if (this.config.privacyMode === "strict")
5455
+ return null;
5456
+ const email = (_a = this.userProperties) === null || _a === void 0 ? void 0 : _a.email;
5457
+ return typeof email === "string" && email ? email : null;
5458
+ };
5459
+ const stampLink = (anchor) => {
5460
+ if (!anchor.href || !matchesPaymentLinkHost(anchor.href))
5461
+ return;
5462
+ try {
5463
+ const u = new URL(anchor.href, window.location.href);
5464
+ let mutated = false;
5465
+ const vid = getClientReferenceId();
5466
+ if (vid && !u.searchParams.get("client_reference_id")) {
5467
+ u.searchParams.set("client_reference_id", vid);
5468
+ mutated = true;
5469
+ }
5470
+ const email = getPrefilledEmail();
5471
+ if (email &&
5472
+ !u.searchParams.get("prefilled_email") &&
5473
+ !u.searchParams.get("locked_prefilled_email")) {
5474
+ // URL API percent-encodes automatically.
5475
+ u.searchParams.set("prefilled_email", email);
5476
+ mutated = true;
5477
+ }
5478
+ if (mutated)
5479
+ anchor.href = u.toString();
5480
+ }
5481
+ catch (_a) {
5482
+ // Ignore malformed URLs — don't break the page.
5483
+ }
5484
+ };
5485
+ const stampEmbeds = () => {
5486
+ const vid = getClientReferenceId();
5487
+ if (!vid)
5488
+ return;
5489
+ document.querySelectorAll("stripe-pricing-table, stripe-buy-button").forEach((el) => {
5490
+ if (!el.getAttribute("client-reference-id")) {
5491
+ el.setAttribute("client-reference-id", vid);
5492
+ }
5493
+ });
5494
+ };
5495
+ const stampAll = () => {
5496
+ document.querySelectorAll("a[href]").forEach((el) => stampLink(el));
5497
+ stampEmbeds();
5498
+ };
5499
+ const onClick = (e) => {
5500
+ var _a, _b;
5501
+ const target = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest("a[href]");
5502
+ if (!target)
5503
+ return;
5504
+ const anchor = target;
5505
+ if (!matchesPaymentLinkHost(anchor.href))
5506
+ return;
5507
+ stampLink(anchor); // re-stamp in case identify() happened since DOMReady
5508
+ try {
5509
+ (_b = this.queue) === null || _b === void 0 ? void 0 : _b.flush();
5510
+ }
5511
+ catch (_c) {
5512
+ // Best-effort — never block navigation.
5513
+ }
5514
+ };
5515
+ stampAll();
5516
+ document.addEventListener("click", onClick, true);
5517
+ try {
5518
+ // Debounce re-stamping (same rationale as the CC sync): a busy SPA can fire
5519
+ // thousands of mutations per second; 150ms is small enough to catch links
5520
+ // before the user can click them, large enough to coalesce bursts. The
5521
+ // click-time re-stamp is the final safety net — no pushState hook needed.
5522
+ let restampTimer = null;
5523
+ const scheduleRestamp = () => {
5524
+ if (restampTimer != null)
5525
+ return;
5526
+ restampTimer = setTimeout(() => {
5527
+ restampTimer = null;
5528
+ stampAll();
5529
+ }, 150);
5530
+ };
5531
+ const observer = new MutationObserver(scheduleRestamp);
5532
+ observer.observe(document.documentElement, { childList: true, subtree: true });
5533
+ // Observer + click listener live for the session; pagehide cleans up on
5534
+ // full-page unload, and destroy() tears them down early via this disposer
5535
+ // (otherwise destroy()+re-init() leaks the observer + capturing listener).
5536
+ this.stripeLinksDisposer = () => {
5537
+ try {
5538
+ observer.disconnect();
5539
+ }
5540
+ catch ( /* idempotent */_a) { /* idempotent */ }
5541
+ if (restampTimer != null) {
5542
+ clearTimeout(restampTimer);
5543
+ restampTimer = null;
5544
+ }
5545
+ document.removeEventListener("click", onClick, true);
5546
+ };
5547
+ window.addEventListener("pagehide", () => { var _a; return (_a = this.stripeLinksDisposer) === null || _a === void 0 ? void 0 : _a.call(this); }, { once: true });
5548
+ }
5549
+ catch (error) {
5550
+ this.log("MutationObserver setup failed (Stripe Payment Link sync):", error);
5551
+ }
5552
+ this.log("Stripe Payment Link auto-decoration active for:", Array.from(hosts));
5553
+ }
5386
5554
  /**
5387
5555
  * Create event payload
5388
5556
  */
@@ -5465,7 +5633,7 @@ class Datalyr {
5465
5633
  // SDK metadata. MUST stay in sync with package.json "version" — the build guard
5466
5634
  // (scripts/check-bundle.js, run by build:check) fails the build if the bundle's
5467
5635
  // sdk_version doesn't match package.json. (FSR-103)
5468
- sdk_version: '1.7.3',
5636
+ sdk_version: '1.7.4',
5469
5637
  sdk_name: 'datalyr-web-sdk'
5470
5638
  };
5471
5639
  return payload;
@@ -5686,6 +5854,10 @@ class Datalyr {
5686
5854
  this.outboundDisposer();
5687
5855
  this.outboundDisposer = undefined;
5688
5856
  }
5857
+ if (this.stripeLinksDisposer) {
5858
+ this.stripeLinksDisposer();
5859
+ this.stripeLinksDisposer = undefined;
5860
+ }
5689
5861
  // Clean up queue
5690
5862
  if (this.queue) {
5691
5863
  this.queue.destroy();