@codefast/tracking 0.7.1 → 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 +13 -0
  2. package/README.md +235 -105
  3. package/dist/adapters/tanstack-start.d.ts +1 -2
  4. package/dist/adapters/tanstack-start.js +1 -2
  5. package/dist/client/ad-framework-consent.d.ts +1 -2
  6. package/dist/client/ad-framework-consent.js +1 -2
  7. package/dist/client/browser-cookie.d.ts +1 -2
  8. package/dist/client/browser-cookie.js +1 -2
  9. package/dist/client/consent-runtime.d.ts +1 -2
  10. package/dist/client/consent-runtime.js +1 -2
  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 +1 -2
  14. package/dist/client/consent-withdrawal.js +1 -2
  15. package/dist/client/cookie-anonymous-id.d.ts +1 -2
  16. package/dist/client/cookie-anonymous-id.js +1 -2
  17. package/dist/client/create-client-tracker.d.ts +1 -2
  18. package/dist/client/create-client-tracker.js +1 -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 +1 -2
  22. package/dist/client/initial-consent-store.js +1 -2
  23. package/dist/client/is-analytics-allowed.d.ts +1 -2
  24. package/dist/client/is-analytics-allowed.js +1 -2
  25. package/dist/client/server-persisted-anonymous-id.d.ts +1 -2
  26. package/dist/client/server-persisted-anonymous-id.js +1 -2
  27. package/dist/core/consent-config.d.ts +1 -2
  28. package/dist/core/consent-config.js +1 -2
  29. package/dist/core/consent-receipt.d.ts +1 -2
  30. package/dist/core/consent-receipt.js +1 -2
  31. package/dist/core/consent.d.ts +1 -2
  32. package/dist/core/consent.js +1 -2
  33. package/dist/core/cookie.d.ts +1 -2
  34. package/dist/core/cookie.js +1 -2
  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 +1 -2
  38. package/dist/core/event-catalog.js +1 -2
  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 +1 -2
  44. package/dist/destinations/ad-consent.js +1 -2
  45. package/dist/destinations/google-analytics.d.ts +1 -2
  46. package/dist/destinations/google-analytics.js +1 -2
  47. package/dist/destinations/google-consent.d.ts +1 -2
  48. package/dist/destinations/google-consent.js +1 -2
  49. package/dist/destinations/meta.d.ts +1 -2
  50. package/dist/destinations/meta.js +1 -2
  51. package/dist/destinations/microsoft-uet.d.ts +1 -2
  52. package/dist/destinations/microsoft-uet.js +1 -2
  53. package/dist/destinations/shared.d.ts +1 -2
  54. package/dist/destinations/shared.js +1 -2
  55. package/dist/destinations/tiktok.d.ts +1 -2
  56. package/dist/destinations/tiktok.js +1 -2
  57. package/dist/destinations/vercel-analytics.d.ts +1 -2
  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 +1 -2
  62. package/dist/react/consent-banner.js +1 -2
  63. package/dist/react/gtag-consent-bootstrap.d.ts +1 -2
  64. package/dist/react/gtag-consent-bootstrap.js +1 -2
  65. package/dist/react/use-consent.d.ts +1 -2
  66. package/dist/react/use-consent.js +1 -2
  67. package/dist/react/use-google-consent-sync.d.ts +1 -2
  68. package/dist/react/use-google-consent-sync.js +1 -2
  69. package/dist/react/use-initial-consent.d.ts +1 -2
  70. package/dist/react/use-initial-consent.js +1 -2
  71. package/dist/server/anonymous-id-cookie.d.ts +1 -2
  72. package/dist/server/anonymous-id-cookie.js +1 -2
  73. package/dist/server/consent-receipt-store.d.ts +1 -2
  74. package/dist/server/consent-receipt-store.js +1 -2
  75. package/dist/server/consent-receipt.d.ts +1 -2
  76. package/dist/server/consent-receipt.js +1 -2
  77. package/dist/server/initial-consent.d.ts +1 -2
  78. package/dist/server/initial-consent.js +1 -2
  79. package/dist/server/measurement-protocol.d.ts +1 -2
  80. package/dist/server/measurement-protocol.js +1 -2
  81. package/dist/server/region.d.ts +1 -2
  82. package/dist/server/region.js +1 -2
  83. package/dist/server/user-deletion.d.ts +1 -2
  84. package/dist/server/user-deletion.js +1 -2
  85. package/dist/tooling/import-protection.d.ts +1 -2
  86. package/dist/tooling/import-protection.js +1 -2
  87. package/package.json +10 -61
  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 -139
  173. package/src/client/ad-framework-consent.ts +0 -88
  174. package/src/client/browser-cookie.ts +0 -16
  175. package/src/client/consent-runtime.ts +0 -82
  176. package/src/client/consent-storage.ts +0 -125
  177. package/src/client/consent-withdrawal.ts +0 -34
  178. package/src/client/cookie-anonymous-id.ts +0 -116
  179. package/src/client/create-client-tracker.ts +0 -174
  180. package/src/client/gpc.ts +0 -15
  181. package/src/client/initial-consent-store.ts +0 -220
  182. package/src/client/is-analytics-allowed.ts +0 -45
  183. package/src/client/server-persisted-anonymous-id.ts +0 -104
  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 -298
  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 -304
  195. package/src/destinations/google-consent.ts +0 -166
  196. package/src/destinations/meta.ts +0 -98
  197. package/src/destinations/microsoft-uet.ts +0 -88
  198. package/src/destinations/shared.ts +0 -51
  199. package/src/destinations/tiktok.ts +0 -87
  200. package/src/destinations/vercel-analytics.ts +0 -41
  201. package/src/index.ts +0 -171
  202. package/src/react/consent-banner.tsx +0 -386
  203. package/src/react/gtag-consent-bootstrap.tsx +0 -50
  204. package/src/react/use-consent.ts +0 -123
  205. package/src/react/use-google-consent-sync.ts +0 -40
  206. package/src/react/use-initial-consent.ts +0 -22
  207. package/src/server/anonymous-id-cookie.ts +0 -105
  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 -48
  211. package/src/server/measurement-protocol.ts +0 -116
  212. package/src/server/region.ts +0 -77
  213. package/src/server/user-deletion.ts +0 -97
  214. package/src/tooling/import-protection.ts +0 -22
@@ -1,386 +0,0 @@
1
- "use client";
2
-
3
- import type { ComponentProps, Dispatch, ReactNode, SetStateAction } from "react";
4
- import { createContext, useContext, useState } from "react";
5
-
6
- import type { ConsentCategory, ConsentDecision } from "#/core/consent";
7
- import type { UseConsentResult } from "#/react/use-consent";
8
-
9
- interface ConsentBannerContextValue {
10
- consent: UseConsentResult;
11
- /** The in-progress per-category selection — `undefined` while the preferences layer is closed. */
12
- pending: ConsentDecision | undefined;
13
- setPending: Dispatch<SetStateAction<ConsentDecision | undefined>>;
14
- }
15
-
16
- const ConsentBannerContext = createContext<ConsentBannerContextValue | null>(null);
17
-
18
- function useConsentBannerContext(part: string): ConsentBannerContextValue {
19
- const context = useContext(ConsentBannerContext);
20
-
21
- if (context === null) {
22
- throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
23
- }
24
-
25
- return context;
26
- }
27
-
28
- /**
29
- * Props for {@link ConsentBanner}.
30
- *
31
- * @since 0.5.0-canary.4
32
- */
33
- export interface ConsentBannerProps extends ComponentProps<"section"> {
34
- consent: UseConsentResult;
35
- /** Overrides the default `isPromptNeeded` gating — e.g. to reopen the banner as a settings panel. */
36
- open?: boolean | undefined;
37
- }
38
-
39
- /**
40
- * Non-blocking opt-in prompt for GDPR/PDPL (VN) regions, as composable parts — the root owns
41
- * the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
42
- * `Save` parts wire their own clicks to the consent hook, so any markup (including a
43
- * design system's button styles via `className`) slots in. Renders nothing once a decision
44
- * exists or the region defaults to opt-out (`consent.isPromptNeeded`), unless `open` says
45
- * otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
46
- * neither traps focus nor blocks the page, so the dialog semantics would over-promise.
47
- * Unstyled by default — style via `className`/`data-slot` selectors, or import the
48
- * optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
49
- * between `prompt` and `preferences` for state-dependent styling.
50
- *
51
- * @since 0.5.0-canary.4
52
- */
53
- export function ConsentBanner({ children, consent, open, ...props }: ConsentBannerProps): ReactNode {
54
- const isVisible = open ?? consent.isPromptNeeded;
55
-
56
- const [pending, setPending] = useState<ConsentDecision | undefined>(undefined);
57
-
58
- if (!isVisible) {
59
- // Closing resets the layer — a reopened banner starts at the prompt, not mid-preferences.
60
- if (pending !== undefined) {
61
- setPending(undefined);
62
- }
63
-
64
- return null;
65
- }
66
-
67
- return (
68
- <ConsentBannerContext.Provider value={{ consent, pending, setPending }}>
69
- <section
70
- aria-label="Cookie consent"
71
- data-slot="consent-banner"
72
- data-state={pending === undefined ? "prompt" : "preferences"}
73
- {...props}
74
- >
75
- {children}
76
- </section>
77
- </ConsentBannerContext.Provider>
78
- );
79
- }
80
-
81
- /**
82
- * Props for {@link ConsentBannerTitle}.
83
- *
84
- * @since 0.5.0-canary.5
85
- */
86
- export interface ConsentBannerTitleProps extends ComponentProps<"h2"> {}
87
-
88
- /**
89
- * Renders the banner's heading.
90
- *
91
- * @since 0.5.0-canary.5
92
- */
93
- export function ConsentBannerTitle({ children, ...props }: ConsentBannerTitleProps): ReactNode {
94
- return (
95
- <h2 data-slot="consent-title" {...props}>
96
- {children}
97
- </h2>
98
- );
99
- }
100
-
101
- /**
102
- * Props for {@link ConsentBannerDescription}.
103
- *
104
- * @since 0.5.0-canary.5
105
- */
106
- export interface ConsentBannerDescriptionProps extends ComponentProps<"p"> {}
107
-
108
- /**
109
- * Hosts the message and the privacy-policy link informed consent requires.
110
- *
111
- * @since 0.5.0-canary.5
112
- */
113
- export function ConsentBannerDescription(props: ConsentBannerDescriptionProps): ReactNode {
114
- return <p data-slot="consent-description" {...props} />;
115
- }
116
-
117
- /**
118
- * Props for {@link ConsentBannerActions}.
119
- *
120
- * @since 0.5.0-canary.5
121
- */
122
- export interface ConsentBannerActionsProps extends ComponentProps<"div"> {}
123
-
124
- /**
125
- * Renders the container grouping the banner's action buttons.
126
- *
127
- * @since 0.5.0-canary.5
128
- */
129
- export function ConsentBannerActions(props: ConsentBannerActionsProps): ReactNode {
130
- return <div data-slot="consent-actions" {...props} />;
131
- }
132
-
133
- /**
134
- * Props for {@link ConsentBannerAccept}.
135
- *
136
- * @since 0.5.0-canary.5
137
- */
138
- export interface ConsentBannerAcceptProps extends Omit<ComponentProps<"button">, "type"> {}
139
-
140
- /**
141
- * Grants the categories the app requested (`useConsent`'s `requestedCategories`) — never more.
142
- *
143
- * @since 0.5.0-canary.5
144
- */
145
- export function ConsentBannerAccept({ onClick, ...props }: ConsentBannerAcceptProps): ReactNode {
146
- const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
147
-
148
- return (
149
- <button
150
- data-action="accept"
151
- data-slot="consent-action"
152
- {...props}
153
- // the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
154
- onClick={(event) => {
155
- onClick?.(event);
156
- consent.grantAll();
157
- setPending(undefined);
158
- }}
159
- type="button"
160
- />
161
- );
162
- }
163
-
164
- /**
165
- * Props for {@link ConsentBannerReject}.
166
- *
167
- * @since 0.5.0-canary.5
168
- */
169
- export interface ConsentBannerRejectProps extends Omit<ComponentProps<"button">, "type"> {}
170
-
171
- /**
172
- * Denies every category via the consent hook's `denyAll` — rejecting stays as easy as accepting.
173
- *
174
- * @since 0.5.0-canary.5
175
- */
176
- export function ConsentBannerReject({ onClick, ...props }: ConsentBannerRejectProps): ReactNode {
177
- const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
178
-
179
- return (
180
- <button
181
- data-action="reject"
182
- data-slot="consent-action"
183
- {...props}
184
- // the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
185
- onClick={(event) => {
186
- onClick?.(event);
187
- consent.denyAll();
188
- setPending(undefined);
189
- }}
190
- type="button"
191
- />
192
- );
193
- }
194
-
195
- /**
196
- * Props for {@link ConsentBannerCustomize}.
197
- *
198
- * @since 0.5.0-canary.5
199
- */
200
- export interface ConsentBannerCustomizeProps extends Omit<ComponentProps<"button">, "type"> {}
201
-
202
- /**
203
- * Opens the per-category preferences layer, starting from the current effective state so
204
- * nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
205
- * GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
206
- *
207
- * @since 0.5.0-canary.5
208
- */
209
- export function ConsentBannerCustomize({ onClick, ...props }: ConsentBannerCustomizeProps): ReactNode {
210
- const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
211
-
212
- if (pending !== undefined) {
213
- return null;
214
- }
215
-
216
- return (
217
- <button
218
- data-action="customize"
219
- data-slot="consent-action"
220
- {...props}
221
- // the part owns the layer toggle — composed so a consumer onClick still runs
222
- onClick={(event) => {
223
- onClick?.(event);
224
- setPending(consent.effectiveConsent);
225
- }}
226
- type="button"
227
- />
228
- );
229
- }
230
-
231
- /**
232
- * Props for {@link ConsentBannerPreferences}.
233
- *
234
- * @since 0.5.0-canary.5
235
- */
236
- export interface ConsentBannerPreferencesProps extends ComponentProps<"div"> {}
237
-
238
- /**
239
- * Container for the per-category rows — renders only while the preferences layer is open.
240
- *
241
- * @since 0.5.0-canary.5
242
- */
243
- export function ConsentBannerPreferences(props: ConsentBannerPreferencesProps): ReactNode {
244
- const { pending } = useConsentBannerContext("ConsentBannerPreferences");
245
-
246
- if (pending === undefined) {
247
- return null;
248
- }
249
-
250
- return <div data-slot="consent-preferences" {...props} />;
251
- }
252
-
253
- /**
254
- * Props for {@link ConsentBannerCategory}.
255
- *
256
- * @since 0.5.0-canary.5
257
- */
258
- export interface ConsentBannerCategoryProps extends ComponentProps<"label"> {
259
- category: ConsentCategory;
260
- }
261
-
262
- /**
263
- * One checkbox row of the preferences layer — `children` is the visitor-facing wording.
264
- *
265
- * @since 0.5.0-canary.5
266
- */
267
- export function ConsentBannerCategory({ category, children, ...props }: ConsentBannerCategoryProps): ReactNode {
268
- const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
269
-
270
- if (pending === undefined) {
271
- return null;
272
- }
273
-
274
- return (
275
- <label data-slot="consent-category" {...props}>
276
- <input
277
- checked={pending[category]}
278
- data-slot="consent-category-checkbox"
279
- onChange={(event) => {
280
- setPending({ ...pending, [category]: event.target.checked });
281
- }}
282
- type="checkbox"
283
- />
284
- {children}
285
- </label>
286
- );
287
- }
288
-
289
- /**
290
- * Props for {@link ConsentBannerSave}.
291
- *
292
- * @since 0.5.0-canary.5
293
- */
294
- export interface ConsentBannerSaveProps extends Omit<ComponentProps<"button">, "type"> {}
295
-
296
- /**
297
- * Persists the pending per-category selection — renders only while the preferences layer is open.
298
- *
299
- * @since 0.5.0-canary.5
300
- */
301
- export function ConsentBannerSave({ onClick, ...props }: ConsentBannerSaveProps): ReactNode {
302
- const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
303
-
304
- if (pending === undefined) {
305
- return null;
306
- }
307
-
308
- return (
309
- <button
310
- data-action="save"
311
- data-slot="consent-action"
312
- {...props}
313
- // the part owns the decision — composed so a consumer onClick still runs
314
- onClick={(event) => {
315
- onClick?.(event);
316
- consent.saveDecision(pending);
317
- setPending(undefined);
318
- }}
319
- type="button"
320
- />
321
- );
322
- }
323
-
324
- /**
325
- * Props for {@link ConsentToggle}.
326
- *
327
- * @since 0.5.0-canary.4
328
- */
329
- export interface ConsentToggleProps extends Omit<ComponentProps<"button">, "children" | "onClick" | "type"> {
330
- /**
331
- * Label shown while the scoped categories are denied — clicking grants them. Defaults
332
- * to "Allow tracking".
333
- */
334
- allowLabel?: string | undefined;
335
- consent: UseConsentResult;
336
- /**
337
- * Label shown while any scoped category is granted — clicking denies them. Defaults to
338
- * CCPA/CPRA "Do Not Sell or Share My Personal Information".
339
- */
340
- denyLabel?: string | undefined;
341
- /**
342
- * Purposes this control flips. Defaults to `["ads"]` — CCPA/CPRA "Do Not Sell or Share"
343
- * mirrors GPC (ads denied, first-party analytics left alone). Pass `["analytics"]` (or
344
- * both) for a broader opt-out. Distinct from `useConsent`'s `requestedCategories` (what the
345
- * prompt asks about / `grantAll` grants).
346
- */
347
- toggledCategories?: ReadonlyArray<ConsentCategory> | undefined;
348
- }
349
-
350
- /**
351
- * Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
352
- * "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
353
- * By default it only flips `ads` (same scope as a GPC signal); pass `toggledCategories`
354
- * to include analytics or both.
355
- *
356
- * @since 0.5.0-canary.4
357
- */
358
- export function ConsentToggle({
359
- allowLabel = "Allow tracking",
360
- consent,
361
- denyLabel = "Do Not Sell or Share My Personal Information",
362
- toggledCategories = ["ads"],
363
- ...props
364
- }: ConsentToggleProps): ReactNode {
365
- const isAnyCategoryGranted = toggledCategories.some((category) => consent.effectiveConsent[category]);
366
-
367
- return (
368
- <button
369
- data-slot="consent-toggle"
370
- {...props}
371
- // the toggle owns its click — flipping the scoped categories is its whole purpose
372
- onClick={() => {
373
- const next = { ...consent.effectiveConsent };
374
-
375
- for (const category of toggledCategories) {
376
- next[category] = !isAnyCategoryGranted;
377
- }
378
-
379
- consent.saveDecision(next);
380
- }}
381
- type="button"
382
- >
383
- {isAnyCategoryGranted ? denyLabel : allowLabel}
384
- </button>
385
- );
386
- }
@@ -1,50 +0,0 @@
1
- import type { ComponentProps, ReactNode } from "react";
2
-
3
- import type { GtagConsentBootstrapOptions } from "#/destinations/google-analytics";
4
- import { buildGtagConsentBootstrapScript } from "#/destinations/google-analytics";
5
-
6
- /**
7
- * Props for {@link GtagConsentBootstrap}.
8
- *
9
- * @since 0.5.0-canary.6
10
- */
11
- export interface GtagConsentBootstrapProps
12
- extends
13
- Omit<ComponentProps<"script">, "children" | "dangerouslySetInnerHTML" | "nonce" | "src" | "type">,
14
- GtagConsentBootstrapOptions {}
15
-
16
- /**
17
- * Framework-agnostic inline `<script>` that runs `buildGtagConsentBootstrapScript` before
18
- * hydration. Pass the same `nonce` here and in `loadGtagScript` for CSP; this component
19
- * only sets the attribute on the host script — the generated source also stamps it onto
20
- * the injected gtag.js tag when `nonce` is provided.
21
- *
22
- * @since 0.5.0-canary.6
23
- */
24
- export function GtagConsentBootstrap({
25
- config,
26
- dataLayerName,
27
- defaultConsent,
28
- debugMode,
29
- gaMeasurementId,
30
- nonce,
31
- ...props
32
- }: GtagConsentBootstrapProps): ReactNode {
33
- const bootstrapOptions: GtagConsentBootstrapOptions = {
34
- config,
35
- defaultConsent,
36
- gaMeasurementId,
37
- ...(dataLayerName === undefined ? {} : { dataLayerName }),
38
- ...(debugMode === undefined ? {} : { debugMode }),
39
- ...(nonce === undefined ? {} : { nonce }),
40
- };
41
-
42
- return (
43
- <script
44
- {...props}
45
- dangerouslySetInnerHTML={{ __html: buildGtagConsentBootstrapScript(bootstrapOptions) }}
46
- {...(nonce === undefined ? {} : { nonce })}
47
- suppressHydrationWarning
48
- />
49
- );
50
- }
@@ -1,123 +0,0 @@
1
- "use client";
2
-
3
- import { useCallback, useMemo, useRef, useSyncExternalStore } from "react";
4
-
5
- import type { ConsentDecision, ConsentMode, ConsentStorage } from "#/core/consent";
6
- import { CONSENT_CATEGORIES, createConsentDecision, readStoredDecision, resolveDefaultConsent } from "#/core/consent";
7
- import type { ConsentConfig } from "#/core/consent-config";
8
-
9
- /**
10
- * Options for `useConsent`.
11
- *
12
- * @since 0.5.0-canary.4
13
- */
14
- export interface UseConsentOptions {
15
- /** Must be the same object the tracker gate and the tag bootstraps receive — one config, every surface. */
16
- config: ConsentConfig;
17
- hasGlobalPrivacyControlSignal?: boolean | undefined;
18
- mode: ConsentMode;
19
- onDecision?: ((decision: ConsentDecision) => void) | undefined;
20
- /** Must be a stable reference (module-level or memoized) — a new object per render resubscribes every render. */
21
- storage: ConsentStorage;
22
- }
23
-
24
- /**
25
- * The live consent state and decision actions returned by `useConsent`.
26
- *
27
- * @since 0.5.0-canary.4
28
- */
29
- export interface UseConsentResult {
30
- /** The stored decision under the current policy version — `undefined` until the visitor makes one. */
31
- decision: ConsentDecision | undefined;
32
- denyAll: () => void;
33
- /** What tags must obey right now — the stored decision, or the region default before one exists. */
34
- effectiveConsent: ConsentDecision;
35
- grantAll: () => void;
36
- /** Effective `analytics` consent — gates this package's own tracker pipeline. */
37
- isAnalyticsAllowed: boolean;
38
- /** True only for opt-in regions with no stored decision yet — drives whether to render the banner. */
39
- isPromptNeeded: boolean;
40
- /** Persist a granular per-category choice, e.g. from a preferences panel. */
41
- saveDecision: (decision: ConsentDecision) => void;
42
- }
43
-
44
- /**
45
- * Bridges `resolveConsentMode`/`resolveDefaultConsent` (core, region-aware) to React via
46
- * `useSyncExternalStore`: the stored record is the single source of truth, hydration is
47
- * safe by construction (the server snapshot is always "no decision yet", matching what
48
- * prerendered HTML could know), and a decision made in another tab syncs through the
49
- * storage subscription.
50
- *
51
- * @since 0.5.0-canary.4
52
- */
53
- export function useConsent(options: UseConsentOptions): UseConsentResult {
54
- const { mode, onDecision, storage } = options;
55
- const { policyVersion, requestedCategories } = options.config;
56
- const hasGlobalPrivacyControlSignal = options.hasGlobalPrivacyControlSignal ?? false;
57
-
58
- // useSyncExternalStore needs a referentially stable snapshot, but JSON-backed storages
59
- // parse a fresh record per load — so the last valid decision is cached by value.
60
- const cachedDecision = useRef<ConsentDecision | undefined>(undefined);
61
-
62
- const decision = useSyncExternalStore(
63
- storage.subscribe,
64
- (): ConsentDecision | undefined => {
65
- // Only a well-formed decision under the current policy version counts — the store
66
- // is tamperable plain JSON, and a garbage value must re-prompt, not silently deny.
67
- const stored = readStoredDecision(storage, policyVersion);
68
-
69
- if (stored === undefined) {
70
- cachedDecision.current = undefined;
71
-
72
- return undefined;
73
- }
74
-
75
- const cached = cachedDecision.current;
76
-
77
- if (cached !== undefined && CONSENT_CATEGORIES.every((category) => cached[category] === stored[category])) {
78
- return cached;
79
- }
80
-
81
- cachedDecision.current = stored;
82
-
83
- return cachedDecision.current;
84
- },
85
- () => undefined,
86
- );
87
-
88
- // Kept stable across renders (when their own inputs don't change) so a consumer that
89
- // passes this hook's result down as a prop/effect-dep doesn't get a fresh identity —
90
- // and therefore an unnecessary re-render/effect-rerun — every render.
91
- const saveDecision = useCallback(
92
- (next: ConsentDecision): void => {
93
- // No local state — the save notifies the subscription, which re-renders with the new snapshot.
94
- storage.save({ decision: next, policyVersion, timestamp: Date.now() });
95
- onDecision?.(next);
96
- },
97
- [onDecision, policyVersion, storage],
98
- );
99
-
100
- const denyAll = useCallback(() => {
101
- saveDecision(createConsentDecision([]));
102
- }, [saveDecision]);
103
-
104
- const grantAll = useCallback(() => {
105
- saveDecision(createConsentDecision(requestedCategories));
106
- }, [requestedCategories, saveDecision]);
107
-
108
- const effectiveConsent =
109
- decision ?? resolveDefaultConsent({ hasGlobalPrivacyControlSignal, mode, requestedCategories });
110
-
111
- return useMemo(
112
- () => ({
113
- decision,
114
- denyAll,
115
- effectiveConsent,
116
- grantAll,
117
- isAnalyticsAllowed: effectiveConsent.analytics,
118
- isPromptNeeded: mode === "opt-in" && decision === undefined,
119
- saveDecision,
120
- }),
121
- [decision, denyAll, effectiveConsent, grantAll, mode, saveDecision],
122
- );
123
- }
@@ -1,40 +0,0 @@
1
- "use client";
2
-
3
- import { useEffect } from "react";
4
-
5
- import { updateGoogleConsent } from "#/destinations/google-analytics";
6
- import type { UseConsentResult } from "#/react/use-consent";
7
-
8
- /**
9
- * Options for `useGoogleConsentSync`.
10
- *
11
- * @since 0.5.0-canary.6
12
- */
13
- export interface UseGoogleConsentSyncOptions {
14
- /**
15
- * Idempotent gtag.js load — advanced Consent Mode already injects the tag from a
16
- * bootstrap; pass a loader as a safety net after a runtime grant.
17
- */
18
- loadGtagScript?: (() => void) | undefined;
19
- }
20
-
21
- /**
22
- * Keeps Google Consent Mode v2 in sync with `useConsent`: emits `update` when a stored
23
- * decision exists (including cross-tab / privacy-page saves), and optionally loads gtag
24
- * when analytics is effective. Mount once on a page-wide surface (e.g. the footer gate).
25
- *
26
- * @since 0.5.0-canary.6
27
- */
28
- export function useGoogleConsentSync(consent: UseConsentResult, options: UseGoogleConsentSyncOptions = {}): void {
29
- const { loadGtagScript } = options;
30
-
31
- useEffect(() => {
32
- if (consent.decision !== undefined) {
33
- updateGoogleConsent(consent.decision);
34
- }
35
-
36
- if (consent.effectiveConsent.analytics) {
37
- loadGtagScript?.();
38
- }
39
- }, [consent.decision, consent.effectiveConsent.analytics, loadGtagScript]);
40
- }
@@ -1,22 +0,0 @@
1
- "use client";
2
-
3
- import { useEffect, useSyncExternalStore } from "react";
4
-
5
- import type { InitialConsentSnapshot, InitialConsentStore } from "#/client/initial-consent-store";
6
-
7
- /**
8
- * Reactive view of a `createInitialConsentStore` — kicks off resolution on mount (a no-op
9
- * when the app already did so at router creation) and re-renders once the server lane
10
- * answers. SSR and the first client render read the strictest snapshot, so hydration can
11
- * never mismatch on region.
12
- *
13
- * @since 0.5.0-canary.6
14
- */
15
- export function useInitialConsent(store: InitialConsentStore): InitialConsentSnapshot {
16
- // Post-hydration safety net — idempotent with an earlier, pre-hydration kick.
17
- useEffect(() => {
18
- store.ensureResolved();
19
- }, [store]);
20
-
21
- return useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
22
- }