@codefast/tracking 1.0.0-canary.6 → 1.0.0-canary.7

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 (267) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +72 -17
  3. package/dist/adapters/tanstack-start.d.ts +80 -0
  4. package/dist/adapters/tanstack-start.d.ts.map +1 -0
  5. package/dist/adapters/tanstack-start.js +74 -0
  6. package/dist/adapters/tanstack-start.js.map +1 -0
  7. package/dist/client/ad-framework-consent.d.ts +62 -0
  8. package/dist/client/ad-framework-consent.d.ts.map +1 -0
  9. package/dist/client/ad-framework-consent.js +40 -0
  10. package/dist/client/ad-framework-consent.js.map +1 -0
  11. package/dist/client/{browser-cookie.d.mts → browser-cookie.d.ts} +2 -4
  12. package/dist/client/browser-cookie.d.ts.map +1 -0
  13. package/dist/client/browser-cookie.js +15 -0
  14. package/dist/client/browser-cookie.js.map +1 -0
  15. package/dist/client/consent-runtime.d.ts +53 -0
  16. package/dist/client/consent-runtime.d.ts.map +1 -0
  17. package/dist/client/consent-runtime.js +33 -0
  18. package/dist/client/consent-runtime.js.map +1 -0
  19. package/dist/client/{consent-storage.d.mts → consent-storage.d.ts} +3 -6
  20. package/dist/client/consent-storage.d.ts.map +1 -0
  21. package/dist/client/consent-storage.js +109 -0
  22. package/dist/client/consent-storage.js.map +1 -0
  23. package/dist/client/consent-withdrawal.d.ts +18 -0
  24. package/dist/client/consent-withdrawal.d.ts.map +1 -0
  25. package/dist/client/consent-withdrawal.js +17 -0
  26. package/dist/client/consent-withdrawal.js.map +1 -0
  27. package/dist/client/cookie-anonymous-id.d.ts +41 -0
  28. package/dist/client/cookie-anonymous-id.d.ts.map +1 -0
  29. package/dist/client/cookie-anonymous-id.js +65 -0
  30. package/dist/client/cookie-anonymous-id.js.map +1 -0
  31. package/dist/client/create-client-tracker.d.ts +60 -0
  32. package/dist/client/create-client-tracker.d.ts.map +1 -0
  33. package/dist/client/create-client-tracker.js +91 -0
  34. package/dist/client/create-client-tracker.js.map +1 -0
  35. package/dist/client/{gpc.d.mts → gpc.d.ts} +2 -4
  36. package/dist/client/gpc.d.ts.map +1 -0
  37. package/dist/client/gpc.js +12 -0
  38. package/dist/client/gpc.js.map +1 -0
  39. package/dist/client/initial-consent-store.d.ts +63 -0
  40. package/dist/client/initial-consent-store.d.ts.map +1 -0
  41. package/dist/client/initial-consent-store.js +138 -0
  42. package/dist/client/initial-consent-store.js.map +1 -0
  43. package/dist/client/is-analytics-allowed.d.ts +26 -0
  44. package/dist/client/is-analytics-allowed.d.ts.map +1 -0
  45. package/dist/client/is-analytics-allowed.js +22 -0
  46. package/dist/client/is-analytics-allowed.js.map +1 -0
  47. package/dist/client/server-persisted-anonymous-id.d.ts +30 -0
  48. package/dist/client/server-persisted-anonymous-id.d.ts.map +1 -0
  49. package/dist/client/server-persisted-anonymous-id.js +72 -0
  50. package/dist/client/server-persisted-anonymous-id.js.map +1 -0
  51. package/dist/core/consent-config.d.ts +24 -0
  52. package/dist/core/consent-config.d.ts.map +1 -0
  53. package/dist/core/consent-config.js +9 -0
  54. package/dist/core/consent-config.js.map +1 -0
  55. package/dist/core/consent-receipt.d.ts +71 -0
  56. package/dist/core/consent-receipt.d.ts.map +1 -0
  57. package/dist/core/consent-receipt.js +39 -0
  58. package/dist/core/consent-receipt.js.map +1 -0
  59. package/dist/core/{consent.d.mts → consent.d.ts} +49 -51
  60. package/dist/core/consent.d.ts.map +1 -0
  61. package/dist/core/consent.js +159 -0
  62. package/dist/core/consent.js.map +1 -0
  63. package/dist/core/{cookie.d.mts → cookie.d.ts} +3 -5
  64. package/dist/core/cookie.d.ts.map +1 -0
  65. package/dist/core/cookie.js +26 -0
  66. package/dist/core/cookie.js.map +1 -0
  67. package/dist/core/destination.d.ts +29 -0
  68. package/dist/core/destination.d.ts.map +1 -0
  69. package/dist/core/destination.js +2 -0
  70. package/dist/core/destination.js.map +1 -0
  71. package/dist/core/event-catalog.d.ts +35 -0
  72. package/dist/core/event-catalog.d.ts.map +1 -0
  73. package/dist/core/event-catalog.js +30 -0
  74. package/dist/core/event-catalog.js.map +1 -0
  75. package/dist/core/{event-id.d.mts → event-id.d.ts} +2 -4
  76. package/dist/core/event-id.d.ts.map +1 -0
  77. package/dist/core/event-id.js +9 -0
  78. package/dist/core/event-id.js.map +1 -0
  79. package/dist/core/tracked-event.d.ts +15 -0
  80. package/dist/core/tracked-event.d.ts.map +1 -0
  81. package/dist/core/tracked-event.js +2 -0
  82. package/dist/core/tracked-event.js.map +1 -0
  83. package/dist/destinations/ad-consent.d.ts +23 -0
  84. package/dist/destinations/ad-consent.d.ts.map +1 -0
  85. package/dist/destinations/ad-consent.js +12 -0
  86. package/dist/destinations/ad-consent.js.map +1 -0
  87. package/dist/destinations/google-analytics.d.ts +148 -0
  88. package/dist/destinations/google-analytics.d.ts.map +1 -0
  89. package/dist/destinations/google-analytics.js +156 -0
  90. package/dist/destinations/google-analytics.js.map +1 -0
  91. package/dist/destinations/google-consent.d.ts +62 -0
  92. package/dist/destinations/google-consent.d.ts.map +1 -0
  93. package/dist/destinations/google-consent.js +109 -0
  94. package/dist/destinations/google-consent.js.map +1 -0
  95. package/dist/destinations/meta.d.ts +63 -0
  96. package/dist/destinations/meta.d.ts.map +1 -0
  97. package/dist/destinations/meta.js +44 -0
  98. package/dist/destinations/meta.js.map +1 -0
  99. package/dist/destinations/microsoft-uet.d.ts +56 -0
  100. package/dist/destinations/microsoft-uet.d.ts.map +1 -0
  101. package/dist/destinations/microsoft-uet.js +41 -0
  102. package/dist/destinations/microsoft-uet.js.map +1 -0
  103. package/dist/destinations/shared.d.ts +18 -0
  104. package/dist/destinations/shared.d.ts.map +1 -0
  105. package/dist/destinations/shared.js +19 -0
  106. package/dist/destinations/shared.js.map +1 -0
  107. package/dist/destinations/tiktok.d.ts +55 -0
  108. package/dist/destinations/tiktok.d.ts.map +1 -0
  109. package/dist/destinations/tiktok.js +40 -0
  110. package/dist/destinations/tiktok.js.map +1 -0
  111. package/dist/destinations/vercel-analytics.d.ts +25 -0
  112. package/dist/destinations/vercel-analytics.d.ts.map +1 -0
  113. package/dist/destinations/vercel-analytics.js +23 -0
  114. package/dist/destinations/vercel-analytics.js.map +1 -0
  115. package/dist/index.d.ts +64 -0
  116. package/dist/index.d.ts.map +1 -0
  117. package/dist/index.js +41 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/react/consent-banner.d.ts +155 -0
  120. package/dist/react/consent-banner.d.ts.map +1 -0
  121. package/dist/react/consent-banner.js +171 -0
  122. package/dist/react/consent-banner.js.map +1 -0
  123. package/dist/react/gtag-consent-bootstrap.d.ts +17 -0
  124. package/dist/react/gtag-consent-bootstrap.d.ts.map +1 -0
  125. package/dist/react/gtag-consent-bootstrap.js +22 -0
  126. package/dist/react/gtag-consent-bootstrap.js.map +1 -0
  127. package/dist/react/use-consent.d.ts +42 -0
  128. package/dist/react/use-consent.d.ts.map +1 -0
  129. package/dist/react/use-consent.js +60 -0
  130. package/dist/react/use-consent.js.map +1 -0
  131. package/dist/react/use-google-consent-sync.d.ts +20 -0
  132. package/dist/react/use-google-consent-sync.d.ts.map +1 -0
  133. package/dist/react/use-google-consent-sync.js +22 -0
  134. package/dist/react/use-google-consent-sync.js.map +1 -0
  135. package/dist/react/{use-initial-consent.d.mts → use-initial-consent.d.ts} +3 -6
  136. package/dist/react/use-initial-consent.d.ts.map +1 -0
  137. package/dist/react/use-initial-consent.js +18 -0
  138. package/dist/react/use-initial-consent.js.map +1 -0
  139. package/dist/server/anonymous-id-cookie.d.ts +67 -0
  140. package/dist/server/anonymous-id-cookie.d.ts.map +1 -0
  141. package/dist/server/anonymous-id-cookie.js +71 -0
  142. package/dist/server/anonymous-id-cookie.js.map +1 -0
  143. package/dist/server/consent-receipt-store.d.ts +56 -0
  144. package/dist/server/consent-receipt-store.d.ts.map +1 -0
  145. package/dist/server/consent-receipt-store.js +44 -0
  146. package/dist/server/consent-receipt-store.js.map +1 -0
  147. package/dist/server/consent-receipt.d.ts +41 -0
  148. package/dist/server/consent-receipt.d.ts.map +1 -0
  149. package/dist/server/consent-receipt.js +60 -0
  150. package/dist/server/consent-receipt.js.map +1 -0
  151. package/dist/server/initial-consent.d.ts +26 -0
  152. package/dist/server/initial-consent.d.ts.map +1 -0
  153. package/dist/server/initial-consent.js +31 -0
  154. package/dist/server/initial-consent.js.map +1 -0
  155. package/dist/server/measurement-protocol.d.ts +69 -0
  156. package/dist/server/measurement-protocol.d.ts.map +1 -0
  157. package/dist/server/measurement-protocol.js +61 -0
  158. package/dist/server/measurement-protocol.js.map +1 -0
  159. package/dist/server/{region.d.mts → region.d.ts} +5 -15
  160. package/dist/server/region.d.ts.map +1 -0
  161. package/dist/server/region.js +69 -0
  162. package/dist/server/region.js.map +1 -0
  163. package/dist/server/user-deletion.d.ts +64 -0
  164. package/dist/server/user-deletion.d.ts.map +1 -0
  165. package/dist/server/user-deletion.js +54 -0
  166. package/dist/server/user-deletion.js.map +1 -0
  167. package/dist/tooling/import-protection.d.ts +19 -0
  168. package/dist/tooling/import-protection.d.ts.map +1 -0
  169. package/dist/tooling/import-protection.js +23 -0
  170. package/dist/tooling/import-protection.js.map +1 -0
  171. package/package.json +238 -59
  172. package/src/adapters/tanstack-start.ts +135 -0
  173. package/src/client/ad-framework-consent.ts +86 -0
  174. package/src/client/cookie-anonymous-id.ts +19 -0
  175. package/src/client/create-client-tracker.ts +81 -10
  176. package/src/client/server-persisted-anonymous-id.ts +4 -1
  177. package/src/core/consent-receipt.ts +115 -0
  178. package/src/core/consent.ts +2 -2
  179. package/src/core/destination.ts +7 -0
  180. package/src/core/event-catalog.ts +5 -4
  181. package/src/core/tracked-event.ts +5 -20
  182. package/src/destinations/ad-consent.ts +29 -0
  183. package/src/destinations/meta.ts +96 -0
  184. package/src/destinations/microsoft-uet.ts +86 -0
  185. package/src/destinations/tiktok.ts +85 -0
  186. package/src/index.ts +138 -19
  187. package/src/server/anonymous-id-cookie.ts +49 -22
  188. package/src/server/consent-receipt-store.ts +79 -0
  189. package/src/server/consent-receipt.ts +89 -0
  190. package/src/server/initial-consent.ts +0 -2
  191. package/src/server/measurement-protocol.ts +114 -0
  192. package/src/server/region.ts +0 -10
  193. package/src/server/user-deletion.ts +93 -0
  194. package/src/tooling/import-protection.ts +22 -0
  195. package/dist/client/browser-cookie.mjs +0 -15
  196. package/dist/client/consent-runtime.d.mts +0 -56
  197. package/dist/client/consent-runtime.mjs +0 -35
  198. package/dist/client/consent-storage.mjs +0 -82
  199. package/dist/client/consent-withdrawal.d.mts +0 -21
  200. package/dist/client/consent-withdrawal.mjs +0 -17
  201. package/dist/client/cookie-anonymous-id.d.mts +0 -37
  202. package/dist/client/cookie-anonymous-id.mjs +0 -46
  203. package/dist/client/create-client-tracker.d.mts +0 -32
  204. package/dist/client/create-client-tracker.mjs +0 -40
  205. package/dist/client/gpc.mjs +0 -14
  206. package/dist/client/index.d.mts +0 -10
  207. package/dist/client/index.mjs +0 -10
  208. package/dist/client/initial-consent-store.d.mts +0 -66
  209. package/dist/client/initial-consent-store.mjs +0 -119
  210. package/dist/client/is-analytics-allowed.d.mts +0 -29
  211. package/dist/client/is-analytics-allowed.mjs +0 -23
  212. package/dist/client/server-persisted-anonymous-id.d.mts +0 -33
  213. package/dist/client/server-persisted-anonymous-id.mjs +0 -50
  214. package/dist/core/consent-config.d.mts +0 -27
  215. package/dist/core/consent-config.mjs +0 -11
  216. package/dist/core/consent.mjs +0 -152
  217. package/dist/core/cookie.mjs +0 -23
  218. package/dist/core/destination.d.mts +0 -25
  219. package/dist/core/destination.mjs +0 -1
  220. package/dist/core/event-catalog.d.mts +0 -39
  221. package/dist/core/event-catalog.mjs +0 -30
  222. package/dist/core/event-id.mjs +0 -11
  223. package/dist/core/index.d.mts +0 -8
  224. package/dist/core/index.mjs +0 -6
  225. package/dist/core/tracked-event.d.mts +0 -30
  226. package/dist/core/tracked-event.mjs +0 -1
  227. package/dist/destinations/google-analytics.d.mts +0 -150
  228. package/dist/destinations/google-analytics.mjs +0 -130
  229. package/dist/destinations/google-consent.d.mts +0 -64
  230. package/dist/destinations/google-consent.mjs +0 -102
  231. package/dist/destinations/index.d.mts +0 -2
  232. package/dist/destinations/index.mjs +0 -2
  233. package/dist/destinations/shared.d.mts +0 -20
  234. package/dist/destinations/shared.mjs +0 -13
  235. package/dist/destinations/vercel-analytics.d.mts +0 -28
  236. package/dist/destinations/vercel-analytics.mjs +0 -23
  237. package/dist/import-protection.d.mts +0 -19
  238. package/dist/import-protection.mjs +0 -23
  239. package/dist/index.d.mts +0 -8
  240. package/dist/index.mjs +0 -6
  241. package/dist/react/consent-banner.d.mts +0 -180
  242. package/dist/react/consent-banner.mjs +0 -214
  243. package/dist/react/gtag-consent-bootstrap.d.mts +0 -27
  244. package/dist/react/gtag-consent-bootstrap.mjs +0 -29
  245. package/dist/react/index.d.mts +0 -6
  246. package/dist/react/index.mjs +0 -6
  247. package/dist/react/use-consent.d.mts +0 -45
  248. package/dist/react/use-consent.mjs +0 -71
  249. package/dist/react/use-google-consent-sync.d.mts +0 -23
  250. package/dist/react/use-google-consent-sync.mjs +0 -24
  251. package/dist/react/use-initial-consent.mjs +0 -19
  252. package/dist/server/anonymous-id-cookie.d.mts +0 -46
  253. package/dist/server/anonymous-id-cookie.mjs +0 -51
  254. package/dist/server/index.d.mts +0 -5
  255. package/dist/server/index.mjs +0 -4
  256. package/dist/server/initial-consent.d.mts +0 -29
  257. package/dist/server/initial-consent.mjs +0 -31
  258. package/dist/server/region.mjs +0 -77
  259. package/dist/tanstack-start/index.d.mts +0 -58
  260. package/dist/tanstack-start/index.mjs +0 -45
  261. package/src/client/index.ts +0 -28
  262. package/src/core/index.ts +0 -40
  263. package/src/destinations/index.ts +0 -19
  264. package/src/import-protection.ts +0 -20
  265. package/src/react/index.ts +0 -37
  266. package/src/server/index.ts +0 -16
  267. package/src/tanstack-start/index.ts +0 -83
@@ -0,0 +1,33 @@
1
+ import { createLocalStorageConsentStorage } from "#/client/consent-storage";
2
+ import { hasGlobalPrivacyControlSignal } from "#/client/gpc";
3
+ import { createInitialConsentStore } from "#/client/initial-consent-store";
4
+ import { createIsAnalyticsAllowed } from "#/client/is-analytics-allowed";
5
+ /**
6
+ * Composes the client half of the consent lane from a single `ConsentConfig`:
7
+ * `localStorage`-backed decision storage, the initial-consent store over the app's
8
+ * server lane, and the non-React analytics gate wired to that store's resolved mode.
9
+ *
10
+ * @since 1.0.0-canary.6
11
+ */
12
+ export function createConsentRuntime(options) {
13
+ const { config } = options;
14
+ const storage = createLocalStorageConsentStorage(config.storageKey);
15
+ const initialConsentStore = createInitialConsentStore({
16
+ resolve: options.resolveInitialConsent,
17
+ sessionStorageKey: options.initialConsentSessionStorageKey,
18
+ });
19
+ const isAnalyticsAllowed = createIsAnalyticsAllowed({
20
+ config,
21
+ getMode: () => initialConsentStore.getSnapshot().initialConsent.mode,
22
+ hasGlobalPrivacyControlSignal: options.hasGlobalPrivacyControlSignal ?? hasGlobalPrivacyControlSignal,
23
+ storage,
24
+ });
25
+ return {
26
+ config,
27
+ ensureInitialConsentResolved: initialConsentStore.ensureResolved,
28
+ initialConsentStore,
29
+ isAnalyticsAllowed,
30
+ storage,
31
+ };
32
+ }
33
+ //# sourceMappingURL=consent-runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-runtime.js","sourceRoot":"","sources":["../../src/client/consent-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AA+CzE;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAA8B;IACjE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC3B,MAAM,OAAO,GAAG,gCAAgC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACpE,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;QACpD,OAAO,EAAE,OAAO,CAAC,qBAAqB;QACtC,iBAAiB,EAAE,OAAO,CAAC,+BAA+B;KAC3D,CAAC,CAAC;IACH,MAAM,kBAAkB,GAAG,wBAAwB,CAAC;QAClD,MAAM;QACN,OAAO,EAAE,GAAG,EAAE,CAAC,mBAAmB,CAAC,WAAW,EAAE,CAAC,cAAc,CAAC,IAAI;QACpE,6BAA6B,EAAE,OAAO,CAAC,6BAA6B,IAAI,6BAA6B;QACrG,OAAO;KACR,CAAC,CAAC;IAEH,OAAO;QACL,MAAM;QACN,4BAA4B,EAAE,mBAAmB,CAAC,cAAc;QAChE,mBAAmB;QACnB,kBAAkB;QAClB,OAAO;KACR,CAAC;AACJ,CAAC"}
@@ -1,6 +1,4 @@
1
- import { ConsentStorage } from "../core/consent.mjs";
2
-
3
- //#region src/client/consent-storage.d.ts
1
+ import type { ConsentStorage } from "#/core/consent";
4
2
  /**
5
3
  * `localStorage`-backed consent storage. Corrupt state, a private-mode/quota error, or no
6
4
  * `window` (SSR) degrades to an in-memory record — the visitor's decision still applies
@@ -19,6 +17,5 @@ import { ConsentStorage } from "../core/consent.mjs";
19
17
  *
20
18
  * @since 0.5.0-canary.4
21
19
  */
22
- declare function createLocalStorageConsentStorage(storageKey: string): ConsentStorage;
23
- //#endregion
24
- export { createLocalStorageConsentStorage };
20
+ export declare function createLocalStorageConsentStorage(storageKey: string): ConsentStorage;
21
+ //# sourceMappingURL=consent-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-storage.d.ts","sourceRoot":"","sources":["../../src/client/consent-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGpE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,cAAc,CAuGnF"}
@@ -0,0 +1,109 @@
1
+ import { isConsentRecord } from "#/core/consent";
2
+ /**
3
+ * `localStorage`-backed consent storage. Corrupt state, a private-mode/quota error, or no
4
+ * `window` (SSR) degrades to an in-memory record — the visitor's decision still applies
5
+ * for the session instead of re-prompting in a loop. `subscribe` fires for same-tab
6
+ * writes and, via the `storage` event, for decisions made in other tabs.
7
+ *
8
+ * `load()` parses and validates at most once per change: the result is cached and
9
+ * invalidated by `save`/`clear` and by cross-tab `storage` events, so per-event consent
10
+ * gates and per-render `useSyncExternalStore` snapshots cost a memory read, not a
11
+ * `JSON.parse`.
12
+ *
13
+ * @remarks
14
+ * The record persists as plain `JSON.stringify(ConsentRecord)` — a stable contract, so
15
+ * pre-hydration inline scripts (e.g. a Consent Mode default bootstrap) can read the
16
+ * decision synchronously before any tag fires.
17
+ *
18
+ * @since 0.5.0-canary.4
19
+ */
20
+ export function createLocalStorageConsentStorage(storageKey) {
21
+ const listeners = new Set();
22
+ let memoryRecord;
23
+ // Keyed by the raw string so ANY write path (save(), another tab, dev tools) is caught
24
+ // by a cheap string compare — parse + validate re-run only when the value changed.
25
+ let cache;
26
+ function notify() {
27
+ for (const listener of listeners) {
28
+ listener();
29
+ }
30
+ }
31
+ return {
32
+ clear() {
33
+ if (typeof globalThis.window === "undefined") {
34
+ return;
35
+ }
36
+ memoryRecord = undefined;
37
+ cache = undefined;
38
+ try {
39
+ globalThis.window.localStorage.removeItem(storageKey);
40
+ }
41
+ catch {
42
+ /* storage blocked (private mode / quota) */
43
+ }
44
+ notify();
45
+ },
46
+ load() {
47
+ if (typeof globalThis.window === "undefined") {
48
+ return undefined;
49
+ }
50
+ try {
51
+ const raw = globalThis.window.localStorage.getItem(storageKey);
52
+ if (cache && raw === cache.raw) {
53
+ return cache.record;
54
+ }
55
+ let record;
56
+ if (raw) {
57
+ const parsed = JSON.parse(raw);
58
+ // Drop malformed records at the source — mirrors the offline-queue storage guard.
59
+ record = isConsentRecord(parsed) ? parsed : memoryRecord;
60
+ }
61
+ else {
62
+ record = memoryRecord;
63
+ }
64
+ cache = { raw, record };
65
+ return record;
66
+ }
67
+ catch {
68
+ // Storage blocked — never seed the cache off an error path.
69
+ return memoryRecord;
70
+ }
71
+ },
72
+ save(record) {
73
+ if (typeof globalThis.window === "undefined") {
74
+ return;
75
+ }
76
+ cache = undefined;
77
+ try {
78
+ globalThis.window.localStorage.setItem(storageKey, JSON.stringify(record));
79
+ // localStorage is authoritative now — a lingering memory copy would shadow an
80
+ // external clear (e.g. the visitor wiping site data).
81
+ memoryRecord = undefined;
82
+ }
83
+ catch {
84
+ // Storage blocked (private mode / quota) — keep the decision for this session.
85
+ memoryRecord = record;
86
+ }
87
+ notify();
88
+ },
89
+ subscribe(listener) {
90
+ listeners.add(listener);
91
+ // The `storage` event only fires in *other* tabs; same-tab writes go through notify().
92
+ function handleStorageEvent(event) {
93
+ if (event.key === null || event.key === storageKey) {
94
+ listener();
95
+ }
96
+ }
97
+ if (typeof globalThis.window !== "undefined") {
98
+ globalThis.window.addEventListener("storage", handleStorageEvent);
99
+ }
100
+ return () => {
101
+ listeners.delete(listener);
102
+ if (typeof globalThis.window !== "undefined") {
103
+ globalThis.window.removeEventListener("storage", handleStorageEvent);
104
+ }
105
+ };
106
+ },
107
+ };
108
+ }
109
+ //# sourceMappingURL=consent-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-storage.js","sourceRoot":"","sources":["../../src/client/consent-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gCAAgC,CAAC,UAAkB;IACjE,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAC;IACxC,IAAI,YAAuC,CAAC;IAC5C,uFAAuF;IACvF,mFAAmF;IACnF,IAAI,KAA4E,CAAC;IAEjF,SAAS,MAAM;QACb,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,QAAQ,E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erOptions {
6
+ /** Forget the cookie-backed visitor id when analytics is denied. */
7
+ clearAnonymousId?: (() => void) | undefined;
8
+ /** Expire Google's `_ga` / `_ga_*` cookies when analytics is denied. */
9
+ clearGoogleAnalyticsCookies?: (() => void) | undefined;
10
+ }
11
+ /**
12
+ * `useConsent({ onDecision })` handler that clears first-party tracking state when the
13
+ * visitor denies (or withdraws) analytics. Grant paths are a no-op.
14
+ *
15
+ * @since 1.0.0-canary.6
16
+ */
17
+ export declare function createConsentWithdrawalHandler(options: ConsentWithdrawalHandlerOptions): (decision: ConsentDecision) => void;
18
+ //# sourceMappingURL=consent-withdrawal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-withdrawal.d.ts","sourceRoot":"","sources":["../../src/client/consent-withdrawal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,+BAA+B;IAC9C,oEAAoE;IACpE,gBAAgB,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;IAC5C,wEAAwE;IACxE,2BAA2B,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;CACxD;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,+BAA+B,GACvC,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,CAWrC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * `useConsent({ onDecision })` handler that clears first-party tracking state when the
3
+ * visitor denies (or withdraws) analytics. Grant paths are a no-op.
4
+ *
5
+ * @since 1.0.0-canary.6
6
+ */
7
+ export function createConsentWithdrawalHandler(options) {
8
+ const { clearAnonymousId, clearGoogleAnalyticsCookies } = options;
9
+ return (decision) => {
10
+ if (decision.analytics) {
11
+ return;
12
+ }
13
+ clearAnonymousId?.();
14
+ clearGoogleAnalyticsCookies?.();
15
+ };
16
+ }
17
+ //# sourceMappingURL=consent-withdrawal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-withdrawal.js","sourceRoot":"","sources":["../../src/client/consent-withdrawal.ts"],"names":[],"mappings":"AAYA;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B,CAC5C,OAAwC;IAExC,MAAM,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,GAAG,OAAO,CAAC;IAElE,OAAO,CAAC,QAAyB,EAAQ,EAAE;QACzC,IAAI,QAAQ,CAAC,SAAS,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QAED,gBAAgB,EAAE,EAAE,CAAC;QACrB,2BAA2B,EAAE,EAAE,CAAC;IAClC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @since 1.0.0-canary.6
3
+ */
4
+ export interface CookieAnonymousIdOptions {
5
+ /** Cookie name — must not collide with another cookie on the domain. */
6
+ cookieName: string;
7
+ /** How long the id survives a return visit. Defaults to one year. */
8
+ maxAgeSeconds?: number | undefined;
9
+ }
10
+ /**
11
+ * @since 1.0.0-canary.6
12
+ */
13
+ export interface CookieAnonymousId {
14
+ /** Expires the cookie — call when the visitor withdraws tracking consent. */
15
+ clear: () => void;
16
+ /**
17
+ * The existing id **without minting one** — `undefined` when the visitor has none yet.
18
+ * Use this to stamp a receipt/decision with the id that already identifies the visitor,
19
+ * rather than re-reading the cookie or minting a throwaway that never correlates.
20
+ */
21
+ current: () => string | undefined;
22
+ /** The existing id, or a freshly minted and persisted one. */
23
+ getOrCreate: () => string;
24
+ /**
25
+ * Rolls an *existing* id's expiry forward without ever minting one — safe to call on
26
+ * page load for returning consented visitors (the ITP-refresh path), a no-op for
27
+ * visitors who never got an id.
28
+ */
29
+ refresh: () => void;
30
+ }
31
+ /**
32
+ * A `document.cookie`-backed anonymous id — not `localStorage`-only, so a server-owned
33
+ * event can read the same id from the request and correlate to this visitor. Pass
34
+ * `getOrCreate` (not the result of calling it) as `ClientTrackerOptions.anonymousId`:
35
+ * it's invoked lazily, so the cookie is minted only once an event is actually allowed to
36
+ * send, never as an import-time side effect.
37
+ *
38
+ * @since 1.0.0-canary.6
39
+ */
40
+ export declare function createCookieAnonymousId(options: CookieAnonymousIdOptions): CookieAnonymousId;
41
+ //# sourceMappingURL=cookie-anonymous-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cookie-anonymous-id.d.ts","sourceRoot":"","sources":["../../src/client/cookie-anonymous-id.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,6EAA6E;IAC7E,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB;;;;OAIG;IACH,OAAO,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;IAClC,8DAA8D;IAC9D,WAAW,EAAE,MAAM,MAAM,CAAC;IAC1B;;;;OAIG;IACH,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,wBAAwB,GAAG,iBAAiB,CAmE5F"}
@@ -0,0 +1,65 @@
1
+ import { writeBrowserCookie } from "#/client/browser-cookie";
2
+ import { ONE_YEAR_IN_SECONDS, readCookieValue } from "#/core/cookie";
3
+ /**
4
+ * A `document.cookie`-backed anonymous id — not `localStorage`-only, so a server-owned
5
+ * event can read the same id from the request and correlate to this visitor. Pass
6
+ * `getOrCreate` (not the result of calling it) as `ClientTrackerOptions.anonymousId`:
7
+ * it's invoked lazily, so the cookie is minted only once an event is actually allowed to
8
+ * send, never as an import-time side effect.
9
+ *
10
+ * @since 1.0.0-canary.6
11
+ */
12
+ export function createCookieAnonymousId(options) {
13
+ const { cookieName, maxAgeSeconds = ONE_YEAR_IN_SECONDS } = options;
14
+ // Cached after the first getOrCreate() so a callback invoked once per tracked event
15
+ // doesn't re-parse the whole cookie header every time — cleared alongside the cookie.
16
+ let cachedId;
17
+ return {
18
+ clear() {
19
+ cachedId = undefined;
20
+ if (typeof document === "undefined") {
21
+ return;
22
+ }
23
+ writeBrowserCookie(cookieName, "", 0);
24
+ },
25
+ current() {
26
+ // Prefer the live cookie — another tab may have minted or cleared it since this
27
+ // instance last cached — falling back to the cache only when off the DOM (SSR).
28
+ if (typeof document === "undefined") {
29
+ return cachedId;
30
+ }
31
+ const existing = readCookieValue(document.cookie, cookieName);
32
+ cachedId = existing;
33
+ return existing;
34
+ },
35
+ getOrCreate() {
36
+ if (typeof document === "undefined") {
37
+ return cachedId ?? crypto.randomUUID();
38
+ }
39
+ const existing = readCookieValue(document.cookie, cookieName);
40
+ // Prefer the live cookie over `cachedId` — another tab's withdrawal expires the
41
+ // cookie without calling this instance's `clear()`, and a re-grant must not revive
42
+ // the pre-withdrawal identity from memory.
43
+ if (existing) {
44
+ cachedId = existing;
45
+ return existing;
46
+ }
47
+ cachedId = undefined;
48
+ const id = crypto.randomUUID();
49
+ writeBrowserCookie(cookieName, id, maxAgeSeconds);
50
+ cachedId = id;
51
+ return id;
52
+ },
53
+ refresh() {
54
+ if (typeof document === "undefined") {
55
+ return;
56
+ }
57
+ const existing = readCookieValue(document.cookie, cookieName);
58
+ if (existing) {
59
+ cachedId = existing;
60
+ writeBrowserCookie(cookieName, existing, maxAgeSeconds);
61
+ }
62
+ },
63
+ };
64
+ }
65
+ //# sourceMappingURL=cookie-anonymous-id.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cookie-anonymous-id.js","sourceRoot":"","sources":["../../src/client/cookie-anonymous-id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAkCrE;;;;;;;;GAQG;AACH,MAAM,UAAU,uBAA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AAK,WAAW,EAAE,CAAC;gBACpC,OAAO;YACT,CAAC;YAED,MAAM,QAAQ,GAAG,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;YAE9D,IAAI,QAAQ,EAAE,CAAC;gBACb,QAAQ,GAAG,QAAQ,CAAC;gBACpB,kBAAkB,CAAC,UAAU,EAAE,QAAQ,EAAE,aAAa,CAAC,CAAC;YAC1D,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import type { Destination } from "#/core/destination";
3
+ import type { EventCatalog } from "#/core/event-catalog";
4
+ import type { TrackedEvent } from "#/core/tracked-event";
5
+ /**
6
+ * Context handed to {@link ClientTrackerOptions.onDeliveryError} for one failed delivery.
7
+ *
8
+ * @since 1.0.0-canary.7
9
+ */
10
+ export interface DeliveryErrorContext {
11
+ destination: Destination;
12
+ error: unknown;
13
+ event: TrackedEvent;
14
+ }
15
+ /**
16
+ * @since 0.5.0-canary.4
17
+ */
18
+ export interface ClientTrackerOptions<Catalog extends EventCatalog> {
19
+ /** Invoked per allowed event — defer creating the id (e.g. minting a cookie) until an event is actually allowed to send, never as an import-time side effect. */
20
+ anonymousId: () => string;
21
+ catalog: Catalog;
22
+ destinations: Array<Destination>;
23
+ /**
24
+ * Consulted before every event — while it returns `false`, only
25
+ * `consentRequirement: "exempt"` destinations still receive events, stripped of
26
+ * identifiers. Omit to always track.
27
+ */
28
+ isAnalyticsAllowed?: (() => boolean) | undefined;
29
+ /**
30
+ * Consulted before an `exempt` destination receives an event while the gate is closed.
31
+ * ePrivacy audience-measurement exemption is jurisdiction-dependent (spec-destinations
32
+ * §2), so it MUST be gateable per region rather than assumed global — return `false`
33
+ * where exemption is not defensible and even exempt sinks are withheld. Omit to treat
34
+ * exempt destinations as exempt everywhere.
35
+ */
36
+ isExemptionAllowed?: (() => boolean) | undefined;
37
+ /**
38
+ * Notified once per failed delivery (a destination throwing or rejecting) so a consumer
39
+ * can meter the failure — the tracker still swallows it so tracking never breaks the
40
+ * interaction. Wire it to a monitor in production.
41
+ */
42
+ onDeliveryError?: ((context: DeliveryErrorContext) => void) | undefined;
43
+ }
44
+ /**
45
+ * @since 0.5.0-canary.4
46
+ */
47
+ export interface ClientTracker<Catalog extends EventCatalog> {
48
+ /**
49
+ * Runs each destination's {@link Destination.onErasure} once on withdrawal (spec-data-subject-rights
50
+ * DSR-V2), passing the subject id to erase. Failures are swallowed so a destination that
51
+ * throws never breaks the withdrawal flow.
52
+ */
53
+ erase: (id: string) => void;
54
+ track: <Name extends keyof Catalog & string>(name: Name, properties: StandardSchemaV1.InferOutput<Catalog[Name]["schema"]>) => void;
55
+ }
56
+ /**
57
+ * @since 0.5.0-canary.4
58
+ */
59
+ export declare function createClientTracker<Catalog extends EventCatalog>(options: ClientTrackerOptions<Catalog>): ClientTracker<Catalog>;
60
+ //# sourceMappingURL=create-client-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-client-tracker.d.ts","sourceRoot":"","sources":["../../src/client/create-client-tracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC,WAAW,EAAE,WAAW,CAAC;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,YAAY,CAAC;CACrB;AA8CD;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,YAAY;IAChE,iKAAiK;IACjK,WAAW,EAAE,MAAM,MAAM,CAAC;IAC1B,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IACjC;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS,CAAC;IACjD;;;;;;OAMG;IACH,kBAAkB,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS,CAAC;IACjD;;;;OAIG;IACH,eAAe,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;CACzE;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,OAAO,SAAS,YAAY;IACzD;;;;OAIG;IACH,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5B,KAAK,EAAE,CAAC,IAAI,SAAS,MAAM,OAAO,GAAG,MAAM,EACzC,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,gBAAgB,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,KAC9D,IAAI,CAAC;CACX;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,SAAS,YAAY,EAC9D,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,GACrC,aAAa,CAAC,OAAO,CAAC,CAqDxB"}
@@ -0,0 +1,91 @@
1
+ import { assertValidEventProperties } from "#/core/event-catalog";
2
+ import { generateEventId } from "#/core/event-id";
3
+ /**
4
+ * `Destination.send` is typed to always return a `Promise`, but that's unenforceable at
5
+ * runtime for a third-party/plain-JS destination — one that ignores the contract and
6
+ * throws synchronously (or returns a non-`Promise`) must still never break the tracked
7
+ * interaction. `Promise.resolve(...)` normalizes a non-`Promise` return; the outer
8
+ * try/catch is the only thing that can catch a throw that happens before `send` ever
9
+ * gets to return anything. Both failure shapes surface through `onDeliveryError` (itself
10
+ * guarded — an observer that throws must not break the interaction either).
11
+ */
12
+ function sendToDestination(destination, event, onDeliveryError) {
13
+ const report = (error) => {
14
+ if (!onDeliveryError) {
15
+ return;
16
+ }
17
+ try {
18
+ onDeliveryError({ destination, error, event });
19
+ }
20
+ catch {
21
+ /* an observer must never break the tracked interaction */
22
+ }
23
+ };
24
+ try {
25
+ void Promise.resolve(destination.send(event)).catch(report);
26
+ }
27
+ catch (error) {
28
+ report(error);
29
+ }
30
+ }
31
+ /** Invokes a destination's erasure once, swallowing both failure shapes — DSR must never break the flow. */
32
+ function eraseInDestination(destination, id) {
33
+ try {
34
+ void Promise.resolve(destination.onErasure?.(id)).catch(() => {
35
+ /* an erasure failure must never break the withdrawal flow */
36
+ });
37
+ }
38
+ catch {
39
+ /* a sync throw must never break the withdrawal flow */
40
+ }
41
+ }
42
+ /**
43
+ * @since 0.5.0-canary.4
44
+ */
45
+ export function createClientTracker(options) {
46
+ // The exempt lane only carries what a cookieless sink may see — identifier-free counts.
47
+ const exemptDestinations = options.destinations.filter((destination) => destination.consentRequirement === "exempt");
48
+ return {
49
+ erase(id) {
50
+ for (const destination of options.destinations) {
51
+ eraseInDestination(destination, id);
52
+ }
53
+ },
54
+ track(name, properties) {
55
+ // noUncheckedIndexedAccess types this as possibly undefined; the check also guards
56
+ // callers who bypass the catalog key type with an `as` cast.
57
+ const definition = options.catalog[name];
58
+ if (!definition) {
59
+ throw new Error(`Unknown event: ${String(name)}`);
60
+ }
61
+ // Forward the parsed output (not the raw input) so unknown keys cannot ride along.
62
+ const validatedProperties = assertValidEventProperties(definition.schema, name, properties);
63
+ // The gate runs per event (not at creation) so a consent change mid-session applies immediately.
64
+ const isAllowed = options.isAnalyticsAllowed?.() !== false;
65
+ // While gated, exempt sinks still receive identifier-free events — but only where
66
+ // exemption is defensible (spec-destinations §2); a `false` here withholds them too.
67
+ const activeDestinations = isAllowed
68
+ ? options.destinations
69
+ : options.isExemptionAllowed?.() === false
70
+ ? []
71
+ : exemptDestinations;
72
+ if (activeDestinations.length === 0) {
73
+ return;
74
+ }
75
+ const event = {
76
+ // Gated events ship identifier-free: no anonymousId is resolved, so none is ever
77
+ // minted as a side effect.
78
+ anonymousId: isAllowed ? options.anonymousId() : "",
79
+ eventId: generateEventId(),
80
+ name,
81
+ properties: validatedProperties,
82
+ timestamp: Date.now(),
83
+ type: "track",
84
+ };
85
+ for (const destination of activeDestinations) {
86
+ sendToDestination(destination, event, options.onDeliveryError);
87
+ }
88
+ },
89
+ };
90
+ }
91
+ //# sourceMappingURL=create-client-tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-client-tracker.js","sourceRoot":"","sources":["../../src/client/create-client-tracker.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAclD;;;;;;;;GAQG;AACH,SAAS,iBAAiB,CACxB,WAAwB,EACxB,KAAmB,EACnB,eAAyD;IAEzD,MAAM,MAAM,GAAG,CAAC,KAAc,EAAQ,EAAE;QACtC,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,eAAe,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QACjD,CAAC;QAAC,MAAM,CAAC;YACP,0DAA0D;QAC5D,CAAC;IACH,CAAC,CAAC;IAEF,IAAI,CAAC;QACH,KAAK,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,CAAC,CAAC;IAChB,CAAC;AACH,CAAC;AAED,4GAA4G;AAC5G,SAAS,kBAAkB,CAAC,WAAwB,EAAE,EAAU;IAC9D,IAAI,CAAC;QACH,KAAK,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;YAC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;YACT,CAAC;YAED,MAAM,KAAK,GAAiB;gBAC1B,iFAAiF;gBACjF,2BAA2B;gBAC3B,WAAW,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE;gBACnD,OAAO,EAAE,eAAe,EAAE;gBAC1B,IAAI;gBACJ,UAAU,EAAE,mBAA8C;gBAC1D,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;gBACrB,IAAI,EAAE,OAAO;aACd,CAAC;YAEF,KAAK,MAAM,WAAW,IAAI,kBAAkB,EAAE,CAAC;gBAC7C,iBAAiB,CAAC,WAAW,EAAE,KAAK,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;YACjE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -1,4 +1,3 @@
1
- //#region src/client/gpc.d.ts
2
1
  /**
3
2
  * Global Privacy Control isn't in lib.dom yet, hence the local interface extension.
4
3
  * CCPA/CPRA requires honoring this as an opt-out signal even under the opt-out default.
@@ -7,6 +6,5 @@
7
6
  *
8
7
  * @since 0.5.0-canary.4
9
8
  */
10
- declare function hasGlobalPrivacyControlSignal(): boolean;
11
- //#endregion
12
- export { hasGlobalPrivacyControlSignal };
9
+ export declare function hasGlobalPrivacyControlSignal(): boolean;
10
+ //# sourceMappingURL=gpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gpc.d.ts","sourceRoot":"","sources":["../../src/client/gpc.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,wBAAgB,6BAA6B,IAAI,OAAO,CAEvD"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Global Privacy Control isn't in lib.dom yet, hence the local interface extension.
3
+ * CCPA/CPRA requires honoring this as an opt-out signal even under the opt-out default.
4
+ * Guarded so SSR renders (where the module is reachable) read `false`, not a crash on
5
+ * runtimes without a global `navigator`.
6
+ *
7
+ * @since 0.5.0-canary.4
8
+ */
9
+ export function hasGlobalPrivacyControlSignal() {
10
+ return typeof navigator !== "undefined" && navigator.globalPrivacyControl === true;
11
+ }
12
+ //# sourceMappingURL=gpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gpc.js","sourceRoot":"","sources":["../../src/client/gpc.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,MAAM,UAAU,6BAA6B;IAC3C,OAAO,OAAO,SAAS,KAAK,WAAW,IAAK,SAA8B,CAAC,oBAAoB,KAAK,IAAI,CAAC;AAC3G,CAAC"}
@@ -0,0 +1,63 @@
1
+ import type { InitialConsent } from "#/core/consent";
2
+ /**
3
+ * External-store view of the visitor's region default — the strictest value until the
4
+ * server lane answers, then the resolved one.
5
+ *
6
+ * @since 1.0.0-canary.6
7
+ */
8
+ export interface InitialConsentSnapshot {
9
+ /** The region-correct default once resolved; {@link STRICTEST_INITIAL_CONSENT} until then. */
10
+ initialConsent: InitialConsent;
11
+ /**
12
+ * True once a usable default is published — either a successful server answer or a
13
+ * fail-closed fallback. A failed fetch still sets this so consent UI can render, but
14
+ * does not lock the session: resolution stays retryable.
15
+ */
16
+ isResolved: boolean;
17
+ }
18
+ /**
19
+ * @since 1.0.0-canary.6
20
+ */
21
+ export interface InitialConsentStoreOptions {
22
+ /**
23
+ * The per-visitor server lane — typically a server function wrapping
24
+ * `resolveInitialConsentFromRequest`. Prerendered/CDN-cached HTML can carry nothing
25
+ * region-specific, so this is the only place the region-correct default may come from.
26
+ */
27
+ resolve: () => Promise<InitialConsent>;
28
+ /**
29
+ * `sessionStorage` key caching the resolved value, so only the first page load of a
30
+ * session pays the round trip — omit to resolve once per page load instead. Cached
31
+ * values are re-validated with `isInitialConsent` before use.
32
+ */
33
+ sessionStorageKey?: string | undefined;
34
+ }
35
+ /**
36
+ * `useSyncExternalStore`-shaped — pass `subscribe`/`getSnapshot`/`getServerSnapshot`
37
+ * straight through, or use the `useInitialConsent` hook from `@codefast/tracking/react`.
38
+ *
39
+ * @since 1.0.0-canary.6
40
+ */
41
+ export interface InitialConsentStore {
42
+ /**
43
+ * Kicks off region resolution (at most one in-flight request; a success is sticky for
44
+ * the store's lifetime). Call it early — e.g. at router creation, window-guarded — so
45
+ * the round trip overlaps hydration instead of waiting for the first effect.
46
+ */
47
+ ensureResolved: () => void;
48
+ getServerSnapshot: () => InitialConsentSnapshot;
49
+ getSnapshot: () => InitialConsentSnapshot;
50
+ /** Test seam — clears resolved state, retry listeners, and the session cache. */
51
+ reset: () => void;
52
+ subscribe: (listener: () => void) => () => void;
53
+ }
54
+ /**
55
+ * The client half of the server-resolved consent lane: single-flight resolution over the
56
+ * given server function, an optional per-session cache, fail-closed publishing on error
57
+ * (consent UI still renders, under the strictest default), and a retry the next time the
58
+ * tab becomes visible — so one failed request never locks the whole SPA session.
59
+ *
60
+ * @since 1.0.0-canary.6
61
+ */
62
+ export declare function createInitialConsentStore(options: InitialConsentStoreOptions): InitialConsentStore;
63
+ //# sourceMappingURL=initial-consent-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initial-consent-store.d.ts","sourceRoot":"","sources":["../../src/client/initial-consent-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGrD;;;;;GAKG;AACH,MAAM,WAAW,sBAAsB;IACrC,8FAA8F;IAC9F,cAAc,EAAE,cAAc,CAAC;IAC/B;;;;OAIG;IACH,UAAU,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;;;OAIG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,CAAC;IACvC;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACxC;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,cAAc,EAAE,MAAM,IAAI,CAAC;IAC3B,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;IAChD,WAAW,EAAE,MAAM,sBAAsB,CAAC;IAC1C,iFAAiF;IACjF,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;CACjD;AAOD;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,0BAA0B,GAAG,mBAAmB,CAkJlG"}