@codefast/tracking 0.5.0-canary.5 → 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 (276) hide show
  1. package/CHANGELOG.md +220 -0
  2. package/README.md +176 -126
  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.ts +9 -0
  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} +8 -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.ts +10 -0
  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.ts +182 -0
  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.ts +15 -0
  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.ts +7 -0
  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.ts +11 -0
  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.ts +27 -0
  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 +203 -108
  172. package/src/adapters/tanstack-start.ts +135 -0
  173. package/src/client/ad-framework-consent.ts +86 -0
  174. package/src/client/browser-cookie.ts +16 -0
  175. package/src/client/consent-runtime.ts +80 -0
  176. package/src/client/consent-storage.ts +31 -1
  177. package/src/client/consent-withdrawal.ts +32 -0
  178. package/src/client/cookie-anonymous-id.ts +112 -0
  179. package/src/client/create-client-tracker.ts +128 -86
  180. package/src/client/gpc.ts +4 -2
  181. package/src/client/initial-consent-store.ts +218 -0
  182. package/src/client/is-analytics-allowed.ts +43 -0
  183. package/src/client/server-persisted-anonymous-id.ts +102 -0
  184. package/src/core/consent-config.ts +27 -0
  185. package/src/core/consent-receipt.ts +115 -0
  186. package/src/core/consent.ts +164 -10
  187. package/src/core/cookie.ts +29 -0
  188. package/src/core/destination.ts +17 -8
  189. package/src/core/event-catalog.ts +29 -15
  190. package/src/core/event-id.ts +1 -2
  191. package/src/core/tracked-event.ts +4 -4
  192. package/src/destinations/ad-consent.ts +29 -0
  193. package/src/destinations/google-analytics.ts +196 -133
  194. package/src/destinations/google-consent.ts +164 -0
  195. package/src/destinations/meta.ts +96 -0
  196. package/src/destinations/microsoft-uet.ts +86 -0
  197. package/src/destinations/shared.ts +45 -0
  198. package/src/destinations/tiktok.ts +85 -0
  199. package/src/destinations/vercel-analytics.ts +23 -24
  200. package/src/index.ts +165 -8
  201. package/src/react/consent-banner.tsx +40 -14
  202. package/src/react/gtag-consent-bootstrap.tsx +48 -0
  203. package/src/react/use-consent.ts +53 -45
  204. package/src/react/use-google-consent-sync.ts +38 -0
  205. package/src/react/use-initial-consent.ts +22 -0
  206. package/src/server/anonymous-id-cookie.ts +103 -0
  207. package/src/server/consent-receipt-store.ts +79 -0
  208. package/src/server/consent-receipt.ts +89 -0
  209. package/src/server/initial-consent.ts +46 -0
  210. package/src/server/measurement-protocol.ts +114 -0
  211. package/src/server/region.ts +22 -15
  212. package/src/server/user-deletion.ts +93 -0
  213. package/src/tooling/import-protection.ts +22 -0
  214. package/dist/client/consent-storage.mjs +0 -63
  215. package/dist/client/create-client-tracker.d.mts +0 -37
  216. package/dist/client/create-client-tracker.mjs +0 -66
  217. package/dist/client/gpc.d.mts +0 -10
  218. package/dist/client/gpc.mjs +0 -12
  219. package/dist/client/index.d.mts +0 -8
  220. package/dist/client/index.mjs +0 -8
  221. package/dist/client/lifecycle.d.mts +0 -23
  222. package/dist/client/lifecycle.mjs +0 -30
  223. package/dist/client/local-storage.d.mts +0 -13
  224. package/dist/client/local-storage.mjs +0 -31
  225. package/dist/client/queue.d.mts +0 -49
  226. package/dist/client/queue.mjs +0 -71
  227. package/dist/client/router.d.mts +0 -30
  228. package/dist/client/router.mjs +0 -17
  229. package/dist/core/consent.d.mts +0 -89
  230. package/dist/core/consent.mjs +0 -57
  231. package/dist/core/destination.d.mts +0 -23
  232. package/dist/core/destination.mjs +0 -1
  233. package/dist/core/event-catalog.d.mts +0 -36
  234. package/dist/core/event-catalog.mjs +0 -12
  235. package/dist/core/event-id.d.mts +0 -10
  236. package/dist/core/event-id.mjs +0 -12
  237. package/dist/core/index.d.mts +0 -6
  238. package/dist/core/index.mjs +0 -4
  239. package/dist/core/tracked-event.d.mts +0 -17
  240. package/dist/core/tracked-event.mjs +0 -1
  241. package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
  242. package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
  243. package/dist/destinations/google-analytics.d.mts +0 -98
  244. package/dist/destinations/google-analytics.mjs +0 -126
  245. package/dist/destinations/http-destination.d.mts +0 -20
  246. package/dist/destinations/http-destination.mjs +0 -25
  247. package/dist/destinations/index.d.mts +0 -5
  248. package/dist/destinations/index.mjs +0 -5
  249. package/dist/destinations/vercel-analytics.d.mts +0 -13
  250. package/dist/destinations/vercel-analytics.mjs +0 -30
  251. package/dist/index.d.mts +0 -6
  252. package/dist/index.mjs +0 -4
  253. package/dist/react/consent-banner.d.mts +0 -163
  254. package/dist/react/consent-banner.mjs +0 -209
  255. package/dist/react/index.d.mts +0 -3
  256. package/dist/react/index.mjs +0 -3
  257. package/dist/react/use-consent.d.mts +0 -49
  258. package/dist/react/use-consent.mjs +0 -54
  259. package/dist/server/create-server-tracker.d.mts +0 -42
  260. package/dist/server/create-server-tracker.mjs +0 -67
  261. package/dist/server/index.d.mts +0 -3
  262. package/dist/server/index.mjs +0 -3
  263. package/dist/server/region.d.mts +0 -20
  264. package/dist/server/region.mjs +0 -56
  265. package/src/client/index.ts +0 -17
  266. package/src/client/lifecycle.ts +0 -50
  267. package/src/client/local-storage.ts +0 -44
  268. package/src/client/queue.ts +0 -117
  269. package/src/client/router.ts +0 -37
  270. package/src/core/index.ts +0 -24
  271. package/src/destinations/ga4-measurement-protocol.ts +0 -166
  272. package/src/destinations/http-destination.ts +0 -33
  273. package/src/destinations/index.ts +0 -20
  274. package/src/react/index.ts +0 -29
  275. package/src/server/create-server-tracker.ts +0 -128
  276. package/src/server/index.ts +0 -4
@@ -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,uBAAuB,CAAC,OAAiC;IACvE,MAAM,EAAE,UAAU,EAAE,aAAa,GAAG,mBAAmB,EAAE,GAAG,OAAO,CAAC;IACpE,oFAAoF;IACpF,sFAAsF;IACtF,IAAI,QAA4B,CAAC;IAEjC,OAAO;QACL,KAAK;YACH,QAAQ,GAAG,SAAS,CAAC;Y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AAkB,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;YAC3D,6DAA6D;QAC/D,CAAC,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,uDAAuD;IACzD,CAAC;AACH,CAAC;AAgDD;;GAEG;AACH,MAAM,UAAU,mBAAmB,CACjC,OAAsC;IAEtC,wFAAwF;IACxF,MAAM,kBAAkB,GAAG,OAA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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"}
@@ -0,0 +1,10 @@
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 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"}
@@ -0,0 +1,138 @@
1
+ import { isInitialConsent, STRICTEST_INITIAL_CONSENT } from "#/core/consent";
2
+ const SERVER_SNAPSHOT = {
3
+ initialConsent: STRICTEST_INITIAL_CONSENT,
4
+ isResolved: false,
5
+ };
6
+ /**
7
+ * The client half of the server-resolved consent lane: single-flight resolution over the
8
+ * given server function, an optional per-session cache, fail-closed publishing on error
9
+ * (consent UI still renders, under the strictest default), and a retry the next time the
10
+ * tab becomes visible — so one failed request never locks the whole SPA session.
11
+ *
12
+ * @since 1.0.0-canary.6
13
+ */
14
+ export function createInitialConsentStore(options) {
15
+ const listeners = new Set();
16
+ let snapshot = SERVER_SNAPSHOT;
17
+ let isFetchInFlight = false;
18
+ /** Sticky only after a successful resolve (or session-cache hit) — failures stay retryable. */
19
+ let hasResolvedSuccessfully = false;
20
+ function notify() {
21
+ for (const listener of listeners) {
22
+ listener();
23
+ }
24
+ }
25
+ // add/removeEventListener dedupe an identical listener, so no armed-flag is needed.
26
+ function stopResolveRetry() {
27
+ if (typeof window === "undefined") {
28
+ return;
29
+ }
30
+ document.removeEventListener("visibilitychange", onResolveRetryResume);
31
+ window.removeEventListener("pageshow", onResolveRetryResume);
32
+ window.removeEventListener("online", onResolveRetryResume);
33
+ }
34
+ function onResolveRetryResume() {
35
+ if (hasResolvedSuccessfully) {
36
+ stopResolveRetry();
37
+ return;
38
+ }
39
+ if (document.visibilityState === "visible") {
40
+ ensureResolved();
41
+ }
42
+ }
43
+ /**
44
+ * After a failed fetch, retry when the tab becomes visible again or connectivity
45
+ * returns (coalesced by in-flight) — a visitor who never leaves the tab must not stay
46
+ * fail-closed for the whole session over one network blip.
47
+ */
48
+ function scheduleResolveRetry() {
49
+ if (typeof window === "undefined") {
50
+ return;
51
+ }
52
+ document.addEventListener("visibilitychange", onResolveRetryResume);
53
+ window.addEventListener("pageshow", onResolveRetryResume);
54
+ window.addEventListener("online", onResolveRetryResume);
55
+ }
56
+ function publishResolved(initialConsent) {
57
+ hasResolvedSuccessfully = true;
58
+ snapshot = { initialConsent, isResolved: true };
59
+ stopResolveRetry();
60
+ notify();
61
+ }
62
+ /** Fail-closed UI without permanently locking the session against a later retry. */
63
+ function publishFailClosed() {
64
+ snapshot = { initialConsent: STRICTEST_INITIAL_CONSENT, isResolved: true };
65
+ notify();
66
+ }
67
+ function readSessionCache() {
68
+ if (options.sessionStorageKey === undefined) {
69
+ return undefined;
70
+ }
71
+ try {
72
+ const raw = window.sessionStorage.getItem(options.sessionStorageKey);
73
+ const parsed = raw === null ? undefined : JSON.parse(raw);
74
+ return isInitialConsent(parsed) ? parsed : undefined;
75
+ }
76
+ catch {
77
+ return undefined;
78
+ }
79
+ }
80
+ function writeSessionCache(resolved) {
81
+ if (options.sessionStorageKey === undefined) {
82
+ return;
83
+ }
84
+ try {
85
+ window.sessionStorage.setItem(options.sessionStorageKey, JSON.stringify(resolved));
86
+ }
87
+ catch {
88
+ /* private mode / quota — resolve again next page load */
89
+ }
90
+ }
91
+ function ensureResolved() {
92
+ if (hasResolvedSuccessfully || isFetchInFlight || typeof window === "undefined") {
93
+ return;
94
+ }
95
+ const cached = readSessionCache();
96
+ if (cached) {
97
+ publishResolved(cached);
98
+ return;
99
+ }
100
+ isFetchInFlight = true;
101
+ // Promise.resolve().then(...) folds a synchronously throwing resolver into the same
102
+ // fail-closed path — tracking must never break the caller (a React mount effect).
103
+ Promise.resolve()
104
+ .then(() => options.resolve())
105
+ .then((resolved) => {
106
+ writeSessionCache(resolved);
107
+ publishResolved(resolved);
108
+ })
109
+ .catch(() => {
110
+ publishFailClosed();
111
+ scheduleResolveRetry();
112
+ })
113
+ .finally(() => {
114
+ isFetchInFlight = false;
115
+ });
116
+ }
117
+ return {
118
+ ensureResolved,
119
+ getServerSnapshot: () => SERVER_SNAPSHOT,
120
+ getSnapshot: () => snapshot,
121
+ reset() {
122
+ stopResolveRetry();
123
+ snapshot = SERVER_SNAPSHOT;
124
+ isFetchInFlight = false;
125
+ hasResolvedSuccessfully = false;
126
+ if (typeof window !== "undefined" && options.sessionStorageKey !== undefined) {
127
+ window.sessionStorage.removeItem(options.sessionStorageKey);
128
+ }
129
+ },
130
+ subscribe(listener) {
131
+ listeners.add(listener);
132
+ return () => {
133
+ listeners.delete(listener);
134
+ };
135
+ },
136
+ };
137
+ }
138
+ //# sourceMappingURL=initial-consent-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initial-consent-store.js","sourceRoot":"","sources":["../../src/client/initial-consent-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAyD7E,MAAM,eAAe,GAA2B;IAC9C,cAAc,EAAE,yBAAyB;IACzC,UAAU,EAAE,KAAK;CAClB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAmC;IAC3E,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAC;IAExC,IAAI,QAAQ,GAA2B,eAAe,CAAC;IACvD,IAAI,eAAe,GAAG,KAAK,CAAC;IAC5B,+FAA+F;IAC/F,IAAI,uBAAuB,GAAG,KAAK,CAAC;IAEpC,SAAS,MAAM;QACb,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;IAED,oFAAoF;IACpF,SAAS,gBAAgB;QACvB,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,OAAO;QACT,CAAC;QAED,QAAQ,CAAC,mBAAmB,CAAC,kBAAkB,EAAE,oBAAoB,CAAC,CAAC;QACvE,MAAM,CAAC,mBAAmB,CAAC,UAAU,EAAE,oBAAoB,CAAC,CAAC;QAC7D,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,oBAAoB,CAAC,CAAC;IAC7D,CAAC;IAED,SAAS,oBAAoB;QAC3B,IAAI,uBAAuB,EAAE,CAAC;YAC5B,gBAAgB,EAAE,CAAC;YAEnB,OAAO;QACT,CAAC;QAED,IAAI,QAAQ,CAAC,eAAe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AAG,eAAe,CAAC;YAC3B,eAAe,GAAG,KAAK,CAAC;YACxB,uBAAuB,GAAG,KAAK,CAAC;YAEhC,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,CAAC,iBAAiB,KAAK,SAAS,EAAE,CAAC;gBAC7E,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;YAC9D,CAAC;QACH,CAAC;QACD,SAAS,CAAC,QAAoB;YAC5B,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAExB,OAAO,GAAG,EAAE;gBACV,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC7B,CAAC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { ConsentMode, ConsentStorage } from "#/core/consent";
2
+ import type { ConsentConfig } from "#/core/consent-config";
3
+ /**
4
+ * @since 1.0.0-canary.6
5
+ */
6
+ export interface IsAnalyticsAllowedOptions {
7
+ /** Must be the same object `useConsent` receives — one config, every surface. */
8
+ config: ConsentConfig;
9
+ /**
10
+ * Re-read each call. Pass `hasGlobalPrivacyControlSignal` from `@codefast/tracking/client`
11
+ * (or a wrapper) so GPC changes apply without recreating the gate.
12
+ */
13
+ hasGlobalPrivacyControlSignal?: (() => boolean) | undefined;
14
+ /** Re-read each call so a post-hydration region resolve can win. */
15
+ getMode: () => ConsentMode;
16
+ /** Must be the same storage instance `useConsent` subscribes to. */
17
+ storage: ConsentStorage;
18
+ }
19
+ /**
20
+ * Non-React mirror of `useConsent`'s `isAnalyticsAllowed` — pass the returned function as
21
+ * `createClientTracker({ isAnalyticsAllowed })` so the pipeline and the banner share one rule.
22
+ *
23
+ * @since 1.0.0-canary.6
24
+ */
25
+ export declare function createIsAnalyticsAllowed(options: IsAnalyticsAllowedOptions): () => boolean;
26
+ //# sourceMappingURL=is-analytics-allowed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-analytics-allowed.d.ts","sourceRoot":"","sources":["../../src/client/is-analytics-allowed.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAElE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,iFAAiF;IACjF,MAAM,EAAE,aAAa,CAAC;IACtB;;;OAGG;IACH,6BAA6B,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,GAAG,SAAS,CAAC;IAC5D,oEAAoE;IACpE,OAAO,EAAE,MAAM,WAAW,CAAC;IAC3B,oEAAoE;IACpE,OAAO,EAAE,cAAc,CAAC;CACzB;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,MAAM,OAAO,CAe1F"}
@@ -0,0 +1,22 @@
1
+ import { resolveEffectiveConsent } from "#/core/consent";
2
+ /**
3
+ * Non-React mirror of `useConsent`'s `isAnalyticsAllowed` — pass the returned function as
4
+ * `createClientTracker({ isAnalyticsAllowed })` so the pipeline and the banner share one rule.
5
+ *
6
+ * @since 1.0.0-canary.6
7
+ */
8
+ export function createIsAnalyticsAllowed(options) {
9
+ const { config, getMode, storage } = options;
10
+ const readGpc = options.hasGlobalPrivacyControlSignal ?? (() => false);
11
+ return () => {
12
+ const effectiveConsent = resolveEffectiveConsent({
13
+ hasGlobalPrivacyControlSignal: readGpc(),
14
+ mode: getMode(),
15
+ policyVersion: config.policyVersion,
16
+ requestedCategories: config.requestedCategories,
17
+ storage,
18
+ });
19
+ return effectiveConsent.analytics;
20
+ };
21
+ }
22
+ //# sourceMappingURL=is-analytics-allowed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-analytics-allowed.js","sourceRoot":"","sources":["../../src/client/is-analytics-allowed.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAoBzD;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,OAAkC;IACzE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAC7C,MAAM,OAAO,GAAG,OAAO,CAAC,6BAA6B,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;IAEvE,OAAO,GAAY,EAAE;QACnB,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;YAC/C,6BAA6B,EAAE,OAAO,EAAE;YACxC,IAAI,EAAE,OAAO,EAAE;YACf,aAAa,EAAE,MAAM,CAAC,aAAa;YACnC,mBAAmB,EAAE,MAAM,CAAC,mBAAmB;YAC/C,OAAO;SACR,CAAC,CAAC;QAEH,OAAO,gBAAgB,CAAC,SAAS,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { CookieAnonymousId } from "#/client/cookie-anonymous-id";
2
+ /**
3
+ * @since 1.0.0-canary.6
4
+ */
5
+ export interface ServerPersistedAnonymousIdOptions {
6
+ /** Server round-trip that expires the cookie via `Set-Cookie` — the server half of `clear()`. */
7
+ clearOnServer?: (() => Promise<void>) | undefined;
8
+ /** Cookie name — must not collide with another cookie on the domain. */
9
+ cookieName: string;
10
+ /** How long the id survives a return visit. Defaults to one year. */
11
+ maxAgeSeconds?: number | undefined;
12
+ /**
13
+ * Server round-trip that re-sets the cookie via `Set-Cookie` (e.g. a server function
14
+ * calling `setAnonymousIdResponseCookie`). Fired at most once per page load, only after an
15
+ * id exists client-side — the server persists and prolongs, it never mints.
16
+ */
17
+ persist: (id: string) => Promise<void>;
18
+ }
19
+ /**
20
+ * `createCookieAnonymousId` plus server persistence: the client still mints the id
21
+ * (lazily, only once an event is allowed to send — consent stays client-owned), then
22
+ * delegates the durable write to the server. A script-written cookie is capped at 7 days
23
+ * by Safari ITP; the server's `Set-Cookie` re-issue is what makes the id actually live
24
+ * its full max-age. Same optimistic `document.cookie` write first, so the current event
25
+ * never waits on the round-trip and a failed `persist` degrades to today's behavior.
26
+ *
27
+ * @since 1.0.0-canary.6
28
+ */
29
+ export declare function createServerPersistedAnonymousId(options: ServerPersistedAnonymousIdOptions): CookieAnonymousId;
30
+ //# sourceMappingURL=server-persisted-anonymous-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-persisted-anonymous-id.d.ts","sourceRoot":"","sources":["../../src/client/server-persisted-anonymous-id.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAItE;;GAEG;AACH,MAAM,WAAW,iCAAiC;IAChD,iGAAiG;IACjG,aAAa,CAAC,EAAE,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC;IAClD,wEAAwE;IACxE,UAAU,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC;;;;OAIG;IACH,OAAO,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC;AAED;;;;;;;;;GASG;AACH,wBAAgB,gCAAgC,CAAC,OAAO,EAAE,iCAAiC,GAAG,iBAAiB,CAqE9G"}
@@ -0,0 +1,72 @@
1
+ import { createCookieAnonymousId } from "#/client/cookie-anonymous-id";
2
+ import { readCookieValue } from "#/core/cookie";
3
+ /**
4
+ * `createCookieAnonymousId` plus server persistence: the client still mints the id
5
+ * (lazily, only once an event is allowed to send — consent stays client-owned), then
6
+ * delegates the durable write to the server. A script-written cookie is capped at 7 days
7
+ * by Safari ITP; the server's `Set-Cookie` re-issue is what makes the id actually live
8
+ * its full max-age. Same optimistic `document.cookie` write first, so the current event
9
+ * never waits on the round-trip and a failed `persist` degrades to today's behavior.
10
+ *
11
+ * @since 1.0.0-canary.6
12
+ */
13
+ export function createServerPersistedAnonymousId(options) {
14
+ const { clearOnServer, cookieName, maxAgeSeconds, persist } = options;
15
+ const local = createCookieAnonymousId({ cookieName, maxAgeSeconds });
16
+ // One attempt per page load — a failure is not retried until the next load, so a down
17
+ // endpoint costs one request, not one per event. Tracking must never break the app.
18
+ let hasRequestedPersist = false;
19
+ return {
20
+ clear() {
21
+ // Skip the server round-trip when the cookie is already gone (e.g. a second
22
+ // withdrawal clear from another mounted consent surface in the same tick).
23
+ const shouldClearServer = typeof document !== "undefined" && Boolean(readCookieValue(document.cookie, cookieName));
24
+ local.clear();
25
+ // A fresh id after a re-grant must be persisted again.
26
+ hasRequestedPersist = false;
27
+ if (shouldClearServer) {
28
+ void clearOnServer?.().catch(() => {
29
+ /* the client-side expiry above already took effect */
30
+ });
31
+ }
32
+ },
33
+ current() {
34
+ return local.current();
35
+ },
36
+ getOrCreate() {
37
+ // Cross-tab withdrawal expires the cookie without this instance's `clear()` — reset
38
+ // so the next minted id is server-persisted (ITP re-issue) again.
39
+ if (typeof document !== "undefined" && !readCookieValue(document.cookie, cookieName)) {
40
+ hasRequestedPersist = false;
41
+ }
42
+ const id = local.getOrCreate();
43
+ // Also fires when the cookie already existed: the re-issue upgrades a script-written
44
+ // cookie to a server-set one and rolls its expiry forward on every visit.
45
+ if (!hasRequestedPersist && typeof document !== "undefined") {
46
+ hasRequestedPersist = true;
47
+ void persist(id).catch(() => {
48
+ /* optimistic client-side cookie still covers this session */
49
+ });
50
+ }
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
+ return;
60
+ }
61
+ local.refresh();
62
+ // Same once-per-load budget as getOrCreate — refresh must not add a second request.
63
+ if (!hasRequestedPersist) {
64
+ hasRequestedPersist = true;
65
+ void persist(existing).catch(() => {
66
+ /* optimistic client-side prolong above still covers this session */
67
+ });
68
+ }
69
+ },
70
+ };
71
+ }
72
+ //# sourceMappingURL=server-persisted-anonymous-id.js.map