@10x-media/analytics 1.0.0-beta.14 → 1.0.0-beta.16

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.
Files changed (175) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/ga4/ga4.d.ts +7 -0
  4. package/dist/adapters/ga4/ga4.js +17 -3
  5. package/dist/adapters/ga4/ga4.js.map +1 -1
  6. package/dist/adapters/plausible/plausible.d.ts +6 -0
  7. package/dist/adapters/plausible/plausible.js +6 -3
  8. package/dist/adapters/plausible/plausible.js.map +1 -1
  9. package/dist/adapters/posthog/cloud.js +20 -0
  10. package/dist/adapters/posthog/cloud.js.map +1 -0
  11. package/dist/adapters/posthog/credentials.js +20 -0
  12. package/dist/adapters/posthog/credentials.js.map +1 -0
  13. package/dist/adapters/posthog/posthog.d.ts +10 -3
  14. package/dist/adapters/posthog/posthog.js +100 -27
  15. package/dist/adapters/posthog/posthog.js.map +1 -1
  16. package/dist/adapters/posthog/projectCheck.js +56 -0
  17. package/dist/adapters/posthog/projectCheck.js.map +1 -0
  18. package/dist/adapters/umami/umami.d.ts +6 -0
  19. package/dist/adapters/umami/umami.js +3 -1
  20. package/dist/adapters/umami/umami.js.map +1 -1
  21. package/dist/capture/trackerConfig.js +29 -6
  22. package/dist/capture/trackerConfig.js.map +1 -1
  23. package/dist/capture/withoutConsent.js +91 -0
  24. package/dist/capture/withoutConsent.js.map +1 -0
  25. package/dist/core/capture.d.ts +41 -11
  26. package/dist/core/captureOptions.js +97 -8
  27. package/dist/core/captureOptions.js.map +1 -1
  28. package/dist/core/contract.d.ts +25 -0
  29. package/dist/core/contract.js.map +1 -1
  30. package/dist/core/options.d.ts +24 -2
  31. package/dist/core/options.js.map +1 -1
  32. package/dist/core/sourceOptions.js +2 -1
  33. package/dist/core/sourceOptions.js.map +1 -1
  34. package/dist/exports/adapters/contract.d.ts +3 -3
  35. package/dist/exports/adapters/ga4.d.ts +4 -4
  36. package/dist/exports/adapters/native.d.ts +5 -5
  37. package/dist/exports/adapters/plausible.d.ts +4 -4
  38. package/dist/exports/adapters/posthog.d.ts +4 -4
  39. package/dist/exports/adapters/umami.d.ts +4 -4
  40. package/dist/exports/next.d.ts +4 -4
  41. package/dist/exports/react.d.ts +3 -2
  42. package/dist/exports/site.d.ts +2 -2
  43. package/dist/exports/testing.d.ts +4 -4
  44. package/dist/exports/tracker.d.ts +3 -2
  45. package/dist/exports/types.d.ts +5 -5
  46. package/dist/index.d.ts +5 -5
  47. package/dist/index.js +2 -0
  48. package/dist/index.js.map +1 -1
  49. package/dist/native/collections/hits.js +1 -0
  50. package/dist/native/collections/hits.js.map +1 -1
  51. package/dist/native/engine/engine.js +5 -1
  52. package/dist/native/engine/engine.js.map +1 -1
  53. package/dist/native/engine/planner.js +2 -1
  54. package/dist/native/engine/planner.js.map +1 -1
  55. package/dist/native/engine/toHit.js +4 -2
  56. package/dist/native/engine/toHit.js.map +1 -1
  57. package/dist/native/ingest/endpoint.js +5 -2
  58. package/dist/native/ingest/endpoint.js.map +1 -1
  59. package/dist/native/ingest/normalizeEvent.d.ts +6 -1
  60. package/dist/native/ingest/normalizeEvent.js +17 -7
  61. package/dist/native/ingest/normalizeEvent.js.map +1 -1
  62. package/dist/native/ingest/serverTrack.js +2 -1
  63. package/dist/native/ingest/serverTrack.js.map +1 -1
  64. package/dist/native/ingest/validate.js +3 -0
  65. package/dist/native/ingest/validate.js.map +1 -1
  66. package/dist/native/nativeAdapter.d.ts +10 -1
  67. package/dist/native/nativeAdapter.js +11 -1
  68. package/dist/native/nativeAdapter.js.map +1 -1
  69. package/dist/native/store/aggregate.js +1 -1
  70. package/dist/native/store/aggregate.js.map +1 -1
  71. package/dist/native/store/hitRow.js +3 -0
  72. package/dist/native/store/hitRow.js.map +1 -1
  73. package/dist/native/store/hits.js +3 -1
  74. package/dist/native/store/hits.js.map +1 -1
  75. package/dist/native/store/mongo/aggregatePipelines.js +10 -2
  76. package/dist/native/store/mongo/aggregatePipelines.js.map +1 -1
  77. package/dist/native/store/mongo/pipelines.js +15 -5
  78. package/dist/native/store/mongo/pipelines.js.map +1 -1
  79. package/dist/native/store/parts.js +9 -3
  80. package/dist/native/store/parts.js.map +1 -1
  81. package/dist/native/store/rawRead.js +6 -3
  82. package/dist/native/store/rawRead.js.map +1 -1
  83. package/dist/native/store/sql/aggregateSql.js +6 -2
  84. package/dist/native/store/sql/aggregateSql.js.map +1 -1
  85. package/dist/native/store/sql/columns.js +1 -0
  86. package/dist/native/store/sql/columns.js.map +1 -1
  87. package/dist/native/store/sql/compile.js +19 -7
  88. package/dist/native/store/sql/compile.js.map +1 -1
  89. package/dist/native/store/types.d.ts +7 -1
  90. package/dist/native/store/types.js +10 -0
  91. package/dist/native/store/types.js.map +1 -0
  92. package/dist/plugin/createRuntime.js +10 -1
  93. package/dist/plugin/createRuntime.js.map +1 -1
  94. package/dist/plugin/runtime.js.map +1 -1
  95. package/dist/providers/collection.d.ts +2 -0
  96. package/dist/providers/collection.js +36 -9
  97. package/dist/providers/collection.js.map +1 -1
  98. package/dist/providers/factory.d.ts +9 -1
  99. package/dist/providers/factory.js +14 -5
  100. package/dist/providers/factory.js.map +1 -1
  101. package/dist/providers/hosts.js +14 -6
  102. package/dist/providers/hosts.js.map +1 -1
  103. package/dist/providers/registry.js +2 -1
  104. package/dist/providers/registry.js.map +1 -1
  105. package/dist/react/AnalyticsProvider.d.ts +6 -0
  106. package/dist/react/AnalyticsProvider.js.map +1 -1
  107. package/dist/react/useAnalytics.js +9 -5
  108. package/dist/react/useAnalytics.js.map +1 -1
  109. package/dist/site/AnalyticsScripts.d.ts +4 -3
  110. package/dist/site/AnalyticsScripts.js +18 -4
  111. package/dist/site/AnalyticsScripts.js.map +1 -1
  112. package/dist/surfacing/todaySplit.js +7 -1
  113. package/dist/surfacing/todaySplit.js.map +1 -1
  114. package/dist/tracker/config.d.ts +26 -1
  115. package/dist/tracker/config.js.map +1 -1
  116. package/dist/tracker/createTracker.d.ts +2 -1
  117. package/dist/tracker/createTracker.js +91 -59
  118. package/dist/tracker/createTracker.js.map +1 -1
  119. package/dist/tracker/gate.d.ts +8 -0
  120. package/dist/tracker/gate.js +61 -11
  121. package/dist/tracker/gate.js.map +1 -1
  122. package/dist/tracker/loadScript.js +2 -1
  123. package/dist/tracker/loadScript.js.map +1 -1
  124. package/dist/tracker/registry.js +14 -3
  125. package/dist/tracker/registry.js.map +1 -1
  126. package/dist/tracker/sinks/createSink.js +27 -8
  127. package/dist/tracker/sinks/createSink.js.map +1 -1
  128. package/dist/tracker/sinks/ga4.js +38 -6
  129. package/dist/tracker/sinks/ga4.js.map +1 -1
  130. package/dist/tracker/sinks/native.js +17 -3
  131. package/dist/tracker/sinks/native.js.map +1 -1
  132. package/dist/tracker/sinks/plausible.js +8 -2
  133. package/dist/tracker/sinks/plausible.js.map +1 -1
  134. package/dist/tracker/sinks/posthog.js +108 -16
  135. package/dist/tracker/sinks/posthog.js.map +1 -1
  136. package/dist/tracker/sinks/umami.js +2 -2
  137. package/dist/tracker/sinks/umami.js.map +1 -1
  138. package/dist/tracker/sinks/vendor.js +25 -11
  139. package/dist/tracker/sinks/vendor.js.map +1 -1
  140. package/dist/tracker/types.d.ts +10 -3
  141. package/dist/translations/ar.js +10 -0
  142. package/dist/translations/ar.js.map +1 -1
  143. package/dist/translations/de.js +10 -0
  144. package/dist/translations/de.js.map +1 -1
  145. package/dist/translations/en.js +10 -0
  146. package/dist/translations/en.js.map +1 -1
  147. package/dist/translations/es.js +10 -0
  148. package/dist/translations/es.js.map +1 -1
  149. package/dist/translations/fr.js +10 -0
  150. package/dist/translations/fr.js.map +1 -1
  151. package/dist/translations/id.js +10 -0
  152. package/dist/translations/id.js.map +1 -1
  153. package/dist/translations/keys.d.ts +10 -0
  154. package/dist/translations/keys.js +11 -1
  155. package/dist/translations/keys.js.map +1 -1
  156. package/dist/translations/ko.js +10 -0
  157. package/dist/translations/ko.js.map +1 -1
  158. package/dist/translations/pt.js +10 -0
  159. package/dist/translations/pt.js.map +1 -1
  160. package/dist/translations/ru.js +10 -0
  161. package/dist/translations/ru.js.map +1 -1
  162. package/dist/translations/uk.js +10 -0
  163. package/dist/translations/uk.js.map +1 -1
  164. package/dist/translations/zh.js +10 -0
  165. package/dist/translations/zh.js.map +1 -1
  166. package/dist/view/consentShare.js +15 -0
  167. package/dist/view/consentShare.js.map +1 -0
  168. package/dist/view/sections/OverviewCards.js +38 -28
  169. package/dist/view/sections/OverviewCards.js.map +1 -1
  170. package/dist/widgets/notices.js +6 -1
  171. package/dist/widgets/notices.js.map +1 -1
  172. package/dist/widgets/readMeta.d.ts +4 -0
  173. package/dist/widgets/readMeta.js +5 -0
  174. package/dist/widgets/readMeta.js.map +1 -1
  175. package/package.json +3 -3
@@ -6,7 +6,7 @@ import { sanitizeItems } from "../goals/items.js";
6
6
  import { createAutoCapture, itemCaptureOf } from "./autoCapture.js";
7
7
  import { createEngagement, msSince } from "./engagement.js";
8
8
  import { campaignFields, itemFields, pageContext } from "./events.js";
9
- import { ALL_PENDING, createGate, createGateQueue, forgetLegacyConsent, queueable } from "./gate.js";
9
+ import { ALL_OFF, ALL_PENDING, createGate, createGateQueue, forgetLegacyConsent, queueable } from "./gate.js";
10
10
  import { createItemCapture } from "./itemCapture.js";
11
11
  import { createScriptLoader } from "./loadScript.js";
12
12
  import { createPageTracking } from "./pageTracking.js";
@@ -25,6 +25,13 @@ const NEGATIVE_REVENUE = "analytics: purchase() ignored a negative amount. Refun
25
25
  const BROWSER_REFUND = "analytics: refund() does nothing in the browser. A refund is recorded server-side, from the payment webhook: anything a public beacon could subtract from a revenue bucket, anyone could forge.";
26
26
  /** Events held while the boot checks an exclusion link or the live preview, a moment at most. */
27
27
  const HELD_LIMIT = 100;
28
+ /** How far each mode goes, for a restore that must never lower a vendor already running. */
29
+ const MODE_RANK = {
30
+ off: 0,
31
+ anonymous: 1,
32
+ cookieless: 2,
33
+ full: 3
34
+ };
28
35
  /** How a tracker puts its held slots back to pending after a back/forward cache restore. */
29
36
  const restorers = /* @__PURE__ */ new WeakMap();
30
37
  /**
@@ -43,6 +50,7 @@ const createNoopTracker = () => ({
43
50
  refund: () => void 0,
44
51
  setGate: () => void 0,
45
52
  gate: ALL_PENDING,
53
+ mode: ALL_OFF,
46
54
  excluded: false,
47
55
  exclude: () => void 0,
48
56
  flush: () => void 0,
@@ -62,6 +70,9 @@ const createSkippedTracker = (config, boot) => {
62
70
  },
63
71
  get gate() {
64
72
  return gate.snapshot;
73
+ },
74
+ get mode() {
75
+ return gate.modes;
65
76
  }
66
77
  };
67
78
  restorers.set(tracker, () => gate.reset());
@@ -69,7 +80,8 @@ const createSkippedTracker = (config, boot) => {
69
80
  };
70
81
  /**
71
82
  * The browser tracker: one sink per slot behind a gate the host's consent manager answers, kept in
72
- * memory alone. No storage is touched before a slot opens, the exclusion link's own write aside.
83
+ * memory alone, each slot capturing in the mode its gate gives it. No storage is touched before a
84
+ * slot runs in full, the exclusion link's own aside.
73
85
  */
74
86
  const createTracker = (config, options = {}) => {
75
87
  const win = currentWindow(options.window);
@@ -82,7 +94,7 @@ const createTracker = (config, options = {}) => {
82
94
  const snippets = options.snippets !== false;
83
95
  const loadScript = options.loadScript ?? createScriptLoader(win, options.nonce);
84
96
  let auto = null;
85
- /** False until the stored flag is read, which waits for the first slot to open. */
97
+ /** False until the stored flag is read, which waits for the first slot to run in full. */
86
98
  let excluded = false;
87
99
  /** Whether the stored flag was read, or made moot by an explicit exclusion on this page. */
88
100
  let flagKnown = false;
@@ -95,28 +107,26 @@ const createTracker = (config, options = {}) => {
95
107
  let destroyed = false;
96
108
  /** The page turned out to be the live preview: nothing more is sent, and the gate still answers. */
97
109
  let skipped = false;
110
+ const gate = createGate(config.slots);
98
111
  const entries = config.slots.map((slot) => ({
99
112
  slot: slot.slot,
100
113
  kind: slot.kind,
101
- /** What waits while the slot's gate is pending, or while the page is only prerendered. */
114
+ /** What waits while the slot is pending and sends nothing, or while the page is only prerendered. */
102
115
  queue: createGateQueue(),
103
- /**
104
- * Whether an opening or an explicit inclusion cleared this vendor's own opt-out on this page,
105
- * which the slot's scripts having run then clears once more.
106
- */
107
- lifted: false,
108
- /** Whether the slot was open on this page, so its script may be running whatever its gate says now. */
116
+ /** Whether the slot's scripts were loaded on this page, so they may be running whatever its mode says now. */
109
117
  ran: false,
110
118
  sink: createSink({
111
119
  slot,
112
120
  win,
113
121
  loadScript,
114
122
  ingestPath: config.ingestPath,
115
- snippets
123
+ snippets,
124
+ mode: () => gate.mode(slot.slot)
116
125
  })
117
126
  }));
118
- const gate = createGate(config.slots);
119
- const allowed = (entry) => gate.status(entry.slot) === "open";
127
+ const modeOf = (entry) => gate.mode(entry.slot);
128
+ /** Whether the slot sends what it is given now, in full or without consent. */
129
+ const sends = (entry) => modeOf(entry) !== "off";
120
130
  const live = () => !destroyed && !skipped;
121
131
  const now = () => win.performance.now();
122
132
  /**
@@ -139,7 +149,7 @@ const createTracker = (config, options = {}) => {
139
149
  activation = () => {
140
150
  activation = null;
141
151
  if (!live()) return;
142
- for (const entry of entries) if (allowed(entry)) {
152
+ for (const entry of entries) if (sends(entry)) {
143
153
  load(entry);
144
154
  drain(entry);
145
155
  }
@@ -149,16 +159,11 @@ const createTracker = (config, options = {}) => {
149
159
  }
150
160
  return true;
151
161
  };
152
- /**
153
- * Injects the slot's scripts, then clears the vendor's opt-out once more when this page lifted
154
- * it: a vendor whose snippet defines its global (PostHog) had none to clear before the snippet ran.
155
- */
162
+ /** Injects the slot's scripts; its sink switches the vendor once more when they ran. */
156
163
  const load = (entry) => {
157
164
  if (excluded || held !== null || !live() || waitUntilShown()) return;
158
165
  entry.ran = true;
159
- entry.sink.ready().then(() => {
160
- if (entry.lifted && !excluded && allowed(entry) && live()) switchVendor(entry, false);
161
- }).catch(() => void 0);
166
+ entry.sink.ready().catch(() => void 0);
162
167
  };
163
168
  /** The event as sent, with how long it waited when it waited at all. */
164
169
  const aged = (event, at) => {
@@ -198,11 +203,8 @@ const createTracker = (config, options = {}) => {
198
203
  return;
199
204
  }
200
205
  if (excluded) return;
201
- for (const entry of entries) {
202
- const status = gate.status(entry.slot);
203
- if (status === "open") deliver(entry, event, at);
204
- else if (status === "pending") hold(entry, event, at);
205
- }
206
+ for (const entry of entries) if (sends(entry)) deliver(entry, event, at);
207
+ else if (gate.status(entry.slot) === "pending") hold(entry, event, at);
206
208
  };
207
209
  const items = itemCaptureOf(config.autoCapture).impressions ? createItemCapture({
208
210
  win,
@@ -215,12 +217,13 @@ const createTracker = (config, options = {}) => {
215
217
  dispatch: (event) => dispatch(event)
216
218
  });
217
219
  /**
218
- * Watches for items while a native slot may receive what it sees, and not otherwise: never
219
- * while the browser is excluded, and never while that slot's gate is not open.
220
+ * Watches for items while a native slot may receive what it sees, and not otherwise: never while
221
+ * the browser is excluded, and never while that slot sends nothing or keeps no engagement.
220
222
  */
221
223
  const syncItems = () => {
222
224
  if (!items) return;
223
- if (!excluded && entries.some((entry) => entry.kind === "native" && allowed(entry))) items.start();
225
+ const counting = (entry) => entry.kind === "native" && (modeOf(entry) === "full" || modeOf(entry) === "cookieless");
226
+ if (!excluded && entries.some(counting)) items.start();
224
227
  else items.stop();
225
228
  };
226
229
  /** Nothing waits and nothing is measured: what an exclusion leaves of the open page. */
@@ -298,25 +301,33 @@ const createTracker = (config, options = {}) => {
298
301
  const refund = () => {
299
302
  warnDropped(BROWSER_REFUND);
300
303
  };
301
- /** Sets or clears the vendor's own opt-out, noting whether this page lifted it. */
302
- const switchVendor = (entry, optOut) => {
303
- entry.lifted = !optOut;
304
- guard(() => entry.sink.exclude?.(optOut));
304
+ /** What the vendor's own client should do now: the slot's mode, or nothing while excluded. */
305
+ const vendorMode = (entry) => {
306
+ const mode = modeOf(entry);
307
+ if (excluded || mode === "off") return "off";
308
+ return mode === "full" ? "full" : "cookieless";
309
+ };
310
+ /** Switches the vendor's own client to what the slot and the exclusion say; `refused` by a refusal. */
311
+ const switchVendor = (entry, refused = false) => {
312
+ guard(() => entry.sink.mode?.({
313
+ mode: vendorMode(entry),
314
+ refused
315
+ }));
305
316
  };
306
317
  /**
307
- * Whether the slot's vendor script may be running: the slot is or was open on this page, or the
318
+ * Whether the slot's vendor script may be running: the slot sends or sent on this page, or the
308
319
  * host loads the vendor scripts itself.
309
320
  */
310
- const mayRun = (entry, open) => open || entry.ran || !snippets;
321
+ const mayRun = (entry, sent) => sent || entry.ran || !snippets;
311
322
  /**
312
- * Mirrors the flag into each vendor's own opt-out, which is all a snippet the server rendered
313
- * obeys. Clearing reaches only an open slot: putting a browser back must never undo a refusal.
323
+ * Mirrors the flag into each vendor's own client, which is all a snippet the server rendered obeys.
324
+ * Clearing reaches only a slot that sends: putting a browser back must never undo a refusal.
314
325
  */
315
326
  const syncVendors = (next) => {
316
- for (const entry of entries) if (next || allowed(entry)) switchVendor(entry, next);
327
+ for (const entry of entries) if (next || sends(entry)) switchVendor(entry);
317
328
  };
318
329
  /**
319
- * Reads the exclusion flag and deletes beta.11's consent key, once a slot is open and never
330
+ * Reads the exclusion flag and deletes beta.11's consent key, once a slot runs in full and never
320
331
  * before, unless the link's `=0` already settled the flag.
321
332
  */
322
333
  const touchStorage = () => {
@@ -330,45 +341,63 @@ const createTracker = (config, options = {}) => {
330
341
  excluded = true;
331
342
  announceExclusion(true);
332
343
  silence();
333
- for (const entry of entries) if (mayRun(entry, allowed(entry))) switchVendor(entry, true);
344
+ for (const entry of entries) if (mayRun(entry, sends(entry))) switchVendor(entry);
345
+ };
346
+ /** Injects the slot's snippet and sends what waited, in order. */
347
+ const release = (entry) => {
348
+ load(entry);
349
+ drain(entry);
334
350
  };
335
- /** Drops what waited, and puts the refusal to the vendor itself whenever its script may be running. */
336
- const close = (entry, from) => {
337
- entry.queue.clear();
338
- entry.lifted = false;
339
- if (mayRun(entry, from === "open")) switchVendor(entry, true);
351
+ /**
352
+ * A refusal: a slot that counts without consent drops to that level, and any other drops what
353
+ * waited and puts the refusal to the vendor itself whenever its script may be running. Either
354
+ * way the vendor forgets what an earlier page's consent left on the device.
355
+ */
356
+ const close = (entry, was) => {
357
+ const sending = sends(entry);
358
+ if (sending || mayRun(entry, was !== "off")) switchVendor(entry, true);
359
+ guard(() => entry.sink.forget?.());
360
+ if (sending) release(entry);
361
+ else entry.queue.clear();
340
362
  };
341
363
  /**
342
- * Clears the vendor's own switch unless the browser is excluded (a refusal on an earlier page may
343
- * have set it), then injects the slot's snippet and sends what waited, in order.
364
+ * Lifts the vendor's own switch unless the browser is excluded (a refusal on an earlier page may
365
+ * have set it), then injects the slot's snippet and sends what waited.
344
366
  */
345
367
  const open = (entry) => {
346
368
  touchStorage();
347
- if (!excluded) switchVendor(entry, false);
348
- load(entry);
349
- drain(entry);
369
+ if (!excluded) switchVendor(entry);
370
+ release(entry);
350
371
  };
351
372
  const setGate = (state) => {
352
373
  if (destroyed) return;
374
+ const was = gate.modes;
353
375
  const moved = gate.answer(state);
354
376
  if (skipped) return;
355
- for (const { slot, from, to } of moved) {
377
+ for (const { slot, to } of moved) {
356
378
  const entry = entries.find((candidate) => candidate.slot === slot);
357
379
  if (!entry) continue;
358
380
  if (to === "open") open(entry);
359
- else close(entry, from);
381
+ else close(entry, was[slot]);
360
382
  }
361
383
  syncItems();
362
384
  };
363
385
  let restoredBy = null;
364
386
  /**
365
387
  * A page restored from the back/forward cache may have been refused in another tab since: its
366
- * held slots wait for the host to answer again, and what is sent meanwhile is queued.
388
+ * held slots return to their pending mode until the host answers again. A vendor already running
389
+ * keeps running until that answer, which switches it as any refusal would.
367
390
  */
368
391
  const restore = (event) => {
369
392
  if (restoredBy === event || !live()) return;
370
393
  restoredBy = event;
371
- if (gate.reset()) syncItems();
394
+ const was = gate.modes;
395
+ if (!gate.reset()) return;
396
+ for (const entry of entries) if (MODE_RANK[modeOf(entry)] > MODE_RANK[was[entry.slot]]) {
397
+ switchVendor(entry);
398
+ release(entry);
399
+ }
400
+ syncItems();
372
401
  };
373
402
  const onPageShow = (event) => {
374
403
  if (event.persisted) restore(event);
@@ -400,13 +429,13 @@ const createTracker = (config, options = {}) => {
400
429
  auto = null;
401
430
  for (const entry of entries) entry.queue.clear();
402
431
  };
403
- const loadOpenSlots = () => {
404
- for (const entry of entries) if (allowed(entry)) load(entry);
432
+ const loadSendingSlots = () => {
433
+ for (const entry of entries) if (sends(entry)) load(entry);
405
434
  };
406
435
  /** The boot proper, once whatever the page was opened with is settled. */
407
436
  const start = () => {
408
- if (entries.some(allowed)) touchStorage();
409
- loadOpenSlots();
437
+ if (entries.some((entry) => modeOf(entry) === "full")) touchStorage();
438
+ loadSendingSlots();
410
439
  page();
411
440
  };
412
441
  const link = config.exclusion;
@@ -454,6 +483,9 @@ const createTracker = (config, options = {}) => {
454
483
  get gate() {
455
484
  return gate.snapshot;
456
485
  },
486
+ get mode() {
487
+ return gate.modes;
488
+ },
457
489
  get excluded() {
458
490
  return excluded;
459
491
  },
@@ -467,7 +499,7 @@ const createTracker = (config, options = {}) => {
467
499
  silence();
468
500
  return;
469
501
  }
470
- loadOpenSlots();
502
+ loadSendingSlots();
471
503
  },
472
504
  destroy() {
473
505
  if (destroyed) return;
@@ -1 +1 @@
1
- {"version":3,"file":"createTracker.js","names":[],"sources":["../../src/tracker/createTracker.ts"],"sourcesContent":["import { PURCHASE_GOAL_SLUG } from '../goals/constants'\nimport { sanitizeItems } from '../goals/items'\nimport { type AutoCapture, createAutoCapture, itemCaptureOf } from './autoCapture'\nimport type { TrackerConfig } from './config'\nimport { matchesDigest } from './digest'\nimport { createEngagement, msSince } from './engagement'\nimport { campaignFields, type ItemCall, itemFields, pageContext } from './events'\nimport {\n\tannounceExclusion,\n\texclusionLinkValueOf,\n\tforgetExclusionParam,\n\treadExclusion,\n\ttakeStashedExclusion,\n\twriteExclusion,\n} from './exclusion'\nimport {\n\tALL_PENDING,\n\tcreateGate,\n\tcreateGateQueue,\n\tforgetLegacyConsent,\n\ttype GateTransition,\n\tqueueable,\n} from './gate'\nimport { createItemCapture, type ItemCapture } from './itemCapture'\nimport { createScriptLoader } from './loadScript'\nimport { createPageTracking } from './pageTracking'\nimport { createSink } from './sinks/createSink'\nimport { inLivePreview, skipReason } from './skip'\nimport type {\n\tGateState,\n\tPurchaseInput,\n\tTracker,\n\tTrackerEvent,\n\tTrackerOptions,\n\tTrackGoalOptions,\n} from './types'\nimport { currentWindow } from './window'\n\n/**\n * Development only: the call is dropped either way, and a silent no-op would read as a working\n * integration.\n */\nconst warnDropped = (message: string): void => {\n\t// biome-ignore lint/plugin: the bundler's own inlined build flag, in browser code with no env module\n\tif (typeof process !== 'undefined' && process.env?.NODE_ENV === 'production') {\n\t\treturn\n\t}\n\tconsole.warn(message)\n}\n\nconst NEGATIVE_REVENUE =\n\t'analytics: purchase() ignored a negative amount. Refunds are recorded server-side, from the payment webhook, and the browser call never sends one.'\n\nconst BROWSER_REFUND =\n\t'analytics: refund() does nothing in the browser. A refund is recorded server-side, from the payment webhook: anything a public beacon could subtract from a revenue bucket, anyone could forge.'\n\n/** Events held while the boot checks an exclusion link or the live preview, a moment at most. */\nconst HELD_LIMIT = 100\n\n/** An event that could not be delivered when it was sent, and when that was on the page's clock. */\ninterface Waiting {\n\tevent: TrackerEvent\n\tat: number\n}\n\n/** How a tracker puts its held slots back to pending after a back/forward cache restore. */\nconst restorers = new WeakMap<Tracker, (event: Event) => void>()\n\n/**\n * Resets the tracker's held slots to pending for a `pageshow` that restored the page; a second call\n * for the same event does nothing, so the registry and the tracker may both listen.\n */\nexport const restoreGate = (tracker: Tracker, event: Event): void => {\n\trestorers.get(tracker)?.(event)\n}\n\n/** Stand-in for a host with no DOM (server rendering, a worker). Every call is a no-op. */\nexport const createNoopTracker = (): Tracker => ({\n\tpage: () => undefined,\n\ttrack: () => undefined,\n\ttrackGoal: () => undefined,\n\tpurchase: () => undefined,\n\trefund: () => undefined,\n\tsetGate: () => undefined,\n\tgate: ALL_PENDING,\n\texcluded: false,\n\texclude: () => undefined,\n\tflush: () => undefined,\n\tdestroy: () => undefined,\n})\n\n/**\n * The tracker of a page nobody should count: it sends and injects nothing, and its gate still\n * takes the host's answers, so a consent UI reads them back as on any other page.\n */\nconst createSkippedTracker = (config: TrackerConfig, boot: GateState | undefined): Tracker => {\n\tconst gate = createGate(config.slots)\n\tif (boot !== undefined) {\n\t\tgate.answer(boot)\n\t}\n\tconst tracker: Tracker = {\n\t\t...createNoopTracker(),\n\t\tsetGate: (state) => {\n\t\t\tgate.answer(state)\n\t\t},\n\t\tget gate() {\n\t\t\treturn gate.snapshot\n\t\t},\n\t}\n\trestorers.set(tracker, () => gate.reset())\n\treturn tracker\n}\n\n/**\n * The browser tracker: one sink per slot behind a gate the host's consent manager answers, kept in\n * memory alone. No storage is touched before a slot opens, the exclusion link's own write aside.\n */\nexport const createTracker = (config: TrackerConfig, options: TrackerOptions = {}): Tracker => {\n\tconst win = currentWindow(options.window)\n\tif (!win) {\n\t\treturn createNoopTracker()\n\t}\n\t// A page nobody should count sends nothing and injects nothing; the link still leaves the address.\n\tif (skipReason(config, win) !== null) {\n\t\ttakeStashedExclusion(win)\n\t\tif (config.exclusion) {\n\t\t\tforgetExclusionParam(win, config.exclusion.param)\n\t\t}\n\t\treturn createSkippedTracker(config, options.gate)\n\t}\n\tconst snippets = options.snippets !== false\n\tconst loadScript = options.loadScript ?? createScriptLoader(win, options.nonce)\n\tlet auto: AutoCapture | null = null\n\t/** False until the stored flag is read, which waits for the first slot to open. */\n\tlet excluded = false\n\t/** Whether the stored flag was read, or made moot by an explicit exclusion on this page. */\n\tlet flagKnown = false\n\tlet legacyForgotten = false\n\t/**\n\t * Events sent while the boot checks an exclusion token or the live preview, null otherwise:\n\t * delivered once both checks pass, dropped when either fails.\n\t */\n\tlet held: Waiting[] | null = null\n\tlet destroyed = false\n\t/** The page turned out to be the live preview: nothing more is sent, and the gate still answers. */\n\tlet skipped = false\n\n\tconst entries = config.slots.map((slot) => ({\n\t\tslot: slot.slot,\n\t\tkind: slot.kind,\n\t\t/** What waits while the slot's gate is pending, or while the page is only prerendered. */\n\t\tqueue: createGateQueue<Waiting>(),\n\t\t/**\n\t\t * Whether an opening or an explicit inclusion cleared this vendor's own opt-out on this page,\n\t\t * which the slot's scripts having run then clears once more.\n\t\t */\n\t\tlifted: false,\n\t\t/** Whether the slot was open on this page, so its script may be running whatever its gate says now. */\n\t\tran: false,\n\t\tsink: createSink({ slot, win, loadScript, ingestPath: config.ingestPath, snippets }),\n\t}))\n\n\ttype Entry = (typeof entries)[number]\n\n\tconst gate = createGate(config.slots)\n\n\tconst allowed = (entry: Entry): boolean => gate.status(entry.slot) === 'open'\n\n\tconst live = (): boolean => !destroyed && !skipped\n\n\tconst now = (): number => win.performance.now()\n\n\t/**\n\t * A throwing vendor global or an unserializable event must not skip the other slots or escape into\n\t * the host's click handler.\n\t */\n\tconst guard = (run: () => void) => {\n\t\ttry {\n\t\t\trun()\n\t\t} catch {\n\t\t\t// Dropped, like every other delivery failure.\n\t\t}\n\t}\n\n\tlet activation: (() => void) | null = null\n\n\t/**\n\t * Whether the browser only prerenders the page, which a visitor may never open. Nothing loads,\n\t * no vendor is called and no pageview is sent until it is shown, when what waited runs once.\n\t */\n\tconst waitUntilShown = (): boolean => {\n\t\tif (Reflect.get(win.document, 'prerendering') !== true) {\n\t\t\treturn false\n\t\t}\n\t\tif (activation === null) {\n\t\t\tactivation = () => {\n\t\t\t\tactivation = null\n\t\t\t\tif (!live()) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tfor (const entry of entries) {\n\t\t\t\t\tif (allowed(entry)) {\n\t\t\t\t\t\tload(entry)\n\t\t\t\t\t\tdrain(entry)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpage()\n\t\t\t}\n\t\t\twin.document.addEventListener('prerenderingchange', activation, { once: true })\n\t\t}\n\t\treturn true\n\t}\n\n\t/**\n\t * Injects the slot's scripts, then clears the vendor's opt-out once more when this page lifted\n\t * it: a vendor whose snippet defines its global (PostHog) had none to clear before the snippet ran.\n\t */\n\tconst load = (entry: Entry) => {\n\t\tif (excluded || held !== null || !live() || waitUntilShown()) {\n\t\t\treturn\n\t\t}\n\t\tentry.ran = true\n\t\tvoid entry.sink\n\t\t\t.ready()\n\t\t\t.then(() => {\n\t\t\t\tif (entry.lifted && !excluded && allowed(entry) && live()) {\n\t\t\t\t\tswitchVendor(entry, false)\n\t\t\t\t}\n\t\t\t})\n\t\t\t.catch(() => undefined)\n\t}\n\n\t/** The event as sent, with how long it waited when it waited at all. */\n\tconst aged = (event: TrackerEvent, at: number | undefined): TrackerEvent => {\n\t\tconst ageMs = at === undefined ? 0 : msSince(win, at)\n\t\treturn ageMs > 0 ? { ...event, ageMs } : event\n\t}\n\n\t/** Queues what the slot's queue may hold of the event, stamped with when it was first sent. */\n\tconst hold = (entry: Entry, event: TrackerEvent, at: number | undefined) => {\n\t\tconst waiting = queueable(event, entry.kind)\n\t\tif (waiting) {\n\t\t\tentry.queue.push({ event: waiting, at: at ?? now() })\n\t\t}\n\t}\n\n\t/** A vendor is called only once the page is shown, since calling it loads its script. */\n\tconst deliver = (entry: Entry, event: TrackerEvent, at?: number) => {\n\t\tif (entry.kind !== 'native' && waitUntilShown()) {\n\t\t\thold(entry, event, at)\n\t\t\treturn\n\t\t}\n\t\tguard(() => entry.sink.send(aged(event, at)))\n\t}\n\n\tconst drain = (entry: Entry) => {\n\t\tfor (const { event, at } of entry.queue.drain()) {\n\t\t\tdeliver(entry, event, at)\n\t\t}\n\t}\n\n\t/** `at` is when an event that waited was first sent; one delivered as it is sent has none. */\n\tconst dispatch = (event: TrackerEvent, at?: number) => {\n\t\tif (!live()) {\n\t\t\treturn\n\t\t}\n\t\tif (held !== null) {\n\t\t\tif (held.length < HELD_LIMIT) {\n\t\t\t\theld.push({ event, at: at ?? now() })\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (excluded) {\n\t\t\treturn\n\t\t}\n\t\tfor (const entry of entries) {\n\t\t\tconst status = gate.status(entry.slot)\n\t\t\tif (status === 'open') {\n\t\t\t\tdeliver(entry, event, at)\n\t\t\t} else if (status === 'pending') {\n\t\t\t\thold(entry, event, at)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Only the native sink counts what was seen, and only while it may be sent anything at all.\n\tconst items: ItemCapture | null = itemCaptureOf(config.autoCapture).impressions\n\t\t? createItemCapture({ win, onFull: () => engagement.send() })\n\t\t: null\n\n\tconst engagement = createEngagement({\n\t\twin,\n\t\tscrollDepth: () => auto?.scrollDepth(),\n\t\ttakeImpressions: () => items?.take() ?? [],\n\t\tdispatch: (event) => dispatch(event),\n\t})\n\n\t/**\n\t * Watches for items while a native slot may receive what it sees, and not otherwise: never\n\t * while the browser is excluded, and never while that slot's gate is not open.\n\t */\n\tconst syncItems = () => {\n\t\tif (!items) {\n\t\t\treturn\n\t\t}\n\t\tif (!excluded && entries.some((entry) => entry.kind === 'native' && allowed(entry))) {\n\t\t\titems.start()\n\t\t} else {\n\t\t\titems.stop()\n\t\t}\n\t}\n\n\t/** Nothing waits and nothing is measured: what an exclusion leaves of the open page. */\n\tconst silence = () => {\n\t\tengagement.stop()\n\t\titems?.stop()\n\t\tfor (const entry of entries) {\n\t\t\tentry.queue.clear()\n\t\t}\n\t}\n\n\t/**\n\t * Whether this tracker sent its document's pageview: every later one is a single-page app's\n\t * navigation, whose referrer is still the one the document was loaded with.\n\t */\n\tlet navigating = false\n\n\tconst page = () => {\n\t\t// While the boot is still checking, its release tracks the page instead.\n\t\tif (!live() || held !== null || waitUntilShown()) {\n\t\t\treturn\n\t\t}\n\t\tengagement.send()\n\t\tconst view = { ...pageContext(win), ...campaignFields(win, config.autoCapture) }\n\t\tdispatch({\n\t\t\ttype: 'pageview',\n\t\t\t...view,\n\t\t\t...(navigating ? { nav: 1 } : {}),\n\t\t\t...(config.autoCapture.scrollDepth ? { scrollDepth: 0 } : {}),\n\t\t})\n\t\tnavigating = true\n\t\tauto?.resetPage()\n\t\t// Like time and scroll, items seen are counted again from the first pageview after an\n\t\t// exclusion is cleared, never from the middle of the page it was cleared on.\n\t\titems?.resetPage()\n\t\tsyncItems()\n\t\tif (excluded) {\n\t\t\tengagement.stop()\n\t\t} else {\n\t\t\tengagement.start(view)\n\t\t}\n\t}\n\n\tconst track = (name: string, props?: Record<string, unknown>, opts?: ItemCall) => {\n\t\tdispatch({\n\t\t\ttype: 'event',\n\t\t\tname,\n\t\t\t...pageContext(win),\n\t\t\t...(props ? { props } : {}),\n\t\t\t...itemFields(opts),\n\t\t})\n\t}\n\n\tconst trackGoal = (slug: string, opts?: TrackGoalOptions & ItemCall) => {\n\t\tconst goal = config.goals.find((candidate) => candidate.slug === slug)\n\t\tconst value = opts?.value ?? goal?.value?.fixed\n\t\tconst currency = opts?.currency ?? goal?.currency\n\t\tdispatch({\n\t\t\ttype: 'goal',\n\t\t\tname: slug,\n\t\t\t...pageContext(win),\n\t\t\t...(value === undefined ? {} : { value }),\n\t\t\t...(currency === undefined ? {} : { currency }),\n\t\t\t...itemFields(opts),\n\t\t})\n\t}\n\n\tconst purchase = (input: PurchaseInput) => {\n\t\t// Not `< 0`, so NaN falls here too: refunds are server-side and ingest refuses a negative\n\t\t// amount, so sending one is a bug to hear about rather than a beacon to spend.\n\t\tif (!(input.value >= 0)) {\n\t\t\twarnDropped(NEGATIVE_REVENUE)\n\t\t\treturn\n\t\t}\n\t\tconst goal = input.goal ?? PURCHASE_GOAL_SLUG\n\t\tconst lines = sanitizeItems(input.items)\n\t\t// The order id rides on its own field: native keeps a digest of it, and each vendor sink maps\n\t\t// it to that vendor's own field.\n\t\tdispatch({\n\t\t\ttype: 'goal',\n\t\t\tname: goal,\n\t\t\t...pageContext(win),\n\t\t\tvalue: input.value,\n\t\t\t...(input.currency === undefined ? {} : { currency: input.currency }),\n\t\t\t...(input.orderId === undefined ? {} : { orderId: input.orderId }),\n\t\t\t...(lines === undefined ? {} : { items: lines }),\n\t\t\t...(input.props ? { props: input.props } : {}),\n\t\t\t...itemFields(input),\n\t\t})\n\t}\n\n\tconst refund = () => {\n\t\twarnDropped(BROWSER_REFUND)\n\t}\n\n\t/** Sets or clears the vendor's own opt-out, noting whether this page lifted it. */\n\tconst switchVendor = (entry: Entry, optOut: boolean) => {\n\t\tentry.lifted = !optOut\n\t\tguard(() => entry.sink.exclude?.(optOut))\n\t}\n\n\t/**\n\t * Whether the slot's vendor script may be running: the slot is or was open on this page, or the\n\t * host loads the vendor scripts itself.\n\t */\n\tconst mayRun = (entry: Entry, open: boolean): boolean => open || entry.ran || !snippets\n\n\t/**\n\t * Mirrors the flag into each vendor's own opt-out, which is all a snippet the server rendered\n\t * obeys. Clearing reaches only an open slot: putting a browser back must never undo a refusal.\n\t */\n\tconst syncVendors = (next: boolean) => {\n\t\tfor (const entry of entries) {\n\t\t\tif (next || allowed(entry)) {\n\t\t\t\tswitchVendor(entry, next)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Reads the exclusion flag and deletes beta.11's consent key, once a slot is open and never\n\t * before, unless the link's `=0` already settled the flag.\n\t */\n\tconst touchStorage = () => {\n\t\tif (!legacyForgotten) {\n\t\t\tlegacyForgotten = true\n\t\t\tforgetLegacyConsent(win)\n\t\t}\n\t\tif (flagKnown) {\n\t\t\treturn\n\t\t}\n\t\tflagKnown = true\n\t\tif (!readExclusion(win)) {\n\t\t\treturn\n\t\t}\n\t\texcluded = true\n\t\tannounceExclusion(true)\n\t\tsilence()\n\t\tfor (const entry of entries) {\n\t\t\tif (mayRun(entry, allowed(entry))) {\n\t\t\t\tswitchVendor(entry, true)\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Drops what waited, and puts the refusal to the vendor itself whenever its script may be running. */\n\tconst close = (entry: Entry, from: GateTransition['from']) => {\n\t\tentry.queue.clear()\n\t\tentry.lifted = false\n\t\tif (mayRun(entry, from === 'open')) {\n\t\t\tswitchVendor(entry, true)\n\t\t}\n\t}\n\n\t/**\n\t * Clears the vendor's own switch unless the browser is excluded (a refusal on an earlier page may\n\t * have set it), then injects the slot's snippet and sends what waited, in order.\n\t */\n\tconst open = (entry: Entry) => {\n\t\ttouchStorage()\n\t\tif (!excluded) {\n\t\t\tswitchVendor(entry, false)\n\t\t}\n\t\tload(entry)\n\t\tdrain(entry)\n\t}\n\n\tconst setGate = (state: GateState) => {\n\t\tif (destroyed) {\n\t\t\treturn\n\t\t}\n\t\tconst moved = gate.answer(state)\n\t\tif (skipped) {\n\t\t\treturn\n\t\t}\n\t\tfor (const { slot, from, to } of moved) {\n\t\t\tconst entry = entries.find((candidate) => candidate.slot === slot)\n\t\t\tif (!entry) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (to === 'open') {\n\t\t\t\topen(entry)\n\t\t\t} else {\n\t\t\t\tclose(entry, from)\n\t\t\t}\n\t\t}\n\t\tsyncItems()\n\t}\n\n\tlet restoredBy: Event | null = null\n\n\t/**\n\t * A page restored from the back/forward cache may have been refused in another tab since: its\n\t * held slots wait for the host to answer again, and what is sent meanwhile is queued.\n\t */\n\tconst restore = (event: Event) => {\n\t\tif (restoredBy === event || !live()) {\n\t\t\treturn\n\t\t}\n\t\trestoredBy = event\n\t\tif (gate.reset()) {\n\t\t\tsyncItems()\n\t\t}\n\t}\n\n\tconst onPageShow = (event: PageTransitionEvent) => {\n\t\tif (event.persisted) {\n\t\t\trestore(event)\n\t\t}\n\t}\n\n\tauto = createAutoCapture({\n\t\twin,\n\t\toptions: config.autoCapture,\n\t\thandlers: { track, trackGoal },\n\t})\n\tconst pageTracking = createPageTracking(win, page)\n\twin.addEventListener('pageshow', onPageShow)\n\n\tlet detached = false\n\t/** Every listener and history patch off, and nothing left waiting. */\n\tconst detach = () => {\n\t\tif (detached) {\n\t\t\treturn\n\t\t}\n\t\tdetached = true\n\t\tif (activation !== null) {\n\t\t\twin.document.removeEventListener('prerenderingchange', activation)\n\t\t\tactivation = null\n\t\t}\n\t\twin.removeEventListener('pageshow', onPageShow)\n\t\tengagement.destroy()\n\t\titems?.destroy()\n\t\tpageTracking.destroy()\n\t\tauto?.destroy()\n\t\tauto = null\n\t\tfor (const entry of entries) {\n\t\t\tentry.queue.clear()\n\t\t}\n\t}\n\n\tconst loadOpenSlots = () => {\n\t\tfor (const entry of entries) {\n\t\t\tif (allowed(entry)) {\n\t\t\t\tload(entry)\n\t\t\t}\n\t\t}\n\t}\n\n\t/** The boot proper, once whatever the page was opened with is settled. */\n\tconst start = () => {\n\t\tif (entries.some(allowed)) {\n\t\t\ttouchStorage()\n\t\t}\n\t\tloadOpenSlots()\n\t\tpage()\n\t}\n\n\t// Read only while links are handed out, and stripped from the address at once so no pageview or\n\t// copied link carries it; a server-rendered snippet may have stripped it already.\n\tconst link = config.exclusion\n\tconst stashed = takeStashedExclusion(win)\n\tconst asked = link\n\t\t? exclusionLinkValueOf(stashed ?? new URLSearchParams(win.location.search).get(link.param))\n\t\t: null\n\tif (link) {\n\t\tforgetExclusionParam(win, link.param)\n\t}\n\t// As explicit as a signed link, `=0` needs no open slot; only an excluded browser has switches to lift.\n\tif (asked === 'include') {\n\t\tflagKnown = true\n\t\tif (readExclusion(win)) {\n\t\t\twriteExclusion(win, false)\n\t\t\tsyncVendors(false)\n\t\t}\n\t}\n\tconst token = link && asked !== null && asked !== 'include' ? asked.token : null\n\tconst shown = inLivePreview(config, win)\n\t// Nothing leaves the page until both are settled, so the first page a staff member opens the\n\t// link on is not counted, and neither is a preview while it is being recognised.\n\tif (token !== null || shown !== null) {\n\t\theld = []\n\t}\n\t// Before anything is delivered, so the boot pageview goes where the host's answer sends it.\n\tif (options.gate !== undefined) {\n\t\tsetGate(options.gate)\n\t}\n\tif (held === null) {\n\t\tstart()\n\t} else {\n\t\tvoid Promise.all([\n\t\t\ttoken !== null && link ? matchesDigest(win, token, link.tokenHash) : false,\n\t\t\tshown ?? false,\n\t\t]).then(([valid, preview]) => {\n\t\t\tconst waiting = held ?? []\n\t\t\theld = null\n\t\t\tif (destroyed) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (preview) {\n\t\t\t\tskipped = true\n\t\t\t\tdetach()\n\t\t\t\treturn\n\t\t\t}\n\t\t\t// The staff member's own link: the one write that needs no open slot.\n\t\t\tif (valid) {\n\t\t\t\texcluded = true\n\t\t\t\tflagKnown = true\n\t\t\t\twriteExclusion(win, true)\n\t\t\t\tsyncVendors(true)\n\t\t\t}\n\t\t\tstart()\n\t\t\tfor (const { event, at } of waiting) {\n\t\t\t\tdispatch(event, at)\n\t\t\t}\n\t\t})\n\t}\n\n\tconst tracker: Tracker = {\n\t\tpage,\n\t\ttrack,\n\t\ttrackGoal,\n\t\tpurchase,\n\t\trefund,\n\t\tflush: () => engagement.send(),\n\t\tsetGate,\n\t\tget gate() {\n\t\t\treturn gate.snapshot\n\t\t},\n\t\tget excluded() {\n\t\t\treturn excluded\n\t\t},\n\t\texclude(next) {\n\t\t\texcluded = next\n\t\t\tflagKnown = true\n\t\t\twriteExclusion(win, next)\n\t\t\tif (!live()) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tsyncVendors(next)\n\t\t\tif (next) {\n\t\t\t\tsilence()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tloadOpenSlots()\n\t\t},\n\t\tdestroy() {\n\t\t\tif (destroyed) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tengagement.send()\n\t\t\tdestroyed = true\n\t\t\theld = null\n\t\t\tdetach()\n\t\t},\n\t}\n\trestorers.set(tracker, restore)\n\treturn tracker\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0CA,MAAM,eAAe,YAA0B;CAE9C,IAAI,OAAO,YAAY,eAAe,QAAQ,KAAK,aAAa,cAC/D;CAED,QAAQ,KAAK,OAAO;AACrB;AAEA,MAAM,mBACL;AAED,MAAM,iBACL;;AAGD,MAAM,aAAa;;AASnB,MAAM,4BAAY,IAAI,QAAyC;;;;;AAM/D,MAAa,eAAe,SAAkB,UAAuB;CACpE,UAAU,IAAI,OAAO,IAAI,KAAK;AAC/B;;AAGA,MAAa,2BAAoC;CAChD,YAAY,KAAA;CACZ,aAAa,KAAA;CACb,iBAAiB,KAAA;CACjB,gBAAgB,KAAA;CAChB,cAAc,KAAA;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ACP,OAAO;EACR;CACD;CACA,UAAU,IAAI,SAAS,OAAO;CAC9B,OAAO;AACR"}
1
+ {"version":3,"file":"createTracker.js","names":[],"sources":["../../src/tracker/createTracker.ts"],"sourcesContent":["import { PURCHASE_GOAL_SLUG } from '../goals/constants'\nimport { sanitizeItems } from '../goals/items'\nimport { type AutoCapture, createAutoCapture, itemCaptureOf } from './autoCapture'\nimport type { SlotMode, TrackerConfig } from './config'\nimport { matchesDigest } from './digest'\nimport { createEngagement, msSince } from './engagement'\nimport { campaignFields, type ItemCall, itemFields, pageContext } from './events'\nimport {\n\tannounceExclusion,\n\texclusionLinkValueOf,\n\tforgetExclusionParam,\n\treadExclusion,\n\ttakeStashedExclusion,\n\twriteExclusion,\n} from './exclusion'\nimport {\n\tALL_OFF,\n\tALL_PENDING,\n\tcreateGate,\n\tcreateGateQueue,\n\tforgetLegacyConsent,\n\tqueueable,\n} from './gate'\nimport { createItemCapture, type ItemCapture } from './itemCapture'\nimport { createScriptLoader } from './loadScript'\nimport { createPageTracking } from './pageTracking'\nimport { createSink } from './sinks/createSink'\nimport { inLivePreview, skipReason } from './skip'\nimport type {\n\tGateState,\n\tPurchaseInput,\n\tTracker,\n\tTrackerEvent,\n\tTrackerOptions,\n\tTrackGoalOptions,\n\tVendorMode,\n} from './types'\nimport { currentWindow } from './window'\n\n/**\n * Development only: the call is dropped either way, and a silent no-op would read as a working\n * integration.\n */\nconst warnDropped = (message: string): void => {\n\t// biome-ignore lint/plugin: the bundler's own inlined build flag, in browser code with no env module\n\tif (typeof process !== 'undefined' && process.env?.NODE_ENV === 'production') {\n\t\treturn\n\t}\n\tconsole.warn(message)\n}\n\nconst NEGATIVE_REVENUE =\n\t'analytics: purchase() ignored a negative amount. Refunds are recorded server-side, from the payment webhook, and the browser call never sends one.'\n\nconst BROWSER_REFUND =\n\t'analytics: refund() does nothing in the browser. A refund is recorded server-side, from the payment webhook: anything a public beacon could subtract from a revenue bucket, anyone could forge.'\n\n/** Events held while the boot checks an exclusion link or the live preview, a moment at most. */\nconst HELD_LIMIT = 100\n\n/** An event that could not be delivered when it was sent, and when that was on the page's clock. */\ninterface Waiting {\n\tevent: TrackerEvent\n\tat: number\n}\n\n/** How far each mode goes, for a restore that must never lower a vendor already running. */\nconst MODE_RANK: Readonly<Record<SlotMode, number>> = {\n\toff: 0,\n\tanonymous: 1,\n\tcookieless: 2,\n\tfull: 3,\n}\n\n/** How a tracker puts its held slots back to pending after a back/forward cache restore. */\nconst restorers = new WeakMap<Tracker, (event: Event) => void>()\n\n/**\n * Resets the tracker's held slots to pending for a `pageshow` that restored the page; a second call\n * for the same event does nothing, so the registry and the tracker may both listen.\n */\nexport const restoreGate = (tracker: Tracker, event: Event): void => {\n\trestorers.get(tracker)?.(event)\n}\n\n/** Stand-in for a host with no DOM (server rendering, a worker). Every call is a no-op. */\nexport const createNoopTracker = (): Tracker => ({\n\tpage: () => undefined,\n\ttrack: () => undefined,\n\ttrackGoal: () => undefined,\n\tpurchase: () => undefined,\n\trefund: () => undefined,\n\tsetGate: () => undefined,\n\tgate: ALL_PENDING,\n\tmode: ALL_OFF,\n\texcluded: false,\n\texclude: () => undefined,\n\tflush: () => undefined,\n\tdestroy: () => undefined,\n})\n\n/**\n * The tracker of a page nobody should count: it sends and injects nothing, and its gate still\n * takes the host's answers, so a consent UI reads them back as on any other page.\n */\nconst createSkippedTracker = (config: TrackerConfig, boot: GateState | undefined): Tracker => {\n\tconst gate = createGate(config.slots)\n\tif (boot !== undefined) {\n\t\tgate.answer(boot)\n\t}\n\tconst tracker: Tracker = {\n\t\t...createNoopTracker(),\n\t\tsetGate: (state) => {\n\t\t\tgate.answer(state)\n\t\t},\n\t\tget gate() {\n\t\t\treturn gate.snapshot\n\t\t},\n\t\tget mode() {\n\t\t\treturn gate.modes\n\t\t},\n\t}\n\trestorers.set(tracker, () => gate.reset())\n\treturn tracker\n}\n\n/**\n * The browser tracker: one sink per slot behind a gate the host's consent manager answers, kept in\n * memory alone, each slot capturing in the mode its gate gives it. No storage is touched before a\n * slot runs in full, the exclusion link's own aside.\n */\nexport const createTracker = (config: TrackerConfig, options: TrackerOptions = {}): Tracker => {\n\tconst win = currentWindow(options.window)\n\tif (!win) {\n\t\treturn createNoopTracker()\n\t}\n\t// A page nobody should count sends nothing and injects nothing; the link still leaves the address.\n\tif (skipReason(config, win) !== null) {\n\t\ttakeStashedExclusion(win)\n\t\tif (config.exclusion) {\n\t\t\tforgetExclusionParam(win, config.exclusion.param)\n\t\t}\n\t\treturn createSkippedTracker(config, options.gate)\n\t}\n\tconst snippets = options.snippets !== false\n\tconst loadScript = options.loadScript ?? createScriptLoader(win, options.nonce)\n\tlet auto: AutoCapture | null = null\n\t/** False until the stored flag is read, which waits for the first slot to run in full. */\n\tlet excluded = false\n\t/** Whether the stored flag was read, or made moot by an explicit exclusion on this page. */\n\tlet flagKnown = false\n\tlet legacyForgotten = false\n\t/**\n\t * Events sent while the boot checks an exclusion token or the live preview, null otherwise:\n\t * delivered once both checks pass, dropped when either fails.\n\t */\n\tlet held: Waiting[] | null = null\n\tlet destroyed = false\n\t/** The page turned out to be the live preview: nothing more is sent, and the gate still answers. */\n\tlet skipped = false\n\n\tconst gate = createGate(config.slots)\n\n\tconst entries = config.slots.map((slot) => ({\n\t\tslot: slot.slot,\n\t\tkind: slot.kind,\n\t\t/** What waits while the slot is pending and sends nothing, or while the page is only prerendered. */\n\t\tqueue: createGateQueue<Waiting>(),\n\t\t/** Whether the slot's scripts were loaded on this page, so they may be running whatever its mode says now. */\n\t\tran: false,\n\t\tsink: createSink({\n\t\t\tslot,\n\t\t\twin,\n\t\t\tloadScript,\n\t\t\tingestPath: config.ingestPath,\n\t\t\tsnippets,\n\t\t\tmode: () => gate.mode(slot.slot),\n\t\t}),\n\t}))\n\n\ttype Entry = (typeof entries)[number]\n\n\tconst modeOf = (entry: Entry): SlotMode => gate.mode(entry.slot)\n\n\t/** Whether the slot sends what it is given now, in full or without consent. */\n\tconst sends = (entry: Entry): boolean => modeOf(entry) !== 'off'\n\n\tconst live = (): boolean => !destroyed && !skipped\n\n\tconst now = (): number => win.performance.now()\n\n\t/**\n\t * A throwing vendor global or an unserializable event must not skip the other slots or escape into\n\t * the host's click handler.\n\t */\n\tconst guard = (run: () => void) => {\n\t\ttry {\n\t\t\trun()\n\t\t} catch {\n\t\t\t// Dropped, like every other delivery failure.\n\t\t}\n\t}\n\n\tlet activation: (() => void) | null = null\n\n\t/**\n\t * Whether the browser only prerenders the page, which a visitor may never open. Nothing loads,\n\t * no vendor is called and no pageview is sent until it is shown, when what waited runs once.\n\t */\n\tconst waitUntilShown = (): boolean => {\n\t\tif (Reflect.get(win.document, 'prerendering') !== true) {\n\t\t\treturn false\n\t\t}\n\t\tif (activation === null) {\n\t\t\tactivation = () => {\n\t\t\t\tactivation = null\n\t\t\t\tif (!live()) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tfor (const entry of entries) {\n\t\t\t\t\tif (sends(entry)) {\n\t\t\t\t\t\tload(entry)\n\t\t\t\t\t\tdrain(entry)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpage()\n\t\t\t}\n\t\t\twin.document.addEventListener('prerenderingchange', activation, { once: true })\n\t\t}\n\t\treturn true\n\t}\n\n\t/** Injects the slot's scripts; its sink switches the vendor once more when they ran. */\n\tconst load = (entry: Entry) => {\n\t\tif (excluded || held !== null || !live() || waitUntilShown()) {\n\t\t\treturn\n\t\t}\n\t\tentry.ran = true\n\t\tvoid entry.sink.ready().catch(() => undefined)\n\t}\n\n\t/** The event as sent, with how long it waited when it waited at all. */\n\tconst aged = (event: TrackerEvent, at: number | undefined): TrackerEvent => {\n\t\tconst ageMs = at === undefined ? 0 : msSince(win, at)\n\t\treturn ageMs > 0 ? { ...event, ageMs } : event\n\t}\n\n\t/** Queues what the slot's queue may hold of the event, stamped with when it was first sent. */\n\tconst hold = (entry: Entry, event: TrackerEvent, at: number | undefined) => {\n\t\tconst waiting = queueable(event, entry.kind)\n\t\tif (waiting) {\n\t\t\tentry.queue.push({ event: waiting, at: at ?? now() })\n\t\t}\n\t}\n\n\t/** A vendor is called only once the page is shown, since calling it loads its script. */\n\tconst deliver = (entry: Entry, event: TrackerEvent, at?: number) => {\n\t\tif (entry.kind !== 'native' && waitUntilShown()) {\n\t\t\thold(entry, event, at)\n\t\t\treturn\n\t\t}\n\t\tguard(() => entry.sink.send(aged(event, at)))\n\t}\n\n\tconst drain = (entry: Entry) => {\n\t\tfor (const { event, at } of entry.queue.drain()) {\n\t\t\tdeliver(entry, event, at)\n\t\t}\n\t}\n\n\t/** `at` is when an event that waited was first sent; one delivered as it is sent has none. */\n\tconst dispatch = (event: TrackerEvent, at?: number) => {\n\t\tif (!live()) {\n\t\t\treturn\n\t\t}\n\t\tif (held !== null) {\n\t\t\tif (held.length < HELD_LIMIT) {\n\t\t\t\theld.push({ event, at: at ?? now() })\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (excluded) {\n\t\t\treturn\n\t\t}\n\t\tfor (const entry of entries) {\n\t\t\tif (sends(entry)) {\n\t\t\t\tdeliver(entry, event, at)\n\t\t\t} else if (gate.status(entry.slot) === 'pending') {\n\t\t\t\thold(entry, event, at)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Only the native sink counts what was seen, and only while it may be sent anything at all.\n\tconst items: ItemCapture | null = itemCaptureOf(config.autoCapture).impressions\n\t\t? createItemCapture({ win, onFull: () => engagement.send() })\n\t\t: null\n\n\tconst engagement = createEngagement({\n\t\twin,\n\t\tscrollDepth: () => auto?.scrollDepth(),\n\t\ttakeImpressions: () => items?.take() ?? [],\n\t\tdispatch: (event) => dispatch(event),\n\t})\n\n\t/**\n\t * Watches for items while a native slot may receive what it sees, and not otherwise: never while\n\t * the browser is excluded, and never while that slot sends nothing or keeps no engagement.\n\t */\n\tconst syncItems = () => {\n\t\tif (!items) {\n\t\t\treturn\n\t\t}\n\t\tconst counting = (entry: Entry) =>\n\t\t\tentry.kind === 'native' && (modeOf(entry) === 'full' || modeOf(entry) === 'cookieless')\n\t\tif (!excluded && entries.some(counting)) {\n\t\t\titems.start()\n\t\t} else {\n\t\t\titems.stop()\n\t\t}\n\t}\n\n\t/** Nothing waits and nothing is measured: what an exclusion leaves of the open page. */\n\tconst silence = () => {\n\t\tengagement.stop()\n\t\titems?.stop()\n\t\tfor (const entry of entries) {\n\t\t\tentry.queue.clear()\n\t\t}\n\t}\n\n\t/**\n\t * Whether this tracker sent its document's pageview: every later one is a single-page app's\n\t * navigation, whose referrer is still the one the document was loaded with.\n\t */\n\tlet navigating = false\n\n\tconst page = () => {\n\t\t// While the boot is still checking, its release tracks the page instead.\n\t\tif (!live() || held !== null || waitUntilShown()) {\n\t\t\treturn\n\t\t}\n\t\tengagement.send()\n\t\tconst view = { ...pageContext(win), ...campaignFields(win, config.autoCapture) }\n\t\tdispatch({\n\t\t\ttype: 'pageview',\n\t\t\t...view,\n\t\t\t...(navigating ? { nav: 1 } : {}),\n\t\t\t...(config.autoCapture.scrollDepth ? { scrollDepth: 0 } : {}),\n\t\t})\n\t\tnavigating = true\n\t\tauto?.resetPage()\n\t\t// Like time and scroll, items seen are counted again from the first pageview after an\n\t\t// exclusion is cleared, never from the middle of the page it was cleared on.\n\t\titems?.resetPage()\n\t\tsyncItems()\n\t\tif (excluded) {\n\t\t\tengagement.stop()\n\t\t} else {\n\t\t\tengagement.start(view)\n\t\t}\n\t}\n\n\tconst track = (name: string, props?: Record<string, unknown>, opts?: ItemCall) => {\n\t\tdispatch({\n\t\t\ttype: 'event',\n\t\t\tname,\n\t\t\t...pageContext(win),\n\t\t\t...(props ? { props } : {}),\n\t\t\t...itemFields(opts),\n\t\t})\n\t}\n\n\tconst trackGoal = (slug: string, opts?: TrackGoalOptions & ItemCall) => {\n\t\tconst goal = config.goals.find((candidate) => candidate.slug === slug)\n\t\tconst value = opts?.value ?? goal?.value?.fixed\n\t\tconst currency = opts?.currency ?? goal?.currency\n\t\tdispatch({\n\t\t\ttype: 'goal',\n\t\t\tname: slug,\n\t\t\t...pageContext(win),\n\t\t\t...(value === undefined ? {} : { value }),\n\t\t\t...(currency === undefined ? {} : { currency }),\n\t\t\t...itemFields(opts),\n\t\t})\n\t}\n\n\tconst purchase = (input: PurchaseInput) => {\n\t\t// Not `< 0`, so NaN falls here too: refunds are server-side and ingest refuses a negative\n\t\t// amount, so sending one is a bug to hear about rather than a beacon to spend.\n\t\tif (!(input.value >= 0)) {\n\t\t\twarnDropped(NEGATIVE_REVENUE)\n\t\t\treturn\n\t\t}\n\t\tconst goal = input.goal ?? PURCHASE_GOAL_SLUG\n\t\tconst lines = sanitizeItems(input.items)\n\t\t// The order id rides on its own field: native keeps a digest of it, and each vendor sink maps\n\t\t// it to that vendor's own field.\n\t\tdispatch({\n\t\t\ttype: 'goal',\n\t\t\tname: goal,\n\t\t\t...pageContext(win),\n\t\t\tvalue: input.value,\n\t\t\t...(input.currency === undefined ? {} : { currency: input.currency }),\n\t\t\t...(input.orderId === undefined ? {} : { orderId: input.orderId }),\n\t\t\t...(lines === undefined ? {} : { items: lines }),\n\t\t\t...(input.props ? { props: input.props } : {}),\n\t\t\t...itemFields(input),\n\t\t})\n\t}\n\n\tconst refund = () => {\n\t\twarnDropped(BROWSER_REFUND)\n\t}\n\n\t/** What the vendor's own client should do now: the slot's mode, or nothing while excluded. */\n\tconst vendorMode = (entry: Entry): VendorMode => {\n\t\tconst mode = modeOf(entry)\n\t\tif (excluded || mode === 'off') {\n\t\t\treturn 'off'\n\t\t}\n\t\treturn mode === 'full' ? 'full' : 'cookieless'\n\t}\n\n\t/** Switches the vendor's own client to what the slot and the exclusion say; `refused` by a refusal. */\n\tconst switchVendor = (entry: Entry, refused = false) => {\n\t\tguard(() => entry.sink.mode?.({ mode: vendorMode(entry), refused }))\n\t}\n\n\t/**\n\t * Whether the slot's vendor script may be running: the slot sends or sent on this page, or the\n\t * host loads the vendor scripts itself.\n\t */\n\tconst mayRun = (entry: Entry, sent: boolean): boolean => sent || entry.ran || !snippets\n\n\t/**\n\t * Mirrors the flag into each vendor's own client, which is all a snippet the server rendered obeys.\n\t * Clearing reaches only a slot that sends: putting a browser back must never undo a refusal.\n\t */\n\tconst syncVendors = (next: boolean) => {\n\t\tfor (const entry of entries) {\n\t\t\tif (next || sends(entry)) {\n\t\t\t\tswitchVendor(entry)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Reads the exclusion flag and deletes beta.11's consent key, once a slot runs in full and never\n\t * before, unless the link's `=0` already settled the flag.\n\t */\n\tconst touchStorage = () => {\n\t\tif (!legacyForgotten) {\n\t\t\tlegacyForgotten = true\n\t\t\tforgetLegacyConsent(win)\n\t\t}\n\t\tif (flagKnown) {\n\t\t\treturn\n\t\t}\n\t\tflagKnown = true\n\t\tif (!readExclusion(win)) {\n\t\t\treturn\n\t\t}\n\t\texcluded = true\n\t\tannounceExclusion(true)\n\t\tsilence()\n\t\tfor (const entry of entries) {\n\t\t\tif (mayRun(entry, sends(entry))) {\n\t\t\t\tswitchVendor(entry)\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Injects the slot's snippet and sends what waited, in order. */\n\tconst release = (entry: Entry) => {\n\t\tload(entry)\n\t\tdrain(entry)\n\t}\n\n\t/**\n\t * A refusal: a slot that counts without consent drops to that level, and any other drops what\n\t * waited and puts the refusal to the vendor itself whenever its script may be running. Either\n\t * way the vendor forgets what an earlier page's consent left on the device.\n\t */\n\tconst close = (entry: Entry, was: SlotMode) => {\n\t\tconst sending = sends(entry)\n\t\tif (sending || mayRun(entry, was !== 'off')) {\n\t\t\tswitchVendor(entry, true)\n\t\t}\n\t\tguard(() => entry.sink.forget?.())\n\t\tif (sending) {\n\t\t\trelease(entry)\n\t\t} else {\n\t\t\tentry.queue.clear()\n\t\t}\n\t}\n\n\t/**\n\t * Lifts the vendor's own switch unless the browser is excluded (a refusal on an earlier page may\n\t * have set it), then injects the slot's snippet and sends what waited.\n\t */\n\tconst open = (entry: Entry) => {\n\t\ttouchStorage()\n\t\tif (!excluded) {\n\t\t\tswitchVendor(entry)\n\t\t}\n\t\trelease(entry)\n\t}\n\n\tconst setGate = (state: GateState) => {\n\t\tif (destroyed) {\n\t\t\treturn\n\t\t}\n\t\tconst was = gate.modes\n\t\tconst moved = gate.answer(state)\n\t\tif (skipped) {\n\t\t\treturn\n\t\t}\n\t\tfor (const { slot, to } of moved) {\n\t\t\tconst entry = entries.find((candidate) => candidate.slot === slot)\n\t\t\tif (!entry) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (to === 'open') {\n\t\t\t\topen(entry)\n\t\t\t} else {\n\t\t\t\tclose(entry, was[slot])\n\t\t\t}\n\t\t}\n\t\tsyncItems()\n\t}\n\n\tlet restoredBy: Event | null = null\n\n\t/**\n\t * A page restored from the back/forward cache may have been refused in another tab since: its\n\t * held slots return to their pending mode until the host answers again. A vendor already running\n\t * keeps running until that answer, which switches it as any refusal would.\n\t */\n\tconst restore = (event: Event) => {\n\t\tif (restoredBy === event || !live()) {\n\t\t\treturn\n\t\t}\n\t\trestoredBy = event\n\t\tconst was = gate.modes\n\t\tif (!gate.reset()) {\n\t\t\treturn\n\t\t}\n\t\tfor (const entry of entries) {\n\t\t\tif (MODE_RANK[modeOf(entry)] > MODE_RANK[was[entry.slot]]) {\n\t\t\t\tswitchVendor(entry)\n\t\t\t\trelease(entry)\n\t\t\t}\n\t\t}\n\t\tsyncItems()\n\t}\n\n\tconst onPageShow = (event: PageTransitionEvent) => {\n\t\tif (event.persisted) {\n\t\t\trestore(event)\n\t\t}\n\t}\n\n\tauto = createAutoCapture({\n\t\twin,\n\t\toptions: config.autoCapture,\n\t\thandlers: { track, trackGoal },\n\t})\n\tconst pageTracking = createPageTracking(win, page)\n\twin.addEventListener('pageshow', onPageShow)\n\n\tlet detached = false\n\t/** Every listener and history patch off, and nothing left waiting. */\n\tconst detach = () => {\n\t\tif (detached) {\n\t\t\treturn\n\t\t}\n\t\tdetached = true\n\t\tif (activation !== null) {\n\t\t\twin.document.removeEventListener('prerenderingchange', activation)\n\t\t\tactivation = null\n\t\t}\n\t\twin.removeEventListener('pageshow', onPageShow)\n\t\tengagement.destroy()\n\t\titems?.destroy()\n\t\tpageTracking.destroy()\n\t\tauto?.destroy()\n\t\tauto = null\n\t\tfor (const entry of entries) {\n\t\t\tentry.queue.clear()\n\t\t}\n\t}\n\n\tconst loadSendingSlots = () => {\n\t\tfor (const entry of entries) {\n\t\t\tif (sends(entry)) {\n\t\t\t\tload(entry)\n\t\t\t}\n\t\t}\n\t}\n\n\t/** The boot proper, once whatever the page was opened with is settled. */\n\tconst start = () => {\n\t\tif (entries.some((entry) => modeOf(entry) === 'full')) {\n\t\t\ttouchStorage()\n\t\t}\n\t\tloadSendingSlots()\n\t\tpage()\n\t}\n\n\t// Read only while links are handed out, and stripped from the address at once so no pageview or\n\t// copied link carries it; a server-rendered snippet may have stripped it already.\n\tconst link = config.exclusion\n\tconst stashed = takeStashedExclusion(win)\n\tconst asked = link\n\t\t? exclusionLinkValueOf(stashed ?? new URLSearchParams(win.location.search).get(link.param))\n\t\t: null\n\tif (link) {\n\t\tforgetExclusionParam(win, link.param)\n\t}\n\t// As explicit as a signed link, `=0` needs no open slot; only an excluded browser has switches to lift.\n\tif (asked === 'include') {\n\t\tflagKnown = true\n\t\tif (readExclusion(win)) {\n\t\t\twriteExclusion(win, false)\n\t\t\tsyncVendors(false)\n\t\t}\n\t}\n\tconst token = link && asked !== null && asked !== 'include' ? asked.token : null\n\tconst shown = inLivePreview(config, win)\n\t// Nothing leaves the page until both are settled, so the first page a staff member opens the\n\t// link on is not counted, and neither is a preview while it is being recognised.\n\tif (token !== null || shown !== null) {\n\t\theld = []\n\t}\n\t// Before anything is delivered, so the boot pageview goes where the host's answer sends it.\n\tif (options.gate !== undefined) {\n\t\tsetGate(options.gate)\n\t}\n\tif (held === null) {\n\t\tstart()\n\t} else {\n\t\tvoid Promise.all([\n\t\t\ttoken !== null && link ? matchesDigest(win, token, link.tokenHash) : false,\n\t\t\tshown ?? false,\n\t\t]).then(([valid, preview]) => {\n\t\t\tconst waiting = held ?? []\n\t\t\theld = null\n\t\t\tif (destroyed) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (preview) {\n\t\t\t\tskipped = true\n\t\t\t\tdetach()\n\t\t\t\treturn\n\t\t\t}\n\t\t\t// The staff member's own link: the one write that needs no open slot.\n\t\t\tif (valid) {\n\t\t\t\texcluded = true\n\t\t\t\tflagKnown = true\n\t\t\t\twriteExclusion(win, true)\n\t\t\t\tsyncVendors(true)\n\t\t\t}\n\t\t\tstart()\n\t\t\tfor (const { event, at } of waiting) {\n\t\t\t\tdispatch(event, at)\n\t\t\t}\n\t\t})\n\t}\n\n\tconst tracker: Tracker = {\n\t\tpage,\n\t\ttrack,\n\t\ttrackGoal,\n\t\tpurchase,\n\t\trefund,\n\t\tflush: () => engagement.send(),\n\t\tsetGate,\n\t\tget gate() {\n\t\t\treturn gate.snapshot\n\t\t},\n\t\tget mode() {\n\t\t\treturn gate.modes\n\t\t},\n\t\tget excluded() {\n\t\t\treturn excluded\n\t\t},\n\t\texclude(next) {\n\t\t\texcluded = next\n\t\t\tflagKnown = true\n\t\t\twriteExclusion(win, next)\n\t\t\tif (!live()) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tsyncVendors(next)\n\t\t\tif (next) {\n\t\t\t\tsilence()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tloadSendingSlots()\n\t\t},\n\t\tdestroy() {\n\t\t\tif (destroyed) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tengagement.send()\n\t\t\tdestroyed = true\n\t\t\theld = null\n\t\t\tdetach()\n\t\t},\n\t}\n\trestorers.set(tracker, restore)\n\treturn tracker\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA2CA,MAAM,eAAe,YAA0B;CAE9C,IAAI,OAAO,YAAY,eAAe,QAAQ,KAAK,aAAa,cAC/D;CAED,QAAQ,KAAK,OAAO;AACrB;AAEA,MAAM,mBACL;AAED,MAAM,iBACL;;AAGD,MAAM,aAAa;;AASnB,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AEN,QAAa,IAAI,CAChB,UAAU,QAAQ,OAAO,cAAc,KAAK,OAAO,KAAK,SAAS,IAAI,OACrE,SAAS,KACV,CAAC,EAAE,MAAM,CAAC,OAAO,aAAa;EAC7B,MAAM,UAAU,QAAQ,CAAC;EACzB,OAAO;EACP,IAAI,WACH;EAED,IAAI,SAAS;GACZ,UAAU;GACV,OAAO;GACP;EACD;EAEA,IAAI,OAAO;GACV,WAAW;GACX,YAAY;GACZ,eAAe,KAAK,IAAI;GACxB,YAAY,IAAI;EACjB;EACA,MAAM;EACN,KAAK,MAAM,EAAE,OAAO,QAAQ,SAC3B,SAAS,OAAO,EAAE;CAEpB,CAAC;CAGF,MAAM,UAAmB;EACxB;EACA;EACA;EACA;EACA;EACA,aAAa,WAAW,KAAK;EAC7B;EACA,IAAI,OAAO;GACV,OAAO,KAAK;EACb;EACA,IAAI,OAAO;GACV,OAAO,KAAK;EACb;EACA,IAAI,WAAW;GACd,OAAO;EACR;EACA,QAAQ,MAAM;GACb,WAAW;GACX,YAAY;GACZ,eAAe,KAAK,IAAI;GACxB,IAAI,CAAC,KAAK,GACT;GAED,YAAY,IAAI;GAChB,IAAI,MAAM;IACT,QAAQ;IACR;GACD;GACA,iBAAiB;EAClB;EACA,UAAU;GACT,IAAI,WACH;GAED,WAAW,KAAK;GAChB,YAAY;GACZ,OAAO;GACP,OAAO;EACR;CACD;CACA,UAAU,IAAI,SAAS,OAAO;CAC9B,OAAO;AACR"}
@@ -0,0 +1,8 @@
1
+ import { CaptureSlot, SlotMode } from "./config.js";
2
+
3
+ //#region src/tracker/gate.d.ts
4
+ /** What each capture slot captures with, as `tracker.mode` and `useAnalytics().mode` give it. */
5
+ type ModeSnapshot = Readonly<Record<CaptureSlot, SlotMode>>;
6
+ //#endregion
7
+ export { ModeSnapshot };
8
+ //# sourceMappingURL=gate.d.ts.map
@@ -11,6 +11,29 @@ const ALL_PENDING = Object.freeze({
11
11
  global: "pending",
12
12
  tenant: "pending"
13
13
  });
14
+ /** The modes of a window with no tracker config yet, and of the server render. */
15
+ const ALL_OFF = Object.freeze({
16
+ global: "off",
17
+ tenant: "off"
18
+ });
19
+ const HELD_MODES = [
20
+ "off",
21
+ "cookieless",
22
+ "anonymous"
23
+ ];
24
+ const isHeldMode = (value) => HELD_MODES.some((mode) => mode === value);
25
+ /**
26
+ * What a slot captures with under `status`: in full once open, else its level for the phase without
27
+ * consent. A slot the gate does not hold, and a config naming no level (cached from 1.0.0-beta.14),
28
+ * send nothing until open.
29
+ */
30
+ const slotMode = (config, status) => {
31
+ if (config === void 0) return "off";
32
+ if (status === "open") return "full";
33
+ if (!slotIsGated(config)) return "off";
34
+ const level = config.withoutConsent?.[status === "pending" ? "pending" : "refused"];
35
+ return isHeldMode(level) ? level : "off";
36
+ };
14
37
  /** Deletes beta.11's consent key. Blocked storage holds none to delete. */
15
38
  const forgetLegacyConsent = (win) => {
16
39
  for (const key of LEGACY_CONSENT_KEYS) writeStorage(win, key, null);
@@ -101,8 +124,21 @@ const answeredSnapshot = (answers, slots) => {
101
124
  });
102
125
  };
103
126
  /**
104
- * What may wait in a pending slot's queue: an engagement only for a native slot, since a vendor
105
- * drops one, and never the items seen, which count only while a slot may receive them.
127
+ * Whether a held slot counts without consent in either phase, which its snippet booted for: PostHog
128
+ * then switches between cookieless and full rather than stopping.
129
+ */
130
+ const slotCountsWithoutConsent = (config) => slotMode(config, "pending") !== "off" || slotMode(config, "closed") !== "off";
131
+ /** The modes a window's answers give `slots`, exactly a booting tracker's; all off before a config. */
132
+ const answeredModes = (answers, slots) => {
133
+ if (slots === void 0) return ALL_OFF;
134
+ const gate = createGate(slots);
135
+ for (const answer of answers) gate.answer(answer);
136
+ return gate.modes;
137
+ };
138
+ /**
139
+ * What may wait in the queue of a slot pending and sending nothing: an engagement only for a
140
+ * native slot, since a vendor drops one, and never the items seen, which count only while a slot
141
+ * may receive them.
106
142
  */
107
143
  const queueable = (event, kind) => {
108
144
  if (event.type === "engagement" && kind !== "native") return null;
@@ -110,7 +146,7 @@ const queueable = (event, kind) => {
110
146
  const { impressions: _impressions, ...rest } = event;
111
147
  return rest.type === "engagement" && !rest.durationMs && !rest.scrollDepth ? null : rest;
112
148
  };
113
- /** Bounded FIFO buffer holding one slot's events while its gate is pending. */
149
+ /** Bounded FIFO buffer holding one slot's events while it is pending and sends nothing. */
114
150
  const createGateQueue = (limit = 100) => {
115
151
  let items = [];
116
152
  return {
@@ -146,15 +182,29 @@ const createGate = (slots) => {
146
182
  tenant: initial("tenant")
147
183
  };
148
184
  const answered = /* @__PURE__ */ new Set();
185
+ const modesOf = () => Object.freeze({
186
+ global: slotMode(slotConfigOf(slots, "global"), statuses.global),
187
+ tenant: slotMode(slotConfigOf(slots, "tenant"), statuses.tenant)
188
+ });
149
189
  let snapshot = Object.freeze({ ...statuses });
190
+ let modes = modesOf();
191
+ const changed = () => {
192
+ snapshot = Object.freeze({ ...statuses });
193
+ const next = modesOf();
194
+ if (GATE_SLOTS.some((slot) => next[slot] !== modes[slot])) modes = next;
195
+ };
150
196
  return {
151
197
  status: (slot) => statuses[slot],
198
+ mode: (slot) => modes[slot],
152
199
  get snapshot() {
153
200
  return snapshot;
154
201
  },
202
+ get modes() {
203
+ return modes;
204
+ },
155
205
  answer(state) {
156
206
  const moved = [];
157
- let changed = false;
207
+ let moving = false;
158
208
  for (const slot of GATE_SLOTS) {
159
209
  const config = slotConfigOf(slots, slot);
160
210
  const open = config && gateAnswer(state, slot, slotIsGated(config));
@@ -163,7 +213,7 @@ const createGate = (slots) => {
163
213
  const from = statuses[slot];
164
214
  if (from === to && answered.has(slot)) continue;
165
215
  answered.add(slot);
166
- changed ||= from !== to;
216
+ moving ||= from !== to;
167
217
  statuses[slot] = to;
168
218
  moved.push({
169
219
  slot,
@@ -171,24 +221,24 @@ const createGate = (slots) => {
171
221
  to
172
222
  });
173
223
  }
174
- if (changed) snapshot = Object.freeze({ ...statuses });
224
+ if (moving) changed();
175
225
  return moved;
176
226
  },
177
227
  reset() {
178
- let changed = false;
228
+ let moving = false;
179
229
  for (const slot of GATE_SLOTS) {
180
230
  const config = slotConfigOf(slots, slot);
181
231
  if (config === void 0 || !slotIsGated(config)) continue;
182
232
  answered.delete(slot);
183
- changed ||= statuses[slot] !== "pending";
233
+ moving ||= statuses[slot] !== "pending";
184
234
  statuses[slot] = "pending";
185
235
  }
186
- if (changed) snapshot = Object.freeze({ ...statuses });
187
- return changed;
236
+ if (moving) changed();
237
+ return moving;
188
238
  }
189
239
  };
190
240
  };
191
241
  //#endregion
192
- export { ALL_PENDING, GATE_SLOTS, answeredSnapshot, appendGateAnswer, createGate, createGateQueue, forgetLegacyConsent, openAtBoot, queueable, settleGateAnswers, slotConfigOf, slotIsGated };
242
+ export { ALL_OFF, ALL_PENDING, GATE_SLOTS, answeredModes, answeredSnapshot, appendGateAnswer, createGate, createGateQueue, forgetLegacyConsent, gateAnswer, openAtBoot, queueable, settleGateAnswers, slotConfigOf, slotCountsWithoutConsent, slotIsGated, slotMode };
193
243
 
194
244
  //# sourceMappingURL=gate.js.map