@datalyr/web 1.7.6 → 1.7.9
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/README.md +542 -476
- package/dist/attribution-lyr.test.d.ts +2 -0
- package/dist/attribution-lyr.test.d.ts.map +1 -0
- package/dist/attribution-oppref.test.d.ts +2 -0
- package/dist/attribution-oppref.test.d.ts.map +1 -0
- package/dist/attribution.d.ts.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/container.d.ts +7 -3
- package/dist/container.d.ts.map +1 -1
- package/dist/container.test.d.ts +2 -0
- package/dist/container.test.d.ts.map +1 -0
- package/dist/datalyr.cjs.js +816 -51
- package/dist/datalyr.cjs.js.map +1 -1
- package/dist/datalyr.esm.js +816 -52
- package/dist/datalyr.esm.js.map +1 -1
- package/dist/datalyr.esm.min.js +1 -1
- package/dist/datalyr.esm.min.js.map +1 -1
- package/dist/datalyr.js +816 -51
- package/dist/datalyr.js.map +1 -1
- package/dist/datalyr.min.js +1 -1
- package/dist/datalyr.min.js.map +1 -1
- package/dist/identify-dedupe.test.d.ts +2 -0
- package/dist/identify-dedupe.test.d.ts.map +1 -0
- package/dist/identity-pii.test.d.ts +2 -0
- package/dist/identity-pii.test.d.ts.map +1 -0
- package/dist/identity.d.ts +46 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/index.d.ts +68 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.test.d.ts +3 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/queue-403.test.d.ts +2 -0
- package/dist/queue-403.test.d.ts.map +1 -0
- package/dist/queue.d.ts +17 -0
- package/dist/queue.d.ts.map +1 -1
- package/dist/storage-migration.test.d.ts +2 -0
- package/dist/storage-migration.test.d.ts.map +1 -0
- package/dist/storage.d.ts.map +1 -1
- package/dist/stripe-session.d.ts +75 -0
- package/dist/stripe-session.d.ts.map +1 -0
- package/dist/stripe-session.test.d.ts +2 -0
- package/dist/stripe-session.test.d.ts.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/datalyr.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @datalyr/web v1.7.
|
|
2
|
+
* @datalyr/web v1.7.9
|
|
3
3
|
* Datalyr Web SDK - Modern attribution tracking for web applications
|
|
4
4
|
* (c) 2026 Datalyr Inc.
|
|
5
5
|
* Released under the MIT License
|
|
@@ -419,8 +419,23 @@ class SafeStorage {
|
|
|
419
419
|
// Get value from legacy key
|
|
420
420
|
const value = this.storage.getItem(legacyKey);
|
|
421
421
|
if (value) {
|
|
422
|
-
//
|
|
423
|
-
|
|
422
|
+
// WEB-29: strip ONLY the leading '__'.
|
|
423
|
+
//
|
|
424
|
+
// This used to strip '__dl_', producing 'dl_anonymous_id' — but that
|
|
425
|
+
// is the LOGICAL key callers pass to get(), not the key it reads.
|
|
426
|
+
// get() prepends this.prefix ('dl_'), so the value actually lives at
|
|
427
|
+
// 'dl_dl_anonymous_id'. The migration therefore wrote to an address
|
|
428
|
+
// nothing reads AND then deleted the legacy key below, destroying
|
|
429
|
+
// the visitor's id instead of migrating it: a silent visitor reset
|
|
430
|
+
// and attribution loss for anyone still on the old prefix.
|
|
431
|
+
//
|
|
432
|
+
// legacy real key : __dl_dl_anonymous_id
|
|
433
|
+
// current real key: dl_dl_anonymous_id ← strip '__'
|
|
434
|
+
// logical key : dl_anonymous_id ← what get() is called with
|
|
435
|
+
//
|
|
436
|
+
// NOTE: the double 'dl_dl_' looks like a typo and is not. Renaming
|
|
437
|
+
// the prefix would orphan every stored id across the install base.
|
|
438
|
+
const actualKey = legacyKey.slice('__'.length); // '__dl_dl_anonymous_id' -> 'dl_dl_anonymous_id'
|
|
424
439
|
// Only migrate if new key doesn't already exist (don't overwrite newer data)
|
|
425
440
|
if (!this.storage.getItem(actualKey)) {
|
|
426
441
|
this.storage.setItem(actualKey, value);
|
|
@@ -1093,6 +1108,9 @@ class IdentityManager {
|
|
|
1093
1108
|
constructor(options = {}) {
|
|
1094
1109
|
this.userId = null;
|
|
1095
1110
|
this.sessionId = null;
|
|
1111
|
+
/** WEB-26: bumps whenever the persisted identity changes, so an in-flight
|
|
1112
|
+
* encrypted write can tell it has been superseded. */
|
|
1113
|
+
this.piiGeneration = 0;
|
|
1096
1114
|
this.persistNewId = options.persistNewId !== false;
|
|
1097
1115
|
this.anonymousId = this.getOrCreateAnonymousId();
|
|
1098
1116
|
this.userId = this.getStoredUserId();
|
|
@@ -1219,6 +1237,126 @@ class IdentityManager {
|
|
|
1219
1237
|
// corrupted by JSON.parse — both would fragment identity from the second page on.
|
|
1220
1238
|
return storage.getString('dl_user_id');
|
|
1221
1239
|
}
|
|
1240
|
+
/**
|
|
1241
|
+
* WEB-22 — is this user id personally identifying, and therefore not
|
|
1242
|
+
* something we may write to localStorage in the clear?
|
|
1243
|
+
*
|
|
1244
|
+
* The auto-identify path calls `identify(email, { email })`, so the raw
|
|
1245
|
+
* address became the `user_id` and was written unencrypted to `dl_user_id`.
|
|
1246
|
+
* Measured 2026-07-25: on the four workspaces using auto-identify, **every**
|
|
1247
|
+
* event carrying a `user_id` had an email in that column (20,502 / 20,502 on
|
|
1248
|
+
* `f6260736` over 7 days) — while the surrounding code went to real lengths to
|
|
1249
|
+
* encrypt the *same* address in `dl_auto_identified_email`.
|
|
1250
|
+
*
|
|
1251
|
+
* Deliberately narrow: an email is the case that actually occurs and the one
|
|
1252
|
+
* the SDK itself creates. Opaque application ids — the overwhelming majority —
|
|
1253
|
+
* keep the existing fast, synchronous, plaintext path with no behaviour change.
|
|
1254
|
+
*/
|
|
1255
|
+
static looksLikePII(userId) {
|
|
1256
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(userId);
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* Persist the user id, encrypting it when it is PII.
|
|
1260
|
+
*
|
|
1261
|
+
* Three-way, and the ordering matters:
|
|
1262
|
+
* - not PII → plaintext `dl_user_id`, exactly as before (sync, always works)
|
|
1263
|
+
* - PII + crypto → encrypted `dl_user_id_pii`, and the plaintext key is REMOVED
|
|
1264
|
+
* - PII, no crypto → memory only, and both keys removed
|
|
1265
|
+
*
|
|
1266
|
+
* That last branch is the deliberate trade. `crypto.subtle` is absent on
|
|
1267
|
+
* http:// and old browsers, and index.ts is explicit that encryption is for
|
|
1268
|
+
* PII-at-rest only and "must NOT gate event delivery". So we never block an
|
|
1269
|
+
* event — `this.userId` is set in memory first and every event still carries
|
|
1270
|
+
* it — we only decline to *persist* an address we cannot protect. The cost is
|
|
1271
|
+
* that a returning visitor on an insecure origin is re-identified by the next
|
|
1272
|
+
* `identify()` call rather than restored from storage; the alternative is
|
|
1273
|
+
* writing their email to disk in the clear, which is what this fixes.
|
|
1274
|
+
*/
|
|
1275
|
+
persistUserId(userId) {
|
|
1276
|
+
if (!IdentityManager.looksLikePII(userId)) {
|
|
1277
|
+
storage.set('dl_user_id', userId);
|
|
1278
|
+
// WEB-26: a previous PII user's encrypted id must not linger once an
|
|
1279
|
+
// opaque id takes over. Reachable when identify('opaque') runs before
|
|
1280
|
+
// hydrateEncryptedUserId() has populated this.userId, so index.ts sees no
|
|
1281
|
+
// current user and skips its account-switch reset().
|
|
1282
|
+
this.piiGeneration += 1;
|
|
1283
|
+
storage.remove('dl_user_id_pii');
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
// Never leave a plaintext copy behind — including one written by a previous
|
|
1287
|
+
// SDK version before this fix existed.
|
|
1288
|
+
storage.remove('dl_user_id');
|
|
1289
|
+
// WEB-26: the encrypted write is async, and reset()/optOut()/consent
|
|
1290
|
+
// withdrawal are not. Without a generation guard this sequence resurrects a
|
|
1291
|
+
// logged-out user's email: identify(email) → write pending →
|
|
1292
|
+
// remove('dl_user_id_pii') deletes NOTHING because the write has not landed
|
|
1293
|
+
// → the write lands → next page load hydrates the previous user's address
|
|
1294
|
+
// and every event ships user_id = their email. Stamp the write and discard
|
|
1295
|
+
// it if the identity moved on while it was in flight.
|
|
1296
|
+
const generation = ++this.piiGeneration;
|
|
1297
|
+
storage.setEncrypted('dl_user_id_pii', userId)
|
|
1298
|
+
.then(() => {
|
|
1299
|
+
if (generation !== this.piiGeneration)
|
|
1300
|
+
storage.remove('dl_user_id_pii');
|
|
1301
|
+
})
|
|
1302
|
+
.catch(() => {
|
|
1303
|
+
// No crypto.subtle (http://, legacy browser) or a storage failure. Drop
|
|
1304
|
+
// the at-rest copy rather than falling back to plaintext PII — but only
|
|
1305
|
+
// if this write is still the current one, or we would delete a newer
|
|
1306
|
+
// user's successfully-written value.
|
|
1307
|
+
if (generation === this.piiGeneration)
|
|
1308
|
+
storage.remove('dl_user_id_pii');
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Restore a PII user id that was persisted encrypted. Async by necessity, so
|
|
1313
|
+
* it cannot run in the constructor; index.ts calls it during init, right after
|
|
1314
|
+
* `dataEncryption.initialize()`.
|
|
1315
|
+
*
|
|
1316
|
+
* Never overwrites an id already established this page load — an explicit
|
|
1317
|
+
* `identify()` that has already run is fresher than anything on disk.
|
|
1318
|
+
*/
|
|
1319
|
+
hydrateEncryptedUserId() {
|
|
1320
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1321
|
+
// WEB-26 (a) — migrate a plaintext address written by <= 1.7.7.
|
|
1322
|
+
//
|
|
1323
|
+
// persistUserId() only removes `dl_user_id` when identify() runs again, and
|
|
1324
|
+
// for the exact population this fix targets it never does:
|
|
1325
|
+
// AutoIdentifyManager.initialize() returns early while
|
|
1326
|
+
// `dl_auto_identified_email` exists (auto-identify.ts:88), so the callback
|
|
1327
|
+
// never fires. Without this the raw email would sit in localStorage forever
|
|
1328
|
+
// on every already-auto-identified browser.
|
|
1329
|
+
const legacy = storage.getString('dl_user_id');
|
|
1330
|
+
if (legacy && IdentityManager.looksLikePII(legacy)) {
|
|
1331
|
+
if (!this.userId)
|
|
1332
|
+
this.userId = legacy;
|
|
1333
|
+
this.persistUserId(legacy);
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
// WEB-26 (b) — recover a write that was dropped because encryption was not
|
|
1337
|
+
// keyed yet. index.ts sets `initialized = true` before initializeAsync()
|
|
1338
|
+
// finishes, so the documented `init(); identify(email)` pattern can reach
|
|
1339
|
+
// persistUserId() before dataEncryption has a key; that write rejects and
|
|
1340
|
+
// nothing is persisted. By here the key exists, so retry once.
|
|
1341
|
+
if (this.userId && IdentityManager.looksLikePII(this.userId)) {
|
|
1342
|
+
const alreadyStored = yield storage.getEncrypted('dl_user_id_pii', null);
|
|
1343
|
+
if (!alreadyStored)
|
|
1344
|
+
this.persistUserId(this.userId);
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (this.userId)
|
|
1348
|
+
return;
|
|
1349
|
+
try {
|
|
1350
|
+
const stored = yield storage.getEncrypted('dl_user_id_pii', null);
|
|
1351
|
+
if (typeof stored === 'string' && stored && !this.userId) {
|
|
1352
|
+
this.userId = stored;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
catch (_a) {
|
|
1356
|
+
// Undecryptable (rotated key, corrupt blob) — stay anonymous.
|
|
1357
|
+
}
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1222
1360
|
/**
|
|
1223
1361
|
* Get the anonymous ID
|
|
1224
1362
|
*/
|
|
@@ -1267,8 +1405,9 @@ class IdentityManager {
|
|
|
1267
1405
|
}
|
|
1268
1406
|
const previousUserId = this.userId;
|
|
1269
1407
|
this.userId = userId;
|
|
1270
|
-
// Persist for future sessions
|
|
1271
|
-
|
|
1408
|
+
// Persist for future sessions. WEB-22: encrypted when the id is PII (the
|
|
1409
|
+
// auto-identify path makes it the raw email); plaintext otherwise.
|
|
1410
|
+
this.persistUserId(userId);
|
|
1272
1411
|
// Return identity link data (will be sent as $identify event)
|
|
1273
1412
|
return {
|
|
1274
1413
|
anonymous_id: this.anonymousId,
|
|
@@ -1291,7 +1430,7 @@ class IdentityManager {
|
|
|
1291
1430
|
// Update current user ID if aliasing to current anonymous ID
|
|
1292
1431
|
if (!previousId || previousId === this.anonymousId) {
|
|
1293
1432
|
this.userId = userId;
|
|
1294
|
-
|
|
1433
|
+
this.persistUserId(userId);
|
|
1295
1434
|
}
|
|
1296
1435
|
return aliasData;
|
|
1297
1436
|
}
|
|
@@ -1301,7 +1440,12 @@ class IdentityManager {
|
|
|
1301
1440
|
*/
|
|
1302
1441
|
reset() {
|
|
1303
1442
|
this.userId = null;
|
|
1443
|
+
// WEB-26: invalidate any encrypted write still in flight.
|
|
1444
|
+
this.piiGeneration += 1;
|
|
1304
1445
|
storage.remove('dl_user_id');
|
|
1446
|
+
// WEB-22: the encrypted PII copy must go too, or a logout would leave the
|
|
1447
|
+
// previous user's email at rest.
|
|
1448
|
+
storage.remove('dl_user_id_pii');
|
|
1305
1449
|
storage.remove('dl_user_traits');
|
|
1306
1450
|
// Generate new anonymous ID for privacy
|
|
1307
1451
|
this.anonymousId = `anon_${generateUUID()}`;
|
|
@@ -1628,6 +1772,12 @@ class AttributionManager {
|
|
|
1628
1772
|
'gbraid', // Google Ads (iOS)
|
|
1629
1773
|
'wbraid', // Google Ads (web)
|
|
1630
1774
|
'ttclid', // TikTok
|
|
1775
|
+
// WEB-28: OpenAI Ads. Captured by iOS and React Native since launch but
|
|
1776
|
+
// never by the web SDK, so every OpenAI Ads *web* conversion delivered with
|
|
1777
|
+
// attribution_type: none unless the customer passed the value by hand.
|
|
1778
|
+
// Placed after the Google block to match the mobile SDKs' ordering — the
|
|
1779
|
+
// first click id present wins, and this must not outrank fbclid/gclid.
|
|
1780
|
+
'oppref', // OpenAI Ads
|
|
1631
1781
|
'msclkid', // Microsoft/Bing
|
|
1632
1782
|
'twclid', // Twitter/X
|
|
1633
1783
|
'li_fat_id', // LinkedIn
|
|
@@ -1976,8 +2126,21 @@ class AttributionManager {
|
|
|
1976
2126
|
// The old `hasCurrentAttribution` (truthy because source is always at least 'direct')
|
|
1977
2127
|
// made the fallback below dead code AND caused storeLastTouch to overwrite a real
|
|
1978
2128
|
// paid last-touch with 'direct'/'none' on the next internal navigation. (WEB NEW-1)
|
|
2129
|
+
// `lyr` counts as a real signal. It is the Datalyr tracking-link tag — the one
|
|
2130
|
+
// attribution parameter the product itself tells customers to put on a URL — and
|
|
2131
|
+
// omitting it meant a bare `?lyr=X` landing (no UTMs, no click id) scored FALSE:
|
|
2132
|
+
// the fallback below then replaced `current` wholesale with the stored touch,
|
|
2133
|
+
// discarding the tag that had just been parsed, and neither store* call ran, so it
|
|
2134
|
+
// never persisted either. Measured 2026-07-25 on the one workspace whose links are
|
|
2135
|
+
// bare: 96 landing URLs carried `lyr=`, 93 events kept it — 3 lost (3.1%), all of
|
|
2136
|
+
// them returning visitors (a first-time visitor has no stored touch to be replaced
|
|
2137
|
+
// by, which is the only reason the number is 3 and not 96). The edge worker's own
|
|
2138
|
+
// `buildDestination` emits exactly this URL shape when a link defines no UTMs.
|
|
2139
|
+
// iOS (AttributionManager.swift) and React Native persist `lyr` unconditionally;
|
|
2140
|
+
// this brings web into line.
|
|
1979
2141
|
const hasRealAttribution = !!(current.clickId ||
|
|
1980
2142
|
current.campaign ||
|
|
2143
|
+
current.lyr ||
|
|
1981
2144
|
(current.source && current.source !== 'direct'));
|
|
1982
2145
|
// Direct / internal navigation: fall back to persisted attribution so the event
|
|
1983
2146
|
// isn't mis-attributed to 'direct'. FSR-48: prefer the LAST touch (the most recent
|
|
@@ -2177,15 +2340,20 @@ class AttributionManager {
|
|
|
2177
2340
|
const DEFAULT_CRITICAL_EVENTS = ['purchase', 'signup', 'subscribe', 'lead', 'conversion'];
|
|
2178
2341
|
// Default high priority events that use faster batching
|
|
2179
2342
|
const DEFAULT_HIGH_PRIORITY_EVENTS = ['add_to_cart', 'begin_checkout', 'view_item', 'search'];
|
|
2180
|
-
//
|
|
2181
|
-
//
|
|
2182
|
-
//
|
|
2343
|
+
// WEB-23: how long to stop sending after a 403 (origin not allowed). Long enough not to
|
|
2344
|
+
// hammer a rejecting origin, short enough that a server-side config fix is picked up
|
|
2345
|
+
// within the same visit. A 403 carries no Retry-After, so this is fixed.
|
|
2346
|
+
const ORIGIN_REJECTED_BACKOFF_MS = 5 * 60 * 1000;
|
|
2347
|
+
// Backpressure, not failure — routed to the offline queue WITHOUT retrying (which would
|
|
2348
|
+
// storm an already-overloaded server) and gated behind rateLimitedUntil. Raised for a 429
|
|
2349
|
+
// and, since WEB-23, for a 403: both mean "not now", not "never". See sendBatch.
|
|
2183
2350
|
class RateLimitError extends Error {
|
|
2184
2351
|
}
|
|
2185
|
-
// A permanent (non-retryable) failure — a 4xx other than 408/429
|
|
2186
|
-
//
|
|
2187
|
-
// queue forever and hammers ingest. Tagged so the send paths DROP the batch
|
|
2188
|
-
// unshifting it back to the head. (FSR-55
|
|
2352
|
+
// A permanent (non-retryable) failure — a 4xx other than 403/408/429, i.e. an invalid
|
|
2353
|
+
// event shape (400) or bad auth (401). Retrying or parking it just head-of-line-blocks
|
|
2354
|
+
// the offline queue forever and hammers ingest. Tagged so the send paths DROP the batch
|
|
2355
|
+
// instead of unshifting it back to the head. (FSR-55, narrowed by WEB-23 — a 403 is
|
|
2356
|
+
// server *configuration* state and does change, so it is no longer in this class.)
|
|
2189
2357
|
class PermanentError extends Error {
|
|
2190
2358
|
constructor(status, message) {
|
|
2191
2359
|
super(message || `HTTP ${status}`);
|
|
@@ -2208,6 +2376,11 @@ class EventQueue {
|
|
|
2208
2376
|
this.inFlight = []; // FIXED (WEB-3): batch currently in _flight's keepalive fetch — forceFlush must NOT re-beacon it
|
|
2209
2377
|
this.enabled = true; // FIXED (consent): when false (opt-out / withdrawn analytics consent) all enqueue/flush/drain is a no-op
|
|
2210
2378
|
this.rateLimitedUntil = 0; // FIXED (429): skip flush/drain until the server's Retry-After window passes
|
|
2379
|
+
// WEB-23: permanent drops used to be invisible — a debug-gated log line and
|
|
2380
|
+
// nothing else, so 11 days of 403s looked identical to a healthy queue. Counted
|
|
2381
|
+
// here and exposed via getStats() so a drop is at least observable.
|
|
2382
|
+
this.droppedEventCount = 0;
|
|
2383
|
+
this.lastDropStatus = null;
|
|
2211
2384
|
// Coerce numeric config to sane values. The old `config.X || default` both turned a
|
|
2212
2385
|
// legitimate 0 into the default AND let invalid values through — a NEGATIVE batchSize
|
|
2213
2386
|
// made processOfflineQueue's `splice(0, -1)` loop forever and FREEZE the host tab.
|
|
@@ -2433,7 +2606,12 @@ class EventQueue {
|
|
|
2433
2606
|
// FSR-55: don't park a permanently-rejected batch — it would never succeed and
|
|
2434
2607
|
// would block the offline drain. Drop it.
|
|
2435
2608
|
if (error instanceof PermanentError) {
|
|
2436
|
-
this.
|
|
2609
|
+
this.droppedEventCount += events.length;
|
|
2610
|
+
this.lastDropStatus = error.status;
|
|
2611
|
+
// console.warn, not this.log: a silent drop is the failure mode that let the
|
|
2612
|
+
// 2026-07-13 outage run for 11 days. This must be visible without debug mode.
|
|
2613
|
+
console.warn(`[Datalyr Queue] Dropped ${events.length} events — permanent error ${error.status}. ` +
|
|
2614
|
+
`${this.droppedEventCount} dropped this session.`);
|
|
2437
2615
|
}
|
|
2438
2616
|
else {
|
|
2439
2617
|
this.moveToOfflineQueue(events);
|
|
@@ -2495,9 +2673,24 @@ class EventQueue {
|
|
|
2495
2673
|
this.log(`Rate limited; backing off ${retryAfter}s`);
|
|
2496
2674
|
throw new RateLimitError('Rate limited (429)');
|
|
2497
2675
|
}
|
|
2498
|
-
//
|
|
2499
|
-
//
|
|
2500
|
-
//
|
|
2676
|
+
// WEB-23: a 403 is NOT permanent — it is server *configuration* state, and it
|
|
2677
|
+
// changes. On 2026-07-13 an allowed_origins regression made ingest reject three
|
|
2678
|
+
// workspaces with 403; the config was fixed 11 days later, but every event sent
|
|
2679
|
+
// in between had already been dropped on the floor by the FSR-55 rule below.
|
|
2680
|
+
// Eleven days of data, unrecoverable, with only a debug log to show for it.
|
|
2681
|
+
//
|
|
2682
|
+
// Treated like 429 instead: park the events offline behind a backoff window, so
|
|
2683
|
+
// we neither hammer a rejecting origin (FSR-55's real concern) nor discard data
|
|
2684
|
+
// that would have been accepted an hour later. The window is fixed because a 403
|
|
2685
|
+
// carries no Retry-After.
|
|
2686
|
+
if (response.status === 403) {
|
|
2687
|
+
this.rateLimitedUntil = Date.now() + ORIGIN_REJECTED_BACKOFF_MS;
|
|
2688
|
+
this.log(`Origin rejected (403); backing off ${ORIGIN_REJECTED_BACKOFF_MS / 1000}s and parking events`);
|
|
2689
|
+
throw new RateLimitError('Origin rejected (403)');
|
|
2690
|
+
}
|
|
2691
|
+
// FSR-55: a 4xx other than 403/408 (timeout) is PERMANENT — invalid event shape
|
|
2692
|
+
// (400) or auth (401). Retrying / parking it just blocks the queue and hammers
|
|
2693
|
+
// ingest forever. Tag it so the caller drops the batch.
|
|
2501
2694
|
if (response.status >= 400 && response.status < 500 && response.status !== 408) {
|
|
2502
2695
|
throw new PermanentError(response.status, `HTTP ${response.status}: ${response.statusText}`);
|
|
2503
2696
|
}
|
|
@@ -2584,9 +2777,25 @@ class EventQueue {
|
|
|
2584
2777
|
// purge. Gating here (a persistence sink, not just the send sinks) closes it.
|
|
2585
2778
|
if (!this.enabled)
|
|
2586
2779
|
return;
|
|
2587
|
-
//
|
|
2780
|
+
// WEB-24. This early return used to `return` outright — losing the batch, because
|
|
2781
|
+
// _flush() has ALREADY spliced these events out of the live queue before calling
|
|
2782
|
+
// here, so they would exist in neither queue.
|
|
2783
|
+
//
|
|
2784
|
+
// Verified 2026-07-25 that the loss is NOT currently reachable: this method is the
|
|
2785
|
+
// only user of offlineQueueLock, its critical section is fully synchronous (push,
|
|
2786
|
+
// splice, then a synchronous saveOfflineQueue → storage.set), and JS is
|
|
2787
|
+
// single-threaded — so the flag can never be observed set on entry. The path is
|
|
2788
|
+
// dead code today, and the review's "drops the batch on lock contention" is REFUTED
|
|
2789
|
+
// as a live loss path.
|
|
2790
|
+
//
|
|
2791
|
+
// It is kept and made safe anyway, because it is one `await` away from being live:
|
|
2792
|
+
// the moment saveOfflineQueue becomes async (IndexedDB, encrypted-at-rest storage,
|
|
2793
|
+
// anything), contention becomes real and silent data loss returns instantly.
|
|
2794
|
+
// Enqueue first, then bail — the events are never dropped either way.
|
|
2588
2795
|
if (this.offlineQueueLock) {
|
|
2589
|
-
console.warn('[Datalyr Queue] Offline queue
|
|
2796
|
+
console.warn('[Datalyr Queue] Offline queue busy; buffering events without persisting');
|
|
2797
|
+
if (events)
|
|
2798
|
+
this.offlineQueue.push(...events);
|
|
2590
2799
|
return;
|
|
2591
2800
|
}
|
|
2592
2801
|
// Acquire lock
|
|
@@ -2605,6 +2814,10 @@ class EventQueue {
|
|
|
2605
2814
|
if (this.offlineQueue.length > this.config.maxOfflineQueueSize) {
|
|
2606
2815
|
const excess = this.offlineQueue.length - this.config.maxOfflineQueueSize;
|
|
2607
2816
|
this.offlineQueue.splice(0, excess); // Remove oldest events
|
|
2817
|
+
// WEB-27: overflow is data loss too, and a sustained 403/park loop is
|
|
2818
|
+
// exactly what produces it. Count it rather than trimming silently.
|
|
2819
|
+
this.droppedEventCount += excess;
|
|
2820
|
+
console.warn(`[Datalyr Queue] Offline queue full — dropped ${excess} oldest event(s).`);
|
|
2608
2821
|
}
|
|
2609
2822
|
this.saveOfflineQueue();
|
|
2610
2823
|
}
|
|
@@ -2713,7 +2926,15 @@ class EventQueue {
|
|
|
2713
2926
|
// that would block every event behind it forever and hammer ingest every tick.
|
|
2714
2927
|
// Drop it (already spliced off the front) and keep draining the rest.
|
|
2715
2928
|
if (error instanceof PermanentError) {
|
|
2716
|
-
|
|
2929
|
+
// WEB-27: count and surface these too. WEB-23's whole effect is to
|
|
2930
|
+
// PARK more batches offline, so the drain — not the live flush — is
|
|
2931
|
+
// where drops now predominantly happen. Counting only _flush left the
|
|
2932
|
+
// dominant loss path invisible, which is the exact failure mode the
|
|
2933
|
+
// counter was added to end.
|
|
2934
|
+
this.droppedEventCount += batch.length;
|
|
2935
|
+
this.lastDropStatus = error.status;
|
|
2936
|
+
console.warn(`[Datalyr Queue] Dropped ${batch.length} offline events — permanent error ${error.status}. ` +
|
|
2937
|
+
`${this.droppedEventCount} dropped this session.`);
|
|
2717
2938
|
this.saveOfflineQueue();
|
|
2718
2939
|
continue;
|
|
2719
2940
|
}
|
|
@@ -2746,6 +2967,23 @@ class EventQueue {
|
|
|
2746
2967
|
getOfflineQueueSize() {
|
|
2747
2968
|
return this.offlineQueue.length;
|
|
2748
2969
|
}
|
|
2970
|
+
/**
|
|
2971
|
+
* WEB-23: queue health, including events the SDK gave up on.
|
|
2972
|
+
*
|
|
2973
|
+
* `droppedEvents` is the number this session discarded as permanently rejected
|
|
2974
|
+
* (a 400/401). It exists because a silent drop is precisely the failure mode
|
|
2975
|
+
* that let the 2026-07-13 `allowed_origins` outage run for eleven days looking
|
|
2976
|
+
* exactly like a healthy queue. A non-zero value here means data was lost.
|
|
2977
|
+
*/
|
|
2978
|
+
getStats() {
|
|
2979
|
+
return {
|
|
2980
|
+
queued: this.queue.length,
|
|
2981
|
+
offline: this.offlineQueue.length,
|
|
2982
|
+
droppedEvents: this.droppedEventCount,
|
|
2983
|
+
lastDropStatus: this.lastDropStatus,
|
|
2984
|
+
backoffUntil: this.rateLimitedUntil,
|
|
2985
|
+
};
|
|
2986
|
+
}
|
|
2749
2987
|
/**
|
|
2750
2988
|
* Get network status
|
|
2751
2989
|
*/
|
|
@@ -3453,12 +3691,10 @@ class ContainerManager {
|
|
|
3453
3691
|
img.dataset.datalyrPixel = script.id;
|
|
3454
3692
|
document.body.appendChild(img);
|
|
3455
3693
|
}
|
|
3456
|
-
/**
|
|
3457
|
-
* Initialize third-party pixels (Meta, Google, TikTok)
|
|
3458
|
-
*/
|
|
3694
|
+
/** Initialize configured third-party pixels in the merchant's page context. */
|
|
3459
3695
|
initializePixels() {
|
|
3460
3696
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3461
|
-
var _a, _b, _c;
|
|
3697
|
+
var _a, _b, _c, _e;
|
|
3462
3698
|
if (!this.pixels)
|
|
3463
3699
|
return;
|
|
3464
3700
|
// Initialize Meta Pixel
|
|
@@ -3473,8 +3709,58 @@ class ContainerManager {
|
|
|
3473
3709
|
if (((_c = this.pixels.tiktok) === null || _c === void 0 ? void 0 : _c.enabled) && this.pixels.tiktok.pixel_id) {
|
|
3474
3710
|
this.initializeTikTokPixel(this.pixels.tiktok);
|
|
3475
3711
|
}
|
|
3712
|
+
// The Whop Pixel needs only the merchant's public company ID. Loading it
|
|
3713
|
+
// here lets Whop link this external-site visitor to a later hosted or
|
|
3714
|
+
// embedded Whop checkout without a Datalyr visitor_id in checkout metadata.
|
|
3715
|
+
if (((_e = this.pixels.whop) === null || _e === void 0 ? void 0 : _e.enabled) && this.pixels.whop.company_id) {
|
|
3716
|
+
this.initializeWhopPixel(this.pixels.whop);
|
|
3717
|
+
}
|
|
3476
3718
|
});
|
|
3477
3719
|
}
|
|
3720
|
+
/** Initialize Whop's official first-party attribution pixel. */
|
|
3721
|
+
initializeWhopPixel(config) {
|
|
3722
|
+
var _a, _b;
|
|
3723
|
+
try {
|
|
3724
|
+
const host = window;
|
|
3725
|
+
if (!host.whop) {
|
|
3726
|
+
const queue = host.whop = {
|
|
3727
|
+
q: [],
|
|
3728
|
+
t: Date.now(),
|
|
3729
|
+
s: [],
|
|
3730
|
+
o: 'https://t.whop.tw',
|
|
3731
|
+
track: function (...args) {
|
|
3732
|
+
queue.q.push([Date.now(), ...args]);
|
|
3733
|
+
},
|
|
3734
|
+
setScope: function (...args) {
|
|
3735
|
+
queue.s = args.filter((value) => typeof value === 'string');
|
|
3736
|
+
queue.q.push([Date.now(), 'setScope', ...queue.s]);
|
|
3737
|
+
},
|
|
3738
|
+
scope: function (...scope) {
|
|
3739
|
+
return {
|
|
3740
|
+
track: function (...args) {
|
|
3741
|
+
queue.q.push([Date.now(), ...args, { __scope: scope }]);
|
|
3742
|
+
},
|
|
3743
|
+
};
|
|
3744
|
+
},
|
|
3745
|
+
};
|
|
3746
|
+
const script = document.createElement('script');
|
|
3747
|
+
script.async = true;
|
|
3748
|
+
script.src = 'https://t.whop.tw/s.js';
|
|
3749
|
+
const firstScript = document.getElementsByTagName('script')[0];
|
|
3750
|
+
(_a = firstScript === null || firstScript === void 0 ? void 0 : firstScript.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(script, firstScript);
|
|
3751
|
+
if (!firstScript)
|
|
3752
|
+
document.head.appendChild(script);
|
|
3753
|
+
}
|
|
3754
|
+
if (typeof ((_b = host.whop) === null || _b === void 0 ? void 0 : _b.setScope) !== 'function') {
|
|
3755
|
+
throw new Error('Existing window.whop does not expose setScope');
|
|
3756
|
+
}
|
|
3757
|
+
host.whop.setScope(config.company_id);
|
|
3758
|
+
this.log('Whop Pixel initialized:', config.company_id);
|
|
3759
|
+
}
|
|
3760
|
+
catch (error) {
|
|
3761
|
+
this.log('Error initializing Whop Pixel:', error);
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3478
3764
|
/**
|
|
3479
3765
|
* Initialize Meta (Facebook) Pixel
|
|
3480
3766
|
*
|
|
@@ -3630,7 +3916,7 @@ class ContainerManager {
|
|
|
3630
3916
|
* Track event to all initialized pixels
|
|
3631
3917
|
*/
|
|
3632
3918
|
trackToPixels(eventName, properties = {}, eventId) {
|
|
3633
|
-
var _a, _b, _c, _e, _f, _g, _h, _j, _k, _l;
|
|
3919
|
+
var _a, _b, _c, _e, _f, _g, _h, _j, _k, _l, _m, _p;
|
|
3634
3920
|
// Datalyr-internal events ($identify, $group, $alias, $auto_identify,
|
|
3635
3921
|
// $app_download_click, …) are not conversions — never forward them to ad
|
|
3636
3922
|
// pixels. (Previously they fired as noise custom events with the $ stripped.)
|
|
@@ -3727,6 +4013,18 @@ class ContainerManager {
|
|
|
3727
4013
|
this.log('Error tracking TikTok Pixel event:', error);
|
|
3728
4014
|
}
|
|
3729
4015
|
}
|
|
4016
|
+
// Whop records checkout and payment events server-side. Datalyr only sends
|
|
4017
|
+
// page views here, including SPA navigations, so purchases are never doubled.
|
|
4018
|
+
if (((_p = (_m = this.pixels) === null || _m === void 0 ? void 0 : _m.whop) === null || _p === void 0 ? void 0 : _p.enabled) &&
|
|
4019
|
+
window.whop &&
|
|
4020
|
+
(eventName === 'pageview' || eventName === 'page_view')) {
|
|
4021
|
+
try {
|
|
4022
|
+
window.whop.track('page');
|
|
4023
|
+
}
|
|
4024
|
+
catch (error) {
|
|
4025
|
+
this.log('Error tracking Whop Pixel page:', error);
|
|
4026
|
+
}
|
|
4027
|
+
}
|
|
3730
4028
|
}
|
|
3731
4029
|
/**
|
|
3732
4030
|
* Manually trigger a custom script
|
|
@@ -4374,6 +4672,256 @@ class AutoIdentifyManager {
|
|
|
4374
4672
|
// someone else (and feeds that person's email to Meta advanced matching).
|
|
4375
4673
|
AutoIdentifyManager.THIRD_PARTY_EMAIL_FIELD = /(recipient|friend|gift|refer|invite|colleague|coworker|share|to[-_]?email|email[-_]?to|second(ary)?[-_]?email)/i;
|
|
4376
4674
|
|
|
4675
|
+
/**
|
|
4676
|
+
* Stripe Checkout Session capture.
|
|
4677
|
+
*
|
|
4678
|
+
* WHY THIS EXISTS
|
|
4679
|
+
* syncStripePaymentLinks stamps `client_reference_id` onto buy.stripe.com
|
|
4680
|
+
* anchors, and deliberately never onto checkout.stripe.com — a Checkout Session
|
|
4681
|
+
* URL ignores the param because the session already exists server-side. That
|
|
4682
|
+
* leaves the single most common SaaS pattern uncovered: the merchant's backend
|
|
4683
|
+
* creates a Session and the browser is sent to checkout.stripe.com/c/pay/cs_...
|
|
4684
|
+
* The payment then reaches our webhook with no visitor at all.
|
|
4685
|
+
*
|
|
4686
|
+
* Measured 2026-07-25, 30d: of payers arriving via Stripe webhooks, 0.5% (one
|
|
4687
|
+
* tenant, 128/28,187) and 13.3% (another, 13/98) had ANY web pageview on the
|
|
4688
|
+
* same visitor. Not a misconfiguration — no tenant is wired, because being
|
|
4689
|
+
* wired currently requires the merchant to hand-write client_reference_id.
|
|
4690
|
+
*
|
|
4691
|
+
* WHAT THIS DOES
|
|
4692
|
+
* The session id must reach the browser for the redirect to happen at all, so
|
|
4693
|
+
* we observe it rather than trying to inject anything:
|
|
4694
|
+
* 1. fetch/XHR response bodies — `{url}` or `{sessionId}` from the
|
|
4695
|
+
* merchant's own create-session endpoint
|
|
4696
|
+
* 2. anchor clicks + window.open — direct links to checkout.stripe.com
|
|
4697
|
+
* A server-side 302 straight to Stripe never exposes the id to JS and is NOT
|
|
4698
|
+
* covered here; that case falls back to the email bridge.
|
|
4699
|
+
*
|
|
4700
|
+
* WHY THIS IS SAFE TO DEFAULT ON, WHEN autoIdentifyAPI IS NOT
|
|
4701
|
+
* autoIdentifyAPI scans the same traffic for EMAILS, which is why it defaults
|
|
4702
|
+
* off: an admin viewing a customer record gets identified as that customer, and
|
|
4703
|
+
* a wrong email propagates into Meta advanced matching. A `cs_live_...` token is
|
|
4704
|
+
* unambiguous, belongs to exactly one checkout, and is not PII. There is no
|
|
4705
|
+
* mis-identification failure mode to guard against — only the wrapper itself,
|
|
4706
|
+
* which is why every hook below is transparent and failure-isolated.
|
|
4707
|
+
*/
|
|
4708
|
+
/** Stripe Checkout Session ids: cs_live_… / cs_test_… */
|
|
4709
|
+
const SESSION_ID_RE = /cs_(?:live|test)_[A-Za-z0-9]{8,}/;
|
|
4710
|
+
/** Hosts whose URLs carry a session id in the path. Exact match, never substring. */
|
|
4711
|
+
const CHECKOUT_HOSTS = new Set(['checkout.stripe.com']);
|
|
4712
|
+
/**
|
|
4713
|
+
* Response bodies are scanned in full, so cap what we're willing to read. A
|
|
4714
|
+
* create-session response is a few hundred bytes; anything large is not it, and
|
|
4715
|
+
* reading it would cost the merchant's page real memory and main-thread time.
|
|
4716
|
+
*/
|
|
4717
|
+
const MAX_SCAN_BYTES = 65536;
|
|
4718
|
+
/** Bodies worth scanning. Stripe ids arrive as JSON or text, never as binary. */
|
|
4719
|
+
const SCANNABLE_TYPE_RE = /(json|text|javascript)/i;
|
|
4720
|
+
function extractSessionId(text) {
|
|
4721
|
+
if (!text)
|
|
4722
|
+
return null;
|
|
4723
|
+
const m = SESSION_ID_RE.exec(text);
|
|
4724
|
+
return m ? m[0] : null;
|
|
4725
|
+
}
|
|
4726
|
+
/**
|
|
4727
|
+
* True when `href` points at a Stripe-hosted checkout. Uses the URL API and
|
|
4728
|
+
* exact host equality — `a[href*="checkout.stripe.com"]` would match
|
|
4729
|
+
* `evil.com/checkout.stripe.com/x` and `checkout.stripe.com.evil.com`.
|
|
4730
|
+
*/
|
|
4731
|
+
function isCheckoutUrl(href, base) {
|
|
4732
|
+
try {
|
|
4733
|
+
const host = new URL(href, base !== null && base !== void 0 ? base : (typeof window !== 'undefined' ? window.location.href : undefined)).hostname.toLowerCase();
|
|
4734
|
+
return CHECKOUT_HOSTS.has(host);
|
|
4735
|
+
}
|
|
4736
|
+
catch (_a) {
|
|
4737
|
+
return false;
|
|
4738
|
+
}
|
|
4739
|
+
}
|
|
4740
|
+
class StripeSessionWatcher {
|
|
4741
|
+
constructor(options = {}) {
|
|
4742
|
+
var _a, _b;
|
|
4743
|
+
this.seen = new Set();
|
|
4744
|
+
this.disposers = [];
|
|
4745
|
+
this.started = false;
|
|
4746
|
+
this.maxSessions = (_a = options.maxSessions) !== null && _a !== void 0 ? _a : 5;
|
|
4747
|
+
this.maxScanBytes = (_b = options.maxScanBytes) !== null && _b !== void 0 ? _b : MAX_SCAN_BYTES;
|
|
4748
|
+
}
|
|
4749
|
+
start(sink) {
|
|
4750
|
+
if (this.started || typeof window === 'undefined')
|
|
4751
|
+
return;
|
|
4752
|
+
this.started = true;
|
|
4753
|
+
this.sink = sink;
|
|
4754
|
+
this.hookFetch();
|
|
4755
|
+
this.hookXhr();
|
|
4756
|
+
this.hookClicks();
|
|
4757
|
+
this.hookWindowOpen();
|
|
4758
|
+
}
|
|
4759
|
+
stop() {
|
|
4760
|
+
// Restore in reverse so a later wrapper never re-installs an earlier one.
|
|
4761
|
+
for (const dispose of this.disposers.reverse()) {
|
|
4762
|
+
try {
|
|
4763
|
+
dispose();
|
|
4764
|
+
}
|
|
4765
|
+
catch (_a) {
|
|
4766
|
+
/* idempotent */
|
|
4767
|
+
}
|
|
4768
|
+
}
|
|
4769
|
+
this.disposers = [];
|
|
4770
|
+
this.started = false;
|
|
4771
|
+
this.sink = undefined;
|
|
4772
|
+
}
|
|
4773
|
+
/** Report a session id at most once, and never more than maxSessions per page. */
|
|
4774
|
+
emit(sessionId) {
|
|
4775
|
+
var _a;
|
|
4776
|
+
if (!sessionId || this.seen.has(sessionId))
|
|
4777
|
+
return;
|
|
4778
|
+
if (this.seen.size >= this.maxSessions)
|
|
4779
|
+
return;
|
|
4780
|
+
this.seen.add(sessionId);
|
|
4781
|
+
try {
|
|
4782
|
+
(_a = this.sink) === null || _a === void 0 ? void 0 : _a.call(this, sessionId);
|
|
4783
|
+
}
|
|
4784
|
+
catch (_b) {
|
|
4785
|
+
// A throwing sink must never surface inside the merchant's fetch chain.
|
|
4786
|
+
}
|
|
4787
|
+
}
|
|
4788
|
+
/**
|
|
4789
|
+
* Scan a Response WITHOUT disturbing the merchant's own read. `clone()` must
|
|
4790
|
+
* happen synchronously, before the caller can consume the body; the read of
|
|
4791
|
+
* the clone is deliberately not awaited by the caller.
|
|
4792
|
+
*/
|
|
4793
|
+
scanResponse(response) {
|
|
4794
|
+
var _a, _b, _c, _d, _e, _f;
|
|
4795
|
+
try {
|
|
4796
|
+
const type = (_c = (_b = (_a = response.headers) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a, 'content-type')) !== null && _c !== void 0 ? _c : '';
|
|
4797
|
+
if (type && !SCANNABLE_TYPE_RE.test(type))
|
|
4798
|
+
return;
|
|
4799
|
+
const declared = Number((_f = (_e = (_d = response.headers) === null || _d === void 0 ? void 0 : _d.get) === null || _e === void 0 ? void 0 : _e.call(_d, 'content-length')) !== null && _f !== void 0 ? _f : NaN);
|
|
4800
|
+
if (Number.isFinite(declared) && declared > this.maxScanBytes)
|
|
4801
|
+
return;
|
|
4802
|
+
const clone = typeof response.clone === 'function' ? response.clone() : null;
|
|
4803
|
+
if (!clone)
|
|
4804
|
+
return;
|
|
4805
|
+
void clone
|
|
4806
|
+
.text()
|
|
4807
|
+
.then((body) => this.emit(extractSessionId(body.slice(0, this.maxScanBytes))))
|
|
4808
|
+
.catch(() => {
|
|
4809
|
+
/* body already disturbed / opaque response — nothing to do */
|
|
4810
|
+
});
|
|
4811
|
+
}
|
|
4812
|
+
catch (_g) {
|
|
4813
|
+
/* never let observation break the request */
|
|
4814
|
+
}
|
|
4815
|
+
}
|
|
4816
|
+
hookFetch() {
|
|
4817
|
+
if (typeof window.fetch !== 'function')
|
|
4818
|
+
return;
|
|
4819
|
+
const original = window.fetch;
|
|
4820
|
+
const self = this;
|
|
4821
|
+
const patched = function patchedFetch(...args) {
|
|
4822
|
+
const result = original.apply(this !== null && this !== void 0 ? this : window, args);
|
|
4823
|
+
try {
|
|
4824
|
+
// Attach passively: the merchant still gets the original promise, and a
|
|
4825
|
+
// rejection here is theirs to handle, not ours to observe twice.
|
|
4826
|
+
result.then((response) => self.scanResponse(response)).catch(() => { });
|
|
4827
|
+
}
|
|
4828
|
+
catch (_a) {
|
|
4829
|
+
/* ignore */
|
|
4830
|
+
}
|
|
4831
|
+
return result;
|
|
4832
|
+
};
|
|
4833
|
+
window.fetch = patched;
|
|
4834
|
+
this.disposers.push(() => {
|
|
4835
|
+
// Only restore if nobody wrapped us afterwards — clobbering a later
|
|
4836
|
+
// wrapper would silently disable whatever installed it.
|
|
4837
|
+
if (window.fetch === patched)
|
|
4838
|
+
window.fetch = original;
|
|
4839
|
+
});
|
|
4840
|
+
}
|
|
4841
|
+
hookXhr() {
|
|
4842
|
+
if (typeof XMLHttpRequest === 'undefined')
|
|
4843
|
+
return;
|
|
4844
|
+
const proto = XMLHttpRequest.prototype;
|
|
4845
|
+
const originalSend = proto.send;
|
|
4846
|
+
if (typeof originalSend !== 'function')
|
|
4847
|
+
return;
|
|
4848
|
+
const self = this;
|
|
4849
|
+
const patched = function patchedSend(...args) {
|
|
4850
|
+
try {
|
|
4851
|
+
this.addEventListener('load', () => {
|
|
4852
|
+
try {
|
|
4853
|
+
// responseText throws for blob/arraybuffer response types.
|
|
4854
|
+
if (this.responseType !== '' && this.responseType !== 'text')
|
|
4855
|
+
return;
|
|
4856
|
+
const body = this.responseText;
|
|
4857
|
+
if (!body || body.length > self.maxScanBytes)
|
|
4858
|
+
return;
|
|
4859
|
+
self.emit(extractSessionId(body));
|
|
4860
|
+
}
|
|
4861
|
+
catch (_a) {
|
|
4862
|
+
/* ignore */
|
|
4863
|
+
}
|
|
4864
|
+
});
|
|
4865
|
+
}
|
|
4866
|
+
catch (_a) {
|
|
4867
|
+
/* ignore */
|
|
4868
|
+
}
|
|
4869
|
+
return originalSend.apply(this, args);
|
|
4870
|
+
};
|
|
4871
|
+
proto.send = patched;
|
|
4872
|
+
this.disposers.push(() => {
|
|
4873
|
+
if (proto.send === patched)
|
|
4874
|
+
proto.send = originalSend;
|
|
4875
|
+
});
|
|
4876
|
+
}
|
|
4877
|
+
/**
|
|
4878
|
+
* Direct links to checkout.stripe.com. Capture phase so we still see the click
|
|
4879
|
+
* when the merchant's own handler calls stopPropagation().
|
|
4880
|
+
*/
|
|
4881
|
+
hookClicks() {
|
|
4882
|
+
if (typeof document === 'undefined')
|
|
4883
|
+
return;
|
|
4884
|
+
const onClick = (e) => {
|
|
4885
|
+
var _a, _b;
|
|
4886
|
+
try {
|
|
4887
|
+
const target = (_b = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest) === null || _b === void 0 ? void 0 : _b.call(_a, 'a[href]');
|
|
4888
|
+
if (!target || !target.href || !isCheckoutUrl(target.href))
|
|
4889
|
+
return;
|
|
4890
|
+
this.emit(extractSessionId(target.href));
|
|
4891
|
+
}
|
|
4892
|
+
catch (_c) {
|
|
4893
|
+
/* never block navigation */
|
|
4894
|
+
}
|
|
4895
|
+
};
|
|
4896
|
+
document.addEventListener('click', onClick, true);
|
|
4897
|
+
this.disposers.push(() => document.removeEventListener('click', onClick, true));
|
|
4898
|
+
}
|
|
4899
|
+
hookWindowOpen() {
|
|
4900
|
+
if (typeof window.open !== 'function')
|
|
4901
|
+
return;
|
|
4902
|
+
const original = window.open;
|
|
4903
|
+
const self = this;
|
|
4904
|
+
const patched = function patchedOpen(...args) {
|
|
4905
|
+
var _a;
|
|
4906
|
+
try {
|
|
4907
|
+
const url = args[0];
|
|
4908
|
+
const href = typeof url === 'string' ? url : (_a = url === null || url === void 0 ? void 0 : url.toString) === null || _a === void 0 ? void 0 : _a.call(url);
|
|
4909
|
+
if (href && isCheckoutUrl(href))
|
|
4910
|
+
self.emit(extractSessionId(href));
|
|
4911
|
+
}
|
|
4912
|
+
catch (_b) {
|
|
4913
|
+
/* ignore */
|
|
4914
|
+
}
|
|
4915
|
+
return original.apply(this !== null && this !== void 0 ? this : window, args);
|
|
4916
|
+
};
|
|
4917
|
+
window.open = patched;
|
|
4918
|
+
this.disposers.push(() => {
|
|
4919
|
+
if (window.open === patched)
|
|
4920
|
+
window.open = original;
|
|
4921
|
+
});
|
|
4922
|
+
}
|
|
4923
|
+
}
|
|
4924
|
+
|
|
4377
4925
|
/** Keys the remote config is allowed to fill on DatalyrConfig. */
|
|
4378
4926
|
const REMOTE_KEYS = [
|
|
4379
4927
|
'autoIdentify',
|
|
@@ -4381,6 +4929,7 @@ const REMOTE_KEYS = [
|
|
|
4381
4929
|
'autoIdentifyAPI',
|
|
4382
4930
|
'autoIdentifyShopify',
|
|
4383
4931
|
'shopifyCartAttributes',
|
|
4932
|
+
'stripeCheckoutSessions',
|
|
4384
4933
|
'checkoutChampDomains',
|
|
4385
4934
|
'respectGlobalPrivacyControl',
|
|
4386
4935
|
'respectDoNotTrack',
|
|
@@ -4419,6 +4968,16 @@ function applyRemoteConfig(config, remote, explicitKeys) {
|
|
|
4419
4968
|
* Datalyr Web SDK
|
|
4420
4969
|
* Modern attribution tracking for web applications
|
|
4421
4970
|
*/
|
|
4971
|
+
/**
|
|
4972
|
+
* WEB-20. Fingerprint of the last identify() that actually emitted, so a host
|
|
4973
|
+
* app calling identify() on every route change does not re-emit an unchanged
|
|
4974
|
+
* identity. Cleared by reset(). Mirrors iOS `datalyr_last_identity_fingerprint`
|
|
4975
|
+
* and RN `@datalyr/last_identity_fingerprint`.
|
|
4976
|
+
*
|
|
4977
|
+
* Not PII: the stored value is a hash, and it is written through the same
|
|
4978
|
+
* `storage` wrapper as every other key (so it inherits the `dl_` prefix).
|
|
4979
|
+
*/
|
|
4980
|
+
const IDENTIFY_FINGERPRINT_KEY = 'dl_identify_fingerprint';
|
|
4422
4981
|
class Datalyr {
|
|
4423
4982
|
constructor() {
|
|
4424
4983
|
// Keys the caller passed to init() (before built-in defaults were merged).
|
|
@@ -4434,10 +4993,26 @@ class Datalyr {
|
|
|
4434
4993
|
this.errors = [];
|
|
4435
4994
|
this.MAX_ERRORS = 50;
|
|
4436
4995
|
this.lastSpaPath = null; // dedups SPA pageviews (replaceState-on-mount double-fire)
|
|
4996
|
+
// Shopify loads Customer Privacy asynchronously. Keep the initial pageview
|
|
4997
|
+
// pending until initialization is complete and analytics consent is known,
|
|
4998
|
+
// then release it exactly once when consent allows tracking.
|
|
4999
|
+
this.initialPageViewReady = false;
|
|
5000
|
+
this.initialPageViewSent = false;
|
|
4437
5001
|
// FIXED (ISSUE-01): Async initialization promise to prevent race conditions
|
|
4438
5002
|
this.initializationPromise = null;
|
|
4439
5003
|
// Opt-out check moved to init() after cookies configured (Issue #14)
|
|
4440
5004
|
}
|
|
5005
|
+
/**
|
|
5006
|
+
* Return the workspace configured for this instance, or null before init().
|
|
5007
|
+
*
|
|
5008
|
+
* CDN bootstrap uses this public accessor instead of reaching into the
|
|
5009
|
+
* private runtime config when deciding whether an existing global belongs
|
|
5010
|
+
* to the requested workspace.
|
|
5011
|
+
*/
|
|
5012
|
+
getWorkspaceId() {
|
|
5013
|
+
var _a, _b;
|
|
5014
|
+
return (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.workspaceId) !== null && _b !== void 0 ? _b : null;
|
|
5015
|
+
}
|
|
4441
5016
|
/**
|
|
4442
5017
|
* Initialize the SDK
|
|
4443
5018
|
*/
|
|
@@ -4581,6 +5156,11 @@ class Datalyr {
|
|
|
4581
5156
|
const deviceId = this.identity.getAnonymousId();
|
|
4582
5157
|
yield dataEncryption.initialize(this.config.workspaceId, deviceId);
|
|
4583
5158
|
this.userProperties = yield storage.getEncrypted('dl_user_traits', {});
|
|
5159
|
+
// WEB-22: restore a PII user id that was persisted encrypted. Must run
|
|
5160
|
+
// here — it needs dataEncryption to be initialized, so it cannot happen
|
|
5161
|
+
// in the IdentityManager constructor. It never overwrites an id already
|
|
5162
|
+
// set by an explicit identify() earlier in this page load.
|
|
5163
|
+
yield this.identity.hydrateEncryptedUserId();
|
|
4584
5164
|
this.log('Encryption initialized, user properties loaded');
|
|
4585
5165
|
}
|
|
4586
5166
|
catch (encErr) {
|
|
@@ -4670,14 +5250,20 @@ class Datalyr {
|
|
|
4670
5250
|
// Setup auto-identify callback
|
|
4671
5251
|
this.autoIdentify.initialize((email, source) => {
|
|
4672
5252
|
this.log(`Auto-identified user: ${email} from ${source}`);
|
|
4673
|
-
//
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
//
|
|
4680
|
-
|
|
5253
|
+
// WEB-21: ONE event, not two.
|
|
5254
|
+
//
|
|
5255
|
+
// This used to emit `$auto_identify` and then call identify(),
|
|
5256
|
+
// which emits `$identify` — two events ~1ms apart carrying the same
|
|
5257
|
+
// email. Production over 7 days on workspace f6260736 showed a
|
|
5258
|
+
// perfect 379 / 379 / 379 split ($identify / $auto_identify /
|
|
5259
|
+
// visitors): every auto-identification was booked twice.
|
|
5260
|
+
//
|
|
5261
|
+
// Nothing consumed `$auto_identify`: grepped across the whole
|
|
5262
|
+
// platform (app/, lib/, all Cloudflare workers, all Tinybird pipes)
|
|
5263
|
+
// — zero readers, while prod carried 6,197 of them in 30 days.
|
|
5264
|
+
// The only information it added over `$identify` was WHICH detector
|
|
5265
|
+
// found the email, so that is preserved as a trait instead.
|
|
5266
|
+
this.identify(email, { email, auto_identify_source: source });
|
|
4681
5267
|
});
|
|
4682
5268
|
}
|
|
4683
5269
|
// Stamp attribution signals into the Shopify cart (OPT-IN, default off).
|
|
@@ -4708,10 +5294,22 @@ class Datalyr {
|
|
|
4708
5294
|
if (this.config.stripePaymentLinks !== false && this.shouldTrack()) {
|
|
4709
5295
|
this.syncStripePaymentLinks((_b = this.config.stripeLinkDomains) !== null && _b !== void 0 ? _b : []);
|
|
4710
5296
|
}
|
|
4711
|
-
//
|
|
4712
|
-
|
|
4713
|
-
|
|
5297
|
+
// Checkout Session capture (DEFAULT ON). The decorator above cannot help
|
|
5298
|
+
// when the merchant's BACKEND creates the session — checkout.stripe.com
|
|
5299
|
+
// ignores client_reference_id post-creation — which is how most SaaS
|
|
5300
|
+
// checkouts work, and why payers arrive at our webhook with no visitor
|
|
5301
|
+
// (measured 0.5% and 13.3% visitor coverage on the two Stripe tenants).
|
|
5302
|
+
// The session id still has to reach the browser, so we observe it and
|
|
5303
|
+
// let the server join on session.id. Same shouldTrack() gate as the
|
|
5304
|
+
// decorator: an opted-out visitor's id is never paired with a checkout.
|
|
5305
|
+
if (this.config.stripeCheckoutSessions !== false && this.shouldTrack()) {
|
|
5306
|
+
this.startStripeSessionCapture();
|
|
4714
5307
|
}
|
|
5308
|
+
// Track the initial page view after encryption is ready. On Shopify the
|
|
5309
|
+
// Customer Privacy API can still be loading, so retain one pending
|
|
5310
|
+
// pageview and release it from onShopifyConsentChanged() once allowed.
|
|
5311
|
+
this.initialPageViewReady = true;
|
|
5312
|
+
this.trackInitialPageViewOnce();
|
|
4715
5313
|
this.log('Async initialization complete');
|
|
4716
5314
|
}
|
|
4717
5315
|
catch (error) {
|
|
@@ -4844,6 +5442,50 @@ class Datalyr {
|
|
|
4844
5442
|
/**
|
|
4845
5443
|
* Identify a user
|
|
4846
5444
|
*/
|
|
5445
|
+
/**
|
|
5446
|
+
* Stable fingerprint of an identity for redundant-emit suppression (WEB-20).
|
|
5447
|
+
*
|
|
5448
|
+
* Keys are sorted and each value is JSON-serialized, so `{a,b}` and `{b,a}`
|
|
5449
|
+
* collapse to one identity while a genuinely changed nested trait does not.
|
|
5450
|
+
* (The mobile SDKs use `String(value)`, which flattens every object to
|
|
5451
|
+
* `[object Object]` and can swallow a real change; web traits are plain JSON,
|
|
5452
|
+
* so it can afford to be exact.)
|
|
5453
|
+
*
|
|
5454
|
+
* Hashed rather than stored raw so the key does not become another copy of the
|
|
5455
|
+
* user's traits at rest. Being honest about the strength: a 32-bit
|
|
5456
|
+
* non-cryptographic digest sitting beside a readable `dl_dl_anonymous_id` is
|
|
5457
|
+
* NOT protection against a determined reader — it defeats casual inspection
|
|
5458
|
+
* and bulk scraping, nothing more. The PII that matters is handled properly
|
|
5459
|
+
* (encrypted `dl_user_id_pii`, encrypted `dl_user_traits`). 32 bits also means
|
|
5460
|
+
* a ~1-in-4.3e9 chance per identity that a genuine change is suppressed;
|
|
5461
|
+
* acceptable for change detection, which is all this is.
|
|
5462
|
+
*/
|
|
5463
|
+
identityFingerprint(userId, traits = {}) {
|
|
5464
|
+
const stableTraits = Object.keys(traits || {})
|
|
5465
|
+
.sort()
|
|
5466
|
+
.map((key) => {
|
|
5467
|
+
let value;
|
|
5468
|
+
try {
|
|
5469
|
+
value = JSON.stringify(traits[key]);
|
|
5470
|
+
}
|
|
5471
|
+
catch (_a) {
|
|
5472
|
+
// Circular/unserializable trait — fall back to a coarse marker rather
|
|
5473
|
+
// than throwing inside identify().
|
|
5474
|
+
value = '[unserializable]';
|
|
5475
|
+
}
|
|
5476
|
+
return `${key}=${value}`;
|
|
5477
|
+
})
|
|
5478
|
+
.join('&');
|
|
5479
|
+
const input = `${this.identity.getAnonymousId()}|${userId}|${stableTraits}`;
|
|
5480
|
+
// 32-bit FNV-1a. Not cryptographic — this only needs to detect change, and
|
|
5481
|
+
// it must be synchronous (crypto.subtle is async and identify() is not).
|
|
5482
|
+
let hash = 0x811c9dc5;
|
|
5483
|
+
for (let i = 0; i < input.length; i++) {
|
|
5484
|
+
hash ^= input.charCodeAt(i);
|
|
5485
|
+
hash = Math.imul(hash, 0x01000193);
|
|
5486
|
+
}
|
|
5487
|
+
return (hash >>> 0).toString(16);
|
|
5488
|
+
}
|
|
4847
5489
|
identify(userId, traits = {}) {
|
|
4848
5490
|
if (!this.initialized) {
|
|
4849
5491
|
console.warn('[Datalyr] SDK not initialized. Call init() first.');
|
|
@@ -4883,8 +5525,31 @@ class Datalyr {
|
|
|
4883
5525
|
// If encryption fails, user traits are only stored in memory (this.userProperties)
|
|
4884
5526
|
// and will be lost on page reload - but PII is NOT exposed in localStorage
|
|
4885
5527
|
});
|
|
4886
|
-
//
|
|
4887
|
-
|
|
5528
|
+
// WEB-20: redundant-identify suppression, matching iOS 2.1.11 / RN 1.7.16.
|
|
5529
|
+
//
|
|
5530
|
+
// identify() is host-app-driven and SPA routers commonly call it in a
|
|
5531
|
+
// route effect, so an unchanged identity was re-emitted on every
|
|
5532
|
+
// navigation. The mobile SDKs got this in 2.1.10/1.7.15; web did not.
|
|
5533
|
+
//
|
|
5534
|
+
// Fingerprint = anonymousId | userId | sorted traits. The anonymous id is
|
|
5535
|
+
// included so a reset()/rotation always re-emits and identity links are
|
|
5536
|
+
// never lost — only exact repeats are skipped.
|
|
5537
|
+
//
|
|
5538
|
+
// Only the EVENT is suppressed. `this.identity.identify()` above has
|
|
5539
|
+
// already persisted `dl_user_id`, traits are still merged and encrypted,
|
|
5540
|
+
// and the plugin handlers below still run on every call — a plugin's
|
|
5541
|
+
// `identify` hook is a customer extension point whose semantics we cannot
|
|
5542
|
+
// assume, so its contract ("called when identify() is called") is
|
|
5543
|
+
// deliberately left intact.
|
|
5544
|
+
const identityFingerprint = this.identityFingerprint(userId, traits);
|
|
5545
|
+
const isRedundantIdentify = storage.getString(IDENTIFY_FINGERPRINT_KEY, null) === identityFingerprint;
|
|
5546
|
+
if (isRedundantIdentify) {
|
|
5547
|
+
this.log('Skipping redundant identify (unchanged identity):', userId);
|
|
5548
|
+
}
|
|
5549
|
+
else {
|
|
5550
|
+
storage.set(IDENTIFY_FINGERPRINT_KEY, identityFingerprint);
|
|
5551
|
+
this.track('$identify', Object.assign(Object.assign({}, identityLink), { traits }));
|
|
5552
|
+
}
|
|
4888
5553
|
// Call plugin handlers
|
|
4889
5554
|
if (this.config.plugins) {
|
|
4890
5555
|
for (const plugin of this.config.plugins) {
|
|
@@ -4904,6 +5569,18 @@ class Datalyr {
|
|
|
4904
5569
|
this.trackError(error, { userId });
|
|
4905
5570
|
}
|
|
4906
5571
|
}
|
|
5572
|
+
/**
|
|
5573
|
+
* WEB-27: queue health, including events the SDK gave up on.
|
|
5574
|
+
*
|
|
5575
|
+
* `getStats()` existed on EventQueue but was unreachable — `queue` is private
|
|
5576
|
+
* and nothing exposed it, so the observability fix could not actually be
|
|
5577
|
+
* observed. A non-zero `droppedEvents` means data was lost.
|
|
5578
|
+
*/
|
|
5579
|
+
getQueueStats() {
|
|
5580
|
+
if (!this.queue)
|
|
5581
|
+
return null;
|
|
5582
|
+
return this.queue.getStats();
|
|
5583
|
+
}
|
|
4907
5584
|
/**
|
|
4908
5585
|
* Track a page view
|
|
4909
5586
|
*/
|
|
@@ -5021,6 +5698,9 @@ class Datalyr {
|
|
|
5021
5698
|
// values to the next user's events (cross-user contamination on shared devices).
|
|
5022
5699
|
this.superProperties = {};
|
|
5023
5700
|
storage.remove('dl_user_traits');
|
|
5701
|
+
// WEB-20: drop the identify fingerprint, so a logout→login (or a different
|
|
5702
|
+
// user on this browser) always re-emits $identify and the link is rebuilt.
|
|
5703
|
+
storage.remove(IDENTIFY_FINGERPRINT_KEY);
|
|
5024
5704
|
// Clear the auto-identify guard so a different user on the same browser is
|
|
5025
5705
|
// re-captured (it short-circuits while dl_auto_identified_email is present), and so
|
|
5026
5706
|
// the prior user's email isn't left at rest after logout.
|
|
@@ -5168,16 +5848,21 @@ class Datalyr {
|
|
|
5168
5848
|
* (same convention as pixels/auto-identify).
|
|
5169
5849
|
*/
|
|
5170
5850
|
disposeMarketingLinkDecorators() {
|
|
5171
|
-
var _a, _b;
|
|
5851
|
+
var _a, _b, _c;
|
|
5172
5852
|
try {
|
|
5173
5853
|
(_a = this.stripeLinksDisposer) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
5174
5854
|
}
|
|
5175
|
-
catch ( /* best-effort */
|
|
5855
|
+
catch ( /* best-effort */_d) { /* best-effort */ }
|
|
5176
5856
|
this.stripeLinksDisposer = undefined;
|
|
5177
5857
|
try {
|
|
5178
|
-
(_b = this.
|
|
5858
|
+
(_b = this.stripeSessionWatcher) === null || _b === void 0 ? void 0 : _b.stop();
|
|
5179
5859
|
}
|
|
5180
|
-
catch ( /* best-effort */
|
|
5860
|
+
catch ( /* best-effort */_e) { /* best-effort */ }
|
|
5861
|
+
this.stripeSessionWatcher = undefined;
|
|
5862
|
+
try {
|
|
5863
|
+
(_c = this.outboundDisposer) === null || _c === void 0 ? void 0 : _c.call(this);
|
|
5864
|
+
}
|
|
5865
|
+
catch ( /* best-effort */_f) { /* best-effort */ }
|
|
5181
5866
|
this.outboundDisposer = undefined;
|
|
5182
5867
|
}
|
|
5183
5868
|
optOut() {
|
|
@@ -5206,8 +5891,22 @@ class Datalyr {
|
|
|
5206
5891
|
// Purge PII at rest.
|
|
5207
5892
|
this.userProperties = {};
|
|
5208
5893
|
storage.remove('dl_user_traits');
|
|
5894
|
+
// WEB-26: remove the PLAINTEXT id as well. optOut() purged only the
|
|
5895
|
+
// encrypted copy, so an opted-out visitor whose email was written by an
|
|
5896
|
+
// older build kept it at rest — the one state where that is least
|
|
5897
|
+
// defensible.
|
|
5898
|
+
storage.remove('dl_user_id');
|
|
5899
|
+
storage.remove('dl_user_id_pii');
|
|
5900
|
+
storage.remove(IDENTIFY_FINGERPRINT_KEY);
|
|
5209
5901
|
storage.remove('dl_auto_identified_email');
|
|
5210
5902
|
storage.remove('dl_journey');
|
|
5903
|
+
// WEB-25: first/last touch hold redacted-but-full landing URLs, referrers and
|
|
5904
|
+
// click ids for up to 90 days. reset() has always cleared them; optOut() did
|
|
5905
|
+
// not — so an opted-out visitor kept marketing attribution at rest, which is
|
|
5906
|
+
// the one state where it is least defensible. Same rationale as dl_journey
|
|
5907
|
+
// directly above.
|
|
5908
|
+
storage.remove('dl_first_touch');
|
|
5909
|
+
storage.remove('dl_last_touch');
|
|
5211
5910
|
this.log('User opted out');
|
|
5212
5911
|
}
|
|
5213
5912
|
/**
|
|
@@ -5270,8 +5969,16 @@ class Datalyr {
|
|
|
5270
5969
|
this.autoIdentify = undefined;
|
|
5271
5970
|
this.userProperties = {};
|
|
5272
5971
|
storage.remove('dl_user_traits');
|
|
5972
|
+
// WEB-26: see optOut() — the plaintext id must go too.
|
|
5973
|
+
storage.remove('dl_user_id');
|
|
5974
|
+
storage.remove('dl_user_id_pii');
|
|
5975
|
+
storage.remove(IDENTIFY_FINGERPRINT_KEY);
|
|
5273
5976
|
storage.remove('dl_auto_identified_email');
|
|
5274
5977
|
storage.remove('dl_journey');
|
|
5978
|
+
// WEB-25: see optOut() — withdrawn analytics consent must not leave 90-day
|
|
5979
|
+
// marketing attribution at rest either.
|
|
5980
|
+
storage.remove('dl_first_touch');
|
|
5981
|
+
storage.remove('dl_last_touch');
|
|
5275
5982
|
}
|
|
5276
5983
|
else {
|
|
5277
5984
|
// TR-15 (P3): grant → persist the in-memory anon id now (see onShopifyConsentChanged).
|
|
@@ -5525,6 +6232,39 @@ class Datalyr {
|
|
|
5525
6232
|
* changes. window.open(paymentLink) is not covered (use getVisitorId()
|
|
5526
6233
|
* manually); iframe/shadow-DOM links are unreachable from document.
|
|
5527
6234
|
*/
|
|
6235
|
+
/**
|
|
6236
|
+
* Observe Stripe Checkout Session ids and report each one once.
|
|
6237
|
+
*
|
|
6238
|
+
* The event is what carries the pairing: the server stores
|
|
6239
|
+
* (stripe_session_id -> visitor_id) and, when checkout.session.completed
|
|
6240
|
+
* arrives without a client_reference_id, joins on session.id and feeds the
|
|
6241
|
+
* SAME cacheCustomerVisitor path the Payment Link flow already uses — so
|
|
6242
|
+
* every later invoice for that customer inherits the visitor too.
|
|
6243
|
+
*
|
|
6244
|
+
* Consent is re-checked at emit time, not just at init: a visitor can
|
|
6245
|
+
* withdraw between page load and checkout, and this pairing is exactly the
|
|
6246
|
+
* kind of identity link that must stop when they do.
|
|
6247
|
+
*/
|
|
6248
|
+
startStripeSessionCapture() {
|
|
6249
|
+
if (this.stripeSessionWatcher)
|
|
6250
|
+
return;
|
|
6251
|
+
const watcher = new StripeSessionWatcher();
|
|
6252
|
+
this.stripeSessionWatcher = watcher;
|
|
6253
|
+
watcher.start((stripeSessionId) => {
|
|
6254
|
+
var _a;
|
|
6255
|
+
if (!this.shouldTrack())
|
|
6256
|
+
return;
|
|
6257
|
+
this.track('$stripe_checkout_session', { stripe_session_id: stripeSessionId });
|
|
6258
|
+
// The very next thing this page does is navigate to Stripe, so flush now
|
|
6259
|
+
// rather than let the batch timer lose the pairing to the unload.
|
|
6260
|
+
try {
|
|
6261
|
+
(_a = this.queue) === null || _a === void 0 ? void 0 : _a.flush();
|
|
6262
|
+
}
|
|
6263
|
+
catch (_b) {
|
|
6264
|
+
/* best-effort — never block the checkout */
|
|
6265
|
+
}
|
|
6266
|
+
});
|
|
6267
|
+
}
|
|
5528
6268
|
syncStripePaymentLinks(extraDomains) {
|
|
5529
6269
|
if (typeof window === "undefined" || typeof document === "undefined")
|
|
5530
6270
|
return;
|
|
@@ -5751,7 +6491,7 @@ class Datalyr {
|
|
|
5751
6491
|
// drift from the package again (it sat at 1.7.4 across the 1.7.5 release). The
|
|
5752
6492
|
// build guard (scripts/check-bundle.js, run by build:check) still verifies the
|
|
5753
6493
|
// deployable bundles carry the package.json version. (FSR-103)
|
|
5754
|
-
sdk_version: '1.7.
|
|
6494
|
+
sdk_version: '1.7.9',
|
|
5755
6495
|
sdk_name: 'datalyr-web-sdk',
|
|
5756
6496
|
// A3-25: versioned-envelope stamp. Every SDK emits schema_version so the ingest/contract
|
|
5757
6497
|
// layer can key on ONE canonical envelope version (snake_case fields, event_name/event_data
|
|
@@ -5944,6 +6684,8 @@ class Datalyr {
|
|
|
5944
6684
|
// visitor_id that vanishes on the next page load. Idempotent.
|
|
5945
6685
|
if (allowed)
|
|
5946
6686
|
this.identity.enablePersistence();
|
|
6687
|
+
if (allowed)
|
|
6688
|
+
this.trackInitialPageViewOnce();
|
|
5947
6689
|
if (!allowed) {
|
|
5948
6690
|
// Mirror setConsent() withdrawal: purge buffered events so events captured
|
|
5949
6691
|
// before the decline can't drain if consent is later re-granted.
|
|
@@ -5975,6 +6717,15 @@ class Datalyr {
|
|
|
5975
6717
|
}
|
|
5976
6718
|
this.log('Shopify consent collected — analytics allowed:', allowed, '— marketing blocked:', marketingBlocked);
|
|
5977
6719
|
}
|
|
6720
|
+
/** Release the automatic landing pageview once, after init and consent. */
|
|
6721
|
+
trackInitialPageViewOnce() {
|
|
6722
|
+
if (!this.initialPageViewReady || this.initialPageViewSent)
|
|
6723
|
+
return;
|
|
6724
|
+
if (!this.config.trackPageViews || !this.initialized || !this.shouldTrack())
|
|
6725
|
+
return;
|
|
6726
|
+
this.initialPageViewSent = true;
|
|
6727
|
+
this.page();
|
|
6728
|
+
}
|
|
5978
6729
|
/**
|
|
5979
6730
|
* Setup SPA tracking
|
|
5980
6731
|
* Fixed Issue #15: Store original methods for cleanup
|
|
@@ -6169,6 +6920,10 @@ class Datalyr {
|
|
|
6169
6920
|
this.stripeLinksDisposer();
|
|
6170
6921
|
this.stripeLinksDisposer = undefined;
|
|
6171
6922
|
}
|
|
6923
|
+
if (this.stripeSessionWatcher) {
|
|
6924
|
+
this.stripeSessionWatcher.stop();
|
|
6925
|
+
this.stripeSessionWatcher = undefined;
|
|
6926
|
+
}
|
|
6172
6927
|
// Clean up queue
|
|
6173
6928
|
if (this.queue) {
|
|
6174
6929
|
this.queue.destroy();
|
|
@@ -6194,6 +6949,8 @@ class Datalyr {
|
|
|
6194
6949
|
this.container = undefined;
|
|
6195
6950
|
this.autoIdentify = undefined;
|
|
6196
6951
|
this.lastSpaPath = null;
|
|
6952
|
+
this.initialPageViewReady = false;
|
|
6953
|
+
this.initialPageViewSent = false;
|
|
6197
6954
|
// Clear any remaining data
|
|
6198
6955
|
this.superProperties = {};
|
|
6199
6956
|
this.userProperties = {};
|
|
@@ -6202,19 +6959,27 @@ class Datalyr {
|
|
|
6202
6959
|
this.log('SDK destroyed');
|
|
6203
6960
|
}
|
|
6204
6961
|
}
|
|
6205
|
-
|
|
6206
|
-
|
|
6207
|
-
|
|
6208
|
-
|
|
6209
|
-
|
|
6210
|
-
|
|
6211
|
-
|
|
6212
|
-
|
|
6962
|
+
/**
|
|
6963
|
+
* Create an independent SDK instance without reading or mutating window.
|
|
6964
|
+
*
|
|
6965
|
+
* The CDN bootstrap uses this when an existing global is configured for a
|
|
6966
|
+
* different workspace. Package consumers can also opt into multiple explicit
|
|
6967
|
+
* instances without changing the default singleton contract below.
|
|
6968
|
+
*/
|
|
6969
|
+
function createDatalyrInstance() {
|
|
6970
|
+
return new Datalyr();
|
|
6971
|
+
}
|
|
6972
|
+
// Create the default singleton. TR-23: reuse an existing window.datalyr (a
|
|
6973
|
+
// prior tag's instance or a package singleton) so npm/ESM/CJS consumers retain
|
|
6974
|
+
// the established one-global behavior. Workspace conflict resolution belongs
|
|
6975
|
+
// to the CDN bootstrap, which has the authoritative script-tag configuration.
|
|
6976
|
+
const datalyr = (typeof window !== 'undefined' && window.datalyr) || createDatalyrInstance();
|
|
6213
6977
|
// Expose global API
|
|
6214
6978
|
if (typeof window !== 'undefined') {
|
|
6215
6979
|
window.datalyr = datalyr;
|
|
6216
6980
|
}
|
|
6217
6981
|
|
|
6982
|
+
exports.createDatalyrInstance = createDatalyrInstance;
|
|
6218
6983
|
exports.datalyr = datalyr;
|
|
6219
6984
|
exports.default = datalyr;
|
|
6220
6985
|
//# sourceMappingURL=datalyr.cjs.js.map
|