@codefast/tracking 0.7.0 → 0.7.2

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 (214) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +235 -105
  3. package/dist/adapters/tanstack-start.d.ts +12 -9
  4. package/dist/adapters/tanstack-start.js +5 -6
  5. package/dist/client/ad-framework-consent.d.ts +9 -8
  6. package/dist/client/ad-framework-consent.js +4 -5
  7. package/dist/client/browser-cookie.d.ts +2 -3
  8. package/dist/client/browser-cookie.js +2 -3
  9. package/dist/client/consent-runtime.d.ts +6 -5
  10. package/dist/client/consent-runtime.js +2 -3
  11. package/dist/client/consent-storage.d.ts +1 -2
  12. package/dist/client/consent-storage.js +1 -2
  13. package/dist/client/consent-withdrawal.d.ts +5 -4
  14. package/dist/client/consent-withdrawal.js +2 -3
  15. package/dist/client/cookie-anonymous-id.d.ts +8 -5
  16. package/dist/client/cookie-anonymous-id.js +2 -3
  17. package/dist/client/create-client-tracker.d.ts +8 -3
  18. package/dist/client/create-client-tracker.js +3 -2
  19. package/dist/client/gpc.d.ts +1 -2
  20. package/dist/client/gpc.js +1 -2
  21. package/dist/client/initial-consent-store.d.ts +7 -6
  22. package/dist/client/initial-consent-store.js +2 -3
  23. package/dist/client/is-analytics-allowed.d.ts +5 -4
  24. package/dist/client/is-analytics-allowed.js +2 -3
  25. package/dist/client/server-persisted-anonymous-id.d.ts +5 -4
  26. package/dist/client/server-persisted-anonymous-id.js +2 -3
  27. package/dist/core/consent-config.d.ts +3 -4
  28. package/dist/core/consent-config.js +2 -3
  29. package/dist/core/consent-receipt.d.ts +7 -8
  30. package/dist/core/consent-receipt.js +2 -3
  31. package/dist/core/consent.d.ts +24 -13
  32. package/dist/core/consent.js +13 -10
  33. package/dist/core/cookie.d.ts +3 -4
  34. package/dist/core/cookie.js +3 -4
  35. package/dist/core/destination.d.ts +1 -2
  36. package/dist/core/destination.js +1 -2
  37. package/dist/core/event-catalog.d.ts +2 -3
  38. package/dist/core/event-catalog.js +2 -3
  39. package/dist/core/event-id.d.ts +1 -2
  40. package/dist/core/event-id.js +1 -2
  41. package/dist/core/tracked-event.d.ts +1 -2
  42. package/dist/core/tracked-event.js +1 -2
  43. package/dist/destinations/ad-consent.d.ts +3 -4
  44. package/dist/destinations/ad-consent.js +2 -3
  45. package/dist/destinations/google-analytics.d.ts +17 -10
  46. package/dist/destinations/google-analytics.js +5 -6
  47. package/dist/destinations/google-consent.d.ts +10 -9
  48. package/dist/destinations/google-consent.js +6 -7
  49. package/dist/destinations/meta.d.ts +8 -7
  50. package/dist/destinations/meta.js +3 -4
  51. package/dist/destinations/microsoft-uet.d.ts +8 -7
  52. package/dist/destinations/microsoft-uet.js +3 -4
  53. package/dist/destinations/shared.d.ts +8 -5
  54. package/dist/destinations/shared.js +4 -3
  55. package/dist/destinations/tiktok.d.ts +8 -7
  56. package/dist/destinations/tiktok.js +3 -4
  57. package/dist/destinations/vercel-analytics.d.ts +4 -3
  58. package/dist/destinations/vercel-analytics.js +1 -2
  59. package/dist/index.d.ts +1 -2
  60. package/dist/index.js +1 -2
  61. package/dist/react/consent-banner.d.ts +29 -2
  62. package/dist/react/consent-banner.js +12 -9
  63. package/dist/react/gtag-consent-bootstrap.d.ts +5 -4
  64. package/dist/react/gtag-consent-bootstrap.js +2 -3
  65. package/dist/react/use-consent.d.ts +5 -2
  66. package/dist/react/use-consent.js +1 -2
  67. package/dist/react/use-google-consent-sync.d.ts +5 -4
  68. package/dist/react/use-google-consent-sync.js +2 -3
  69. package/dist/react/use-initial-consent.d.ts +2 -3
  70. package/dist/react/use-initial-consent.js +2 -3
  71. package/dist/server/anonymous-id-cookie.d.ts +8 -7
  72. package/dist/server/anonymous-id-cookie.js +5 -6
  73. package/dist/server/consent-receipt-store.d.ts +5 -6
  74. package/dist/server/consent-receipt-store.js +3 -4
  75. package/dist/server/consent-receipt.d.ts +5 -6
  76. package/dist/server/consent-receipt.js +4 -5
  77. package/dist/server/initial-consent.d.ts +5 -4
  78. package/dist/server/initial-consent.js +2 -3
  79. package/dist/server/measurement-protocol.d.ts +10 -9
  80. package/dist/server/measurement-protocol.js +4 -5
  81. package/dist/server/region.d.ts +3 -4
  82. package/dist/server/region.js +3 -4
  83. package/dist/server/user-deletion.d.ts +10 -7
  84. package/dist/server/user-deletion.js +3 -4
  85. package/dist/tooling/import-protection.d.ts +2 -3
  86. package/dist/tooling/import-protection.js +2 -3
  87. package/package.json +11 -62
  88. package/dist/adapters/tanstack-start.d.ts.map +0 -1
  89. package/dist/adapters/tanstack-start.js.map +0 -1
  90. package/dist/client/ad-framework-consent.d.ts.map +0 -1
  91. package/dist/client/ad-framework-consent.js.map +0 -1
  92. package/dist/client/browser-cookie.d.ts.map +0 -1
  93. package/dist/client/browser-cookie.js.map +0 -1
  94. package/dist/client/consent-runtime.d.ts.map +0 -1
  95. package/dist/client/consent-runtime.js.map +0 -1
  96. package/dist/client/consent-storage.d.ts.map +0 -1
  97. package/dist/client/consent-storage.js.map +0 -1
  98. package/dist/client/consent-withdrawal.d.ts.map +0 -1
  99. package/dist/client/consent-withdrawal.js.map +0 -1
  100. package/dist/client/cookie-anonymous-id.d.ts.map +0 -1
  101. package/dist/client/cookie-anonymous-id.js.map +0 -1
  102. package/dist/client/create-client-tracker.d.ts.map +0 -1
  103. package/dist/client/create-client-tracker.js.map +0 -1
  104. package/dist/client/gpc.d.ts.map +0 -1
  105. package/dist/client/gpc.js.map +0 -1
  106. package/dist/client/initial-consent-store.d.ts.map +0 -1
  107. package/dist/client/initial-consent-store.js.map +0 -1
  108. package/dist/client/is-analytics-allowed.d.ts.map +0 -1
  109. package/dist/client/is-analytics-allowed.js.map +0 -1
  110. package/dist/client/server-persisted-anonymous-id.d.ts.map +0 -1
  111. package/dist/client/server-persisted-anonymous-id.js.map +0 -1
  112. package/dist/core/consent-config.d.ts.map +0 -1
  113. package/dist/core/consent-config.js.map +0 -1
  114. package/dist/core/consent-receipt.d.ts.map +0 -1
  115. package/dist/core/consent-receipt.js.map +0 -1
  116. package/dist/core/consent.d.ts.map +0 -1
  117. package/dist/core/consent.js.map +0 -1
  118. package/dist/core/cookie.d.ts.map +0 -1
  119. package/dist/core/cookie.js.map +0 -1
  120. package/dist/core/destination.d.ts.map +0 -1
  121. package/dist/core/destination.js.map +0 -1
  122. package/dist/core/event-catalog.d.ts.map +0 -1
  123. package/dist/core/event-catalog.js.map +0 -1
  124. package/dist/core/event-id.d.ts.map +0 -1
  125. package/dist/core/event-id.js.map +0 -1
  126. package/dist/core/tracked-event.d.ts.map +0 -1
  127. package/dist/core/tracked-event.js.map +0 -1
  128. package/dist/destinations/ad-consent.d.ts.map +0 -1
  129. package/dist/destinations/ad-consent.js.map +0 -1
  130. package/dist/destinations/google-analytics.d.ts.map +0 -1
  131. package/dist/destinations/google-analytics.js.map +0 -1
  132. package/dist/destinations/google-consent.d.ts.map +0 -1
  133. package/dist/destinations/google-consent.js.map +0 -1
  134. package/dist/destinations/meta.d.ts.map +0 -1
  135. package/dist/destinations/meta.js.map +0 -1
  136. package/dist/destinations/microsoft-uet.d.ts.map +0 -1
  137. package/dist/destinations/microsoft-uet.js.map +0 -1
  138. package/dist/destinations/shared.d.ts.map +0 -1
  139. package/dist/destinations/shared.js.map +0 -1
  140. package/dist/destinations/tiktok.d.ts.map +0 -1
  141. package/dist/destinations/tiktok.js.map +0 -1
  142. package/dist/destinations/vercel-analytics.d.ts.map +0 -1
  143. package/dist/destinations/vercel-analytics.js.map +0 -1
  144. package/dist/index.d.ts.map +0 -1
  145. package/dist/index.js.map +0 -1
  146. package/dist/react/consent-banner.d.ts.map +0 -1
  147. package/dist/react/consent-banner.js.map +0 -1
  148. package/dist/react/gtag-consent-bootstrap.d.ts.map +0 -1
  149. package/dist/react/gtag-consent-bootstrap.js.map +0 -1
  150. package/dist/react/use-consent.d.ts.map +0 -1
  151. package/dist/react/use-consent.js.map +0 -1
  152. package/dist/react/use-google-consent-sync.d.ts.map +0 -1
  153. package/dist/react/use-google-consent-sync.js.map +0 -1
  154. package/dist/react/use-initial-consent.d.ts.map +0 -1
  155. package/dist/react/use-initial-consent.js.map +0 -1
  156. package/dist/server/anonymous-id-cookie.d.ts.map +0 -1
  157. package/dist/server/anonymous-id-cookie.js.map +0 -1
  158. package/dist/server/consent-receipt-store.d.ts.map +0 -1
  159. package/dist/server/consent-receipt-store.js.map +0 -1
  160. package/dist/server/consent-receipt.d.ts.map +0 -1
  161. package/dist/server/consent-receipt.js.map +0 -1
  162. package/dist/server/initial-consent.d.ts.map +0 -1
  163. package/dist/server/initial-consent.js.map +0 -1
  164. package/dist/server/measurement-protocol.d.ts.map +0 -1
  165. package/dist/server/measurement-protocol.js.map +0 -1
  166. package/dist/server/region.d.ts.map +0 -1
  167. package/dist/server/region.js.map +0 -1
  168. package/dist/server/user-deletion.d.ts.map +0 -1
  169. package/dist/server/user-deletion.js.map +0 -1
  170. package/dist/tooling/import-protection.d.ts.map +0 -1
  171. package/dist/tooling/import-protection.js.map +0 -1
  172. package/src/adapters/tanstack-start.ts +0 -135
  173. package/src/client/ad-framework-consent.ts +0 -86
  174. package/src/client/browser-cookie.ts +0 -16
  175. package/src/client/consent-runtime.ts +0 -80
  176. package/src/client/consent-storage.ts +0 -125
  177. package/src/client/consent-withdrawal.ts +0 -32
  178. package/src/client/cookie-anonymous-id.ts +0 -112
  179. package/src/client/create-client-tracker.ts +0 -168
  180. package/src/client/gpc.ts +0 -15
  181. package/src/client/initial-consent-store.ts +0 -218
  182. package/src/client/is-analytics-allowed.ts +0 -43
  183. package/src/client/server-persisted-anonymous-id.ts +0 -102
  184. package/src/core/consent-config.ts +0 -27
  185. package/src/core/consent-receipt.ts +0 -115
  186. package/src/core/consent.ts +0 -286
  187. package/src/core/cookie.ts +0 -29
  188. package/src/core/destination.ts +0 -29
  189. package/src/core/event-catalog.ts +0 -54
  190. package/src/core/event-id.ts +0 -8
  191. package/src/core/tracked-event.ts +0 -14
  192. package/src/css/consent.css +0 -119
  193. package/src/destinations/ad-consent.ts +0 -29
  194. package/src/destinations/google-analytics.ts +0 -296
  195. package/src/destinations/google-consent.ts +0 -164
  196. package/src/destinations/meta.ts +0 -96
  197. package/src/destinations/microsoft-uet.ts +0 -86
  198. package/src/destinations/shared.ts +0 -45
  199. package/src/destinations/tiktok.ts +0 -85
  200. package/src/destinations/vercel-analytics.ts +0 -39
  201. package/src/index.ts +0 -171
  202. package/src/react/consent-banner.tsx +0 -360
  203. package/src/react/gtag-consent-bootstrap.tsx +0 -48
  204. package/src/react/use-consent.ts +0 -119
  205. package/src/react/use-google-consent-sync.ts +0 -38
  206. package/src/react/use-initial-consent.ts +0 -22
  207. package/src/server/anonymous-id-cookie.ts +0 -103
  208. package/src/server/consent-receipt-store.ts +0 -79
  209. package/src/server/consent-receipt.ts +0 -89
  210. package/src/server/initial-consent.ts +0 -46
  211. package/src/server/measurement-protocol.ts +0 -114
  212. package/src/server/region.ts +0 -77
  213. package/src/server/user-deletion.ts +0 -93
  214. package/src/tooling/import-protection.ts +0 -22
@@ -1,286 +0,0 @@
1
- /**
2
- * @since 0.5.0-canary.4
3
- */
4
- export type ConsentRegion = "eu" | "other" | "us" | "vn";
5
-
6
- /**
7
- * @since 1.0.0-canary.6
8
- */
9
- export const CONSENT_REGIONS: ReadonlyArray<ConsentRegion> = ["eu", "other", "us", "vn"];
10
-
11
- /**
12
- * Guards a region read from untrusted storage (a cached `InitialConsent`, a cookie).
13
- *
14
- * @since 1.0.0-canary.6
15
- */
16
- export function isConsentRegion(value: unknown): value is ConsentRegion {
17
- return CONSENT_REGIONS.includes(value as ConsentRegion);
18
- }
19
-
20
- /**
21
- * @since 0.5.0-canary.4
22
- */
23
- export type ConsentMode = "opt-in" | "opt-out";
24
-
25
- /**
26
- * GDPR (EU) and the PDPL — Luật 91/2025/QH15 (VN) — require explicit opt-in before any non-essential
27
- * tracking; CCPA/CPRA (US) defaults to opt-out instead. There is no single global default
28
- * that satisfies both, so the mode is resolved per region.
29
- *
30
- * @since 0.5.0-canary.4
31
- */
32
- export function resolveConsentMode(region: ConsentRegion): ConsentMode {
33
- switch (region) {
34
- case "eu":
35
- case "vn": {
36
- return "opt-in";
37
- }
38
-
39
- case "other":
40
- case "us": {
41
- return "opt-out";
42
- }
43
- }
44
- }
45
-
46
- /**
47
- * Consent purposes, mirroring Google Consent Mode v2's split: `analytics` gates
48
- * measurement (`analytics_storage`), `ads` gates advertising storage and data sharing
49
- * (`ad_storage`/`ad_user_data`/`ad_personalization`).
50
- *
51
- * @since 0.5.0-canary.4
52
- */
53
- export type ConsentCategory = "ads" | "analytics";
54
-
55
- /**
56
- * @since 0.5.0-canary.4
57
- */
58
- export const CONSENT_CATEGORIES: ReadonlyArray<ConsentCategory> = ["ads", "analytics"];
59
-
60
- /**
61
- * Per-category consent — GDPR consent must be granular per purpose, so a single
62
- * granted/denied flag can't represent it. `true` means the visitor granted that purpose.
63
- *
64
- * @since 0.5.0-canary.4
65
- */
66
- export type ConsentDecision = Record<ConsentCategory, boolean>;
67
-
68
- /**
69
- * A decision granting exactly the given categories and denying every other one.
70
- *
71
- * @since 0.5.0-canary.4
72
- */
73
- export function createConsentDecision(grantedCategories: ReadonlyArray<ConsentCategory>): ConsentDecision {
74
- return { ads: grantedCategories.includes("ads"), analytics: grantedCategories.includes("analytics") };
75
- }
76
-
77
- function isPlainObject(value: unknown): value is Record<string, unknown> {
78
- return typeof value === "object" && value !== null && !Array.isArray(value);
79
- }
80
-
81
- /**
82
- * Guards stored JSON — the record is tamperable, so every category must be an explicit boolean.
83
- *
84
- * @since 0.5.0-canary.4
85
- */
86
- export function isConsentDecision(value: unknown): value is ConsentDecision {
87
- return isPlainObject(value) && CONSENT_CATEGORIES.every((category) => typeof value[category] === "boolean");
88
- }
89
-
90
- /**
91
- * Re-derives a clean per-category decision from a tamperable record — extra keys are
92
- * dropped, every category becomes an explicit boolean. The one normalization rule every
93
- * reader of stored consent shares (e.g. `readStoredDecision`).
94
- *
95
- * @since 1.0.0-canary.6
96
- */
97
- export function normalizeConsentDecision(decision: ConsentDecision): ConsentDecision {
98
- return createConsentDecision(CONSENT_CATEGORIES.filter((category) => decision[category]));
99
- }
100
-
101
- /**
102
- * Guards a persisted consent record read from untrusted storage (e.g. `localStorage`).
103
- *
104
- * @since 1.0.0-canary.6
105
- */
106
- export function isConsentRecord(value: unknown): value is ConsentRecord {
107
- return (
108
- isPlainObject(value) &&
109
- isConsentDecision(value.decision) &&
110
- typeof value.policyVersion === "string" &&
111
- typeof value.timestamp === "number"
112
- );
113
- }
114
-
115
- /**
116
- * @since 1.0.0-canary.6
117
- */
118
- export interface ResolveDefaultConsentOptions {
119
- /** A "do not sell or share" opt-out — forces `ads` denied under opt-out regions. */
120
- hasGlobalPrivacyControlSignal: boolean;
121
- mode: ConsentMode;
122
- requestedCategories: ReadonlyArray<ConsentCategory>;
123
- }
124
-
125
- /**
126
- * The effective consent before the visitor decides: opt-in regions start all-denied,
127
- * opt-out regions grant the categories the app asks for.
128
- *
129
- * @remarks
130
- * A Global Privacy Control signal is a "do not sell or share" opt-out (CCPA/CPRA), so it
131
- * forces `ads` denied — it does not withdraw first-party analytics measurement.
132
- *
133
- * @since 0.5.0-canary.4
134
- */
135
- export function resolveDefaultConsent(options: ResolveDefaultConsentOptions): ConsentDecision {
136
- const { hasGlobalPrivacyControlSignal, mode, requestedCategories } = options;
137
-
138
- if (mode === "opt-in") {
139
- return createConsentDecision([]);
140
- }
141
-
142
- const decision = createConsentDecision(requestedCategories);
143
-
144
- if (hasGlobalPrivacyControlSignal) {
145
- decision.ads = false;
146
- }
147
-
148
- return decision;
149
- }
150
-
151
- /**
152
- * Region-resolved consent defaults — typically from `resolveInitialConsent` on a
153
- * per-request server lane, then handed to the client so it never re-guesses the mode.
154
- *
155
- * @remarks
156
- * Drive UI and gates from `mode` (recompute `effectiveConsent` with live navigator GPC).
157
- * `defaultConsent` is the server snapshot — useful when you need request-time `sec-gpc`
158
- * as-is; analytics-only apps can ignore it because GPC only forces `ads` denied.
159
- *
160
- * @since 1.0.0-canary.6
161
- */
162
- export interface InitialConsent {
163
- defaultConsent: ConsentDecision;
164
- mode: ConsentMode;
165
- region: ConsentRegion;
166
- }
167
-
168
- /**
169
- * What shared HTML bakes and what an unknown-country request resolves to: all categories
170
- * denied under opt-in — the one default that is safe to show any visitor anywhere.
171
- * Prerendered/CDN-cached markup must carry nothing region-specific, and a missing geo
172
- * header means "unknown visitor", never "known non-EU visitor".
173
- *
174
- * @since 1.0.0-canary.6
175
- */
176
- export const STRICTEST_INITIAL_CONSENT: InitialConsent = Object.freeze({
177
- defaultConsent: Object.freeze(createConsentDecision([])),
178
- mode: "opt-in",
179
- region: "other",
180
- });
181
-
182
- /**
183
- * Guards an `InitialConsent` read from untrusted storage (a session cache, a cookie).
184
- * Enforces the mode/region pairing rule; the one deliberate exception is
185
- * {@link STRICTEST_INITIAL_CONSENT}'s own pairing — stricter than the region's usual
186
- * mode, never looser.
187
- *
188
- * @since 1.0.0-canary.6
189
- */
190
- export function isInitialConsent(value: unknown): value is InitialConsent {
191
- if (!isPlainObject(value)) {
192
- return false;
193
- }
194
-
195
- const mode = value.mode;
196
- const region = value.region;
197
-
198
- if (
199
- (mode !== "opt-in" && mode !== "opt-out") ||
200
- !isConsentRegion(region) ||
201
- !isConsentDecision(value.defaultConsent)
202
- ) {
203
- return false;
204
- }
205
-
206
- return (
207
- mode === resolveConsentMode(region) ||
208
- (mode === STRICTEST_INITIAL_CONSENT.mode && region === STRICTEST_INITIAL_CONSENT.region)
209
- );
210
- }
211
-
212
- /**
213
- * Vietnam's PDPL and GDPR both expect a record of *when* and *under what policy* consent
214
- * was given, not just per-category flags — `policyVersion` lets a later policy change
215
- * invalidate stale consent.
216
- *
217
- * @since 0.5.0-canary.4
218
- */
219
- export interface ConsentRecord {
220
- decision: ConsentDecision;
221
- policyVersion: string;
222
- timestamp: number;
223
- }
224
-
225
- /**
226
- * Persistence backend for the visitor's consent decision — swap in your own
227
- * implementation for non-browser environments or a different persistence layer.
228
- *
229
- * @since 0.5.0-canary.4
230
- */
231
- export interface ConsentStorage {
232
- clear: () => void;
233
- load: () => ConsentRecord | undefined;
234
- save: (record: ConsentRecord) => void;
235
- /**
236
- * Notify on any change to the stored record, including writes from other tabs — this
237
- * is what drives `useConsent`'s external-store subscription. Returns an unsubscribe.
238
- */
239
- subscribe: (listener: () => void) => () => void;
240
- }
241
-
242
- /**
243
- * The stored decision if one exists under the current `policyVersion`, normalized to drop
244
- * any tampered extra keys — `undefined` if there is none yet, the record is malformed, or
245
- * it was recorded under a superseded policy version.
246
- *
247
- * @since 1.0.0-canary.6
248
- */
249
- export function readStoredDecision(storage: ConsentStorage, policyVersion: string): ConsentDecision | undefined {
250
- const record = storage.load();
251
-
252
- if (record?.policyVersion !== policyVersion || !isConsentDecision(record.decision)) {
253
- return undefined;
254
- }
255
-
256
- return normalizeConsentDecision(record.decision);
257
- }
258
-
259
- /**
260
- * @since 1.0.0-canary.6
261
- */
262
- export interface ResolveEffectiveConsentOptions {
263
- /** A "do not sell or share" opt-out — forces `ads` denied under opt-out regions. */
264
- hasGlobalPrivacyControlSignal: boolean;
265
- mode: ConsentMode;
266
- policyVersion: string;
267
- requestedCategories: ReadonlyArray<ConsentCategory>;
268
- storage: ConsentStorage;
269
- }
270
-
271
- /**
272
- * The consent a tracker should honor right now: the stored decision if one exists, else
273
- * the region default — the same rule `useConsent` applies to its `effectiveConsent`, so a
274
- * non-React gate (e.g. a tracker's `isAnalyticsAllowed` option) doesn't have to reimplement
275
- * "read storage, validate the policy version, fall back to `resolveDefaultConsent`" itself.
276
- *
277
- * @since 1.0.0-canary.6
278
- */
279
- export function resolveEffectiveConsent(options: ResolveEffectiveConsentOptions): ConsentDecision {
280
- const { hasGlobalPrivacyControlSignal, mode, policyVersion, requestedCategories, storage } = options;
281
-
282
- return (
283
- readStoredDecision(storage, policyVersion) ??
284
- resolveDefaultConsent({ hasGlobalPrivacyControlSignal, mode, requestedCategories })
285
- );
286
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * Default lifetime shared by the anonymous-id and consent cookies — one year.
3
- *
4
- * @since 1.0.0-canary.6
5
- */
6
- export const ONE_YEAR_IN_SECONDS: number = 60 * 60 * 24 * 365;
7
-
8
- /**
9
- * The value of `cookieName` inside a cookie string — a request `Cookie` header or
10
- * `document.cookie`, which share the `name=value; name2=value2` wire format — or
11
- * `undefined` when absent. The name must match exactly; a longer prefix sibling is ignored.
12
- *
13
- * @since 1.0.0-canary.6
14
- */
15
- export function readCookieValue(cookieString: string | null | undefined, cookieName: string): string | undefined {
16
- if (!cookieString) {
17
- return undefined;
18
- }
19
-
20
- for (const part of cookieString.split(";")) {
21
- const separatorIndex = part.indexOf("=");
22
-
23
- if (separatorIndex !== -1 && part.slice(0, separatorIndex).trim() === cookieName) {
24
- return part.slice(separatorIndex + 1).trim();
25
- }
26
- }
27
-
28
- return undefined;
29
- }
@@ -1,29 +0,0 @@
1
- import type { TrackedEvent } from "#/core/tracked-event";
2
-
3
- /**
4
- * Fan-out target for tracked events. The tracker depends only on this interface, never
5
- * on a specific provider SDK — destinations own their transport (gtag.js and Vercel both
6
- * queue and batch in-page), so `send` is fire-and-forget at track time.
7
- *
8
- * @since 0.5.0-canary.4
9
- */
10
- export interface Destination {
11
- /**
12
- * "exempt" keeps this destination receiving events while the tracker's analytics gate
13
- * is closed — only for cookieless, identifier-free sinks: gated envelopes carry no
14
- * `anonymousId`, and nothing is persisted.
15
- *
16
- * @defaultValue "required"
17
- */
18
- consentRequirement?: "exempt" | "required" | undefined;
19
- name: string;
20
- /**
21
- * Erase this destination's trace of the subject on withdrawal (spec-data-subject-rights
22
- * §3): clear the destination's cookies and stop sending, and — where the platform offers
23
- * one — request per-visitor deletion. Optional: a sink with nothing to erase omits it.
24
- * Invoked at most once per withdrawal; the tracker swallows its failures.
25
- */
26
- onErasure?: ((id: string) => Promise<void> | void) | undefined;
27
- /** Must always return a `Promise` — mark `send` `async` even if the body has no `await`. */
28
- send: (event: TrackedEvent) => Promise<void>;
29
- }
@@ -1,54 +0,0 @@
1
- import type { StandardSchemaV1 } from "@standard-schema/spec";
2
-
3
- /**
4
- * One event's schema. Any Standard Schema library works (zod, zod/mini, valibot, ...) —
5
- * the client bundle only pays for the one the app already ships.
6
- *
7
- * @since 0.5.0-canary.4
8
- */
9
- export interface EventDefinition<Schema extends StandardSchemaV1 = StandardSchemaV1> {
10
- schema: Schema;
11
- }
12
-
13
- /**
14
- * An app-defined map of event name to its definition. `@codefast/tracking` ships no
15
- * events of its own — every consuming app builds and passes in its own catalog.
16
- *
17
- * @since 0.5.0-canary.4
18
- */
19
- export type EventCatalog = Record<string, EventDefinition>;
20
-
21
- /**
22
- * Identity helper for declaring a catalog with inference — nicer call-site than
23
- * `satisfies EventCatalog` when the catalog is assembled across multiple files.
24
- *
25
- * @since 0.5.0-canary.4
26
- */
27
- export function defineEventCatalog<Catalog extends EventCatalog>(catalog: Catalog): Catalog {
28
- return catalog;
29
- }
30
-
31
- /**
32
- * Validates event properties against the catalog schema and returns the parsed output
33
- * (unknown keys stripped / transforms applied) so destinations never see raw input.
34
- *
35
- * @throws Error when validation reports issues, or when the schema validates asynchronously —
36
- * tracking sits on synchronous call paths, so async schemas are unsupported by design.
37
- *
38
- * @since 1.0.0-canary.6
39
- */
40
- export function assertValidEventProperties(schema: StandardSchemaV1, eventName: string, properties: unknown): unknown {
41
- const result = schema["~standard"].validate(properties);
42
-
43
- if (result instanceof Promise) {
44
- throw new TypeError(`Event "${eventName}" uses an async schema — event properties must validate synchronously`);
45
- }
46
-
47
- if (result.issues) {
48
- const detail = result.issues.map((issue) => issue.message).join("; ");
49
-
50
- throw new Error(`Invalid properties for event "${eventName}": ${detail}`);
51
- }
52
-
53
- return result.value;
54
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * Stamped on every envelope at track time — destinations that dedupe can key on it.
3
- *
4
- * @since 0.5.0-canary.4
5
- */
6
- export function generateEventId(): string {
7
- return crypto.randomUUID();
8
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * The event envelope destinations receive. `type` stays a discriminant so a future envelope
3
- * kind is an additive union member, not a wire-format break.
4
- *
5
- * @since 0.5.0-canary.4
6
- */
7
- export interface TrackedEvent {
8
- anonymousId: string;
9
- eventId: string;
10
- name: string;
11
- properties: Record<string, unknown>;
12
- timestamp: number;
13
- type: "track";
14
- }
@@ -1,119 +0,0 @@
1
- /* -------------------------------------------------------------------------
2
- * Optional consent UI theme
3
- *
4
- * Plain CSS over the components' data-slot attributes — no Tailwind, no
5
- * design-system dependency. Import it for a sensible default look:
6
- *
7
- * @import "@codefast/tracking/css/consent.css";
8
- *
9
- * Every color routes through a `--consent-*` custom property with a
10
- * `light-dark()` fallback, so a host app can retheme by defining those
11
- * properties (or skip this file entirely and style the slots itself).
12
- * ------------------------------------------------------------------------- */
13
-
14
- [data-slot="consent-banner"] {
15
- position: fixed;
16
- inset-inline: 1rem;
17
- bottom: 1rem;
18
- z-index: 50;
19
- margin-inline: auto;
20
- max-width: 24rem;
21
- padding: 1.25rem;
22
- border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
23
- border-radius: 1rem;
24
- background: var(--consent-surface, light-dark(#fff, #1c1c1e));
25
- color: var(--consent-fg, light-dark(#1c1c1e, #f2f2f7));
26
- box-shadow: 0 12px 32px rgb(0 0 0 / 18%);
27
- font-size: 0.875rem;
28
- line-height: 1.5;
29
- animation: consent-banner-in 220ms ease-out;
30
- }
31
-
32
- @media (min-width: 640px) {
33
- [data-slot="consent-banner"] {
34
- inset-inline: auto 1rem;
35
- }
36
- }
37
-
38
- @keyframes consent-banner-in {
39
- from {
40
- opacity: 0;
41
- transform: translateY(1rem);
42
- }
43
- }
44
-
45
- @media (prefers-reduced-motion: reduce) {
46
- [data-slot="consent-banner"] {
47
- animation: none;
48
- }
49
- }
50
-
51
- [data-slot="consent-title"] {
52
- margin: 0 0 0.375rem;
53
- font-size: 0.875rem;
54
- font-weight: 600;
55
- }
56
-
57
- [data-slot="consent-description"] {
58
- margin: 0 0 1rem;
59
- color: var(--consent-muted, light-dark(rgb(28 28 30 / 65%), rgb(242 242 247 / 65%)));
60
- }
61
-
62
- [data-slot="consent-description"] a {
63
- color: inherit;
64
- text-decoration: underline;
65
- text-underline-offset: 0.2em;
66
- }
67
-
68
- [data-slot="consent-preferences"] {
69
- display: flex;
70
- flex-direction: column;
71
- gap: 0.5rem;
72
- margin-bottom: 1rem;
73
- }
74
-
75
- [data-slot="consent-category"] {
76
- display: flex;
77
- gap: 0.5rem;
78
- align-items: baseline;
79
- }
80
-
81
- [data-slot="consent-actions"] {
82
- display: flex;
83
- gap: 0.5rem;
84
- }
85
-
86
- [data-slot="consent-action"] {
87
- flex: 1;
88
- padding: 0.5rem 0.75rem;
89
- border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
90
- border-radius: 0.5rem;
91
- background: transparent;
92
- color: inherit;
93
- font: inherit;
94
- font-weight: 500;
95
- cursor: pointer;
96
- }
97
-
98
- /* The affirmative actions carry the accent; Reject stays equally prominent, just outlined. */
99
- [data-slot="consent-action"][data-action="accept"],
100
- [data-slot="consent-action"][data-action="save"] {
101
- border-color: transparent;
102
- background: var(--consent-accent, light-dark(#1c1c1e, #f2f2f7));
103
- color: var(--consent-accent-fg, light-dark(#fff, #1c1c1e));
104
- }
105
-
106
- [data-slot="consent-toggle"] {
107
- position: fixed;
108
- inset-inline-start: 1rem;
109
- bottom: 1rem;
110
- z-index: 50;
111
- padding: 0.375rem 0.75rem;
112
- border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
113
- border-radius: 9999px;
114
- background: var(--consent-surface, light-dark(#fff, #1c1c1e));
115
- color: var(--consent-muted, light-dark(rgb(28 28 30 / 65%), rgb(242 242 247 / 65%)));
116
- font: inherit;
117
- font-size: 0.75rem;
118
- cursor: pointer;
119
- }
@@ -1,29 +0,0 @@
1
- import type { ConsentDecision } from "#/core/consent";
2
-
3
- /**
4
- * The normalized two-lever ad-platform consent state derived from one `{ ads, analytics }`
5
- * decision (spec-destinations §5): `analytics` drives whether events transmit at all, and
6
- * `ads` drives Limited Data Use. The levers are independent — a US opt-out (`ads: false`)
7
- * restricts to LDU but MUST NOT withhold first-party `analytics` (spec-consent §3). Every
8
- * ad destination consumes this same shape so per-vendor mappings cannot drift.
9
- *
10
- * @since 1.0.0-canary.7
11
- */
12
- export interface AdConsentState {
13
- /** `ads` denied → the platform must restrict to Limited Data Use (Meta/TikTok LDU, UET `ad_storage` denied). */
14
- limitedDataUse: boolean;
15
- /** `analytics` granted → events may transmit; denied → hold/revoke and send nothing. */
16
- transmissionAllowed: boolean;
17
- }
18
-
19
- /**
20
- * Normalizes a package `ConsentDecision` to the vendor-agnostic {@link AdConsentState}.
21
- *
22
- * @since 1.0.0-canary.7
23
- */
24
- export function toAdConsentState(decision: ConsentDecision): AdConsentState {
25
- return {
26
- limitedDataUse: !decision.ads,
27
- transmissionAllowed: decision.analytics,
28
- };
29
- }