@codefast/tracking 0.5.0-canary.4 → 1.0.0-canary.6
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.
- package/CHANGELOG.md +171 -0
- package/README.md +123 -96
- package/dist/client/browser-cookie.d.mts +11 -0
- package/dist/client/browser-cookie.mjs +15 -0
- package/dist/client/consent-runtime.d.mts +56 -0
- package/dist/client/consent-runtime.mjs +35 -0
- package/dist/client/consent-storage.d.mts +5 -0
- package/dist/client/consent-storage.mjs +20 -1
- package/dist/client/consent-withdrawal.d.mts +21 -0
- package/dist/client/consent-withdrawal.mjs +17 -0
- package/dist/client/cookie-anonymous-id.d.mts +37 -0
- package/dist/client/cookie-anonymous-id.mjs +46 -0
- package/dist/client/create-client-tracker.d.mts +11 -16
- package/dist/client/create-client-tracker.mjs +27 -53
- package/dist/client/gpc.d.mts +2 -0
- package/dist/client/gpc.mjs +3 -1
- package/dist/client/index.d.mts +7 -5
- package/dist/client/index.mjs +8 -6
- package/dist/client/initial-consent-store.d.mts +66 -0
- package/dist/client/initial-consent-store.mjs +119 -0
- package/dist/client/is-analytics-allowed.d.mts +29 -0
- package/dist/client/is-analytics-allowed.mjs +23 -0
- package/dist/client/server-persisted-anonymous-id.d.mts +33 -0
- package/dist/client/server-persisted-anonymous-id.mjs +50 -0
- package/dist/core/consent-config.d.mts +27 -0
- package/dist/core/consent-config.mjs +11 -0
- package/dist/core/consent.d.mts +99 -4
- package/dist/core/consent.mjs +99 -4
- package/dist/core/cookie.d.mts +17 -0
- package/dist/core/cookie.mjs +23 -0
- package/dist/core/destination.d.mts +10 -8
- package/dist/core/event-catalog.d.mts +16 -13
- package/dist/core/event-catalog.mjs +19 -1
- package/dist/core/event-id.d.mts +1 -2
- package/dist/core/event-id.mjs +1 -2
- package/dist/core/index.d.mts +6 -4
- package/dist/core/index.mjs +5 -3
- package/dist/core/tracked-event.d.mts +21 -8
- package/dist/destinations/google-analytics.d.mts +100 -48
- package/dist/destinations/google-analytics.mjs +89 -85
- package/dist/destinations/google-consent.d.mts +64 -0
- package/dist/destinations/google-consent.mjs +102 -0
- package/dist/destinations/index.d.mts +2 -5
- package/dist/destinations/index.mjs +2 -5
- package/dist/destinations/shared.d.mts +20 -0
- package/dist/destinations/shared.mjs +13 -0
- package/dist/destinations/vercel-analytics.d.mts +19 -4
- package/dist/destinations/vercel-analytics.mjs +11 -18
- package/dist/import-protection.d.mts +19 -0
- package/dist/import-protection.mjs +23 -0
- package/dist/index.d.mts +6 -4
- package/dist/index.mjs +5 -3
- package/dist/react/consent-banner.d.mts +143 -38
- package/dist/react/consent-banner.mjs +192 -92
- package/dist/react/gtag-consent-bootstrap.d.mts +27 -0
- package/dist/react/gtag-consent-bootstrap.mjs +29 -0
- package/dist/react/index.d.mts +5 -2
- package/dist/react/index.mjs +5 -2
- package/dist/react/use-consent.d.mts +9 -13
- package/dist/react/use-consent.mjs +42 -25
- package/dist/react/use-google-consent-sync.d.mts +23 -0
- package/dist/react/use-google-consent-sync.mjs +24 -0
- package/dist/react/use-initial-consent.d.mts +14 -0
- package/dist/react/use-initial-consent.mjs +19 -0
- package/dist/server/anonymous-id-cookie.d.mts +46 -0
- package/dist/server/anonymous-id-cookie.mjs +51 -0
- package/dist/server/index.d.mts +5 -3
- package/dist/server/index.mjs +4 -3
- package/dist/server/initial-consent.d.mts +29 -0
- package/dist/server/initial-consent.mjs +31 -0
- package/dist/server/region.d.mts +21 -4
- package/dist/server/region.mjs +26 -5
- package/dist/tanstack-start/index.d.mts +58 -0
- package/dist/tanstack-start/index.mjs +45 -0
- package/package.json +27 -110
- package/src/client/browser-cookie.ts +16 -0
- package/src/client/consent-runtime.ts +80 -0
- package/src/client/consent-storage.ts +31 -1
- package/src/client/consent-withdrawal.ts +32 -0
- package/src/client/cookie-anonymous-id.ts +93 -0
- package/src/client/create-client-tracker.ts +56 -85
- package/src/client/gpc.ts +4 -2
- package/src/client/index.ts +19 -8
- package/src/client/initial-consent-store.ts +218 -0
- package/src/client/is-analytics-allowed.ts +43 -0
- package/src/client/server-persisted-anonymous-id.ts +99 -0
- package/src/core/consent-config.ts +27 -0
- package/src/core/consent.ts +164 -10
- package/src/core/cookie.ts +29 -0
- package/src/core/destination.ts +10 -8
- package/src/core/event-catalog.ts +28 -15
- package/src/core/event-id.ts +1 -2
- package/src/core/index.ts +19 -3
- package/src/core/tracked-event.ts +22 -7
- package/src/css/consent.css +119 -0
- package/src/destinations/google-analytics.ts +196 -133
- package/src/destinations/google-consent.ts +164 -0
- package/src/destinations/index.ts +14 -15
- package/src/destinations/shared.ts +45 -0
- package/src/destinations/vercel-analytics.ts +23 -24
- package/src/import-protection.ts +20 -0
- package/src/index.ts +46 -8
- package/src/react/consent-banner.tsx +308 -111
- package/src/react/gtag-consent-bootstrap.tsx +48 -0
- package/src/react/index.ts +34 -2
- package/src/react/use-consent.ts +53 -45
- package/src/react/use-google-consent-sync.ts +38 -0
- package/src/react/use-initial-consent.ts +22 -0
- package/src/server/anonymous-id-cookie.ts +76 -0
- package/src/server/index.ts +15 -3
- package/src/server/initial-consent.ts +48 -0
- package/src/server/region.ts +22 -5
- package/src/tanstack-start/index.ts +83 -0
- package/dist/client/lifecycle.d.mts +0 -23
- package/dist/client/lifecycle.mjs +0 -30
- package/dist/client/local-storage.d.mts +0 -13
- package/dist/client/local-storage.mjs +0 -31
- package/dist/client/queue.d.mts +0 -49
- package/dist/client/queue.mjs +0 -71
- package/dist/client/router.d.mts +0 -30
- package/dist/client/router.mjs +0 -17
- package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
- package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
- package/dist/destinations/http-destination.d.mts +0 -20
- package/dist/destinations/http-destination.mjs +0 -25
- package/dist/server/create-server-tracker.d.mts +0 -42
- package/dist/server/create-server-tracker.mjs +0 -67
- package/src/client/lifecycle.ts +0 -50
- package/src/client/local-storage.ts +0 -44
- package/src/client/queue.ts +0 -117
- package/src/client/router.ts +0 -37
- package/src/destinations/ga4-measurement-protocol.ts +0 -166
- package/src/destinations/http-destination.ts +0 -33
- package/src/server/create-server-tracker.ts +0 -128
|
@@ -1,127 +1,299 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ComponentProps, Dispatch, ReactNode, SetStateAction } from "react";
|
|
4
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
3
5
|
|
|
4
6
|
import type { ConsentCategory, ConsentDecision } from "#/core/consent";
|
|
5
|
-
import { CONSENT_CATEGORIES } from "#/core/consent";
|
|
6
7
|
import type { UseConsentResult } from "#/react/use-consent";
|
|
7
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
|
+
* @since 0.5.0-canary.4
|
|
30
|
+
*/
|
|
31
|
+
export interface ConsentBannerProps extends ComponentProps<"section"> {
|
|
32
|
+
consent: UseConsentResult;
|
|
33
|
+
/** Overrides the default `isPromptNeeded` gating — e.g. to reopen the banner as a settings panel. */
|
|
34
|
+
open?: boolean | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
8
37
|
/**
|
|
9
|
-
*
|
|
38
|
+
* Non-blocking opt-in prompt for GDPR/PDPL (VN) regions, as composable parts — the root owns
|
|
39
|
+
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
40
|
+
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
41
|
+
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
42
|
+
* exists or the region defaults to opt-out (`consent.isPromptNeeded`), unless `open` says
|
|
43
|
+
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
44
|
+
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
45
|
+
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
46
|
+
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
47
|
+
* between `prompt` and `preferences` for state-dependent styling.
|
|
10
48
|
*
|
|
11
49
|
* @since 0.5.0-canary.4
|
|
12
50
|
*/
|
|
13
|
-
export
|
|
51
|
+
export function ConsentBanner({ children, consent, open, ...props }: ConsentBannerProps): ReactNode {
|
|
52
|
+
const [pending, setPending] = useState<ConsentDecision | undefined>(undefined);
|
|
53
|
+
|
|
54
|
+
const isVisible = open ?? consent.isPromptNeeded;
|
|
55
|
+
|
|
56
|
+
// Closing must reset the layer — a reopened banner starts at the prompt, not mid-preferences.
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (!isVisible) {
|
|
59
|
+
setPending(undefined);
|
|
60
|
+
}
|
|
61
|
+
}, [isVisible]);
|
|
62
|
+
|
|
63
|
+
if (!isVisible) {
|
|
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
|
+
* @since 0.5.0-canary.5
|
|
83
|
+
*/
|
|
84
|
+
export interface ConsentBannerTitleProps extends ComponentProps<"h2"> {}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @since 0.5.0-canary.5
|
|
88
|
+
*/
|
|
89
|
+
export function ConsentBannerTitle({ children, ...props }: ConsentBannerTitleProps): ReactNode {
|
|
90
|
+
return (
|
|
91
|
+
<h2 data-slot="consent-title" {...props}>
|
|
92
|
+
{children}
|
|
93
|
+
</h2>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @since 0.5.0-canary.5
|
|
99
|
+
*/
|
|
100
|
+
export interface ConsentBannerDescriptionProps extends ComponentProps<"p"> {}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Hosts the message and the privacy-policy link informed consent requires.
|
|
104
|
+
*
|
|
105
|
+
* @since 0.5.0-canary.5
|
|
106
|
+
*/
|
|
107
|
+
export function ConsentBannerDescription(props: ConsentBannerDescriptionProps): ReactNode {
|
|
108
|
+
return <p data-slot="consent-description" {...props} />;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @since 0.5.0-canary.5
|
|
113
|
+
*/
|
|
114
|
+
export interface ConsentBannerActionsProps extends ComponentProps<"div"> {}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @since 0.5.0-canary.5
|
|
118
|
+
*/
|
|
119
|
+
export function ConsentBannerActions(props: ConsentBannerActionsProps): ReactNode {
|
|
120
|
+
return <div data-slot="consent-actions" {...props} />;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @since 0.5.0-canary.5
|
|
125
|
+
*/
|
|
126
|
+
export interface ConsentBannerAcceptProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Grants the categories the app requested (`useConsent`'s `requestedCategories`) — never more.
|
|
130
|
+
*
|
|
131
|
+
* @since 0.5.0-canary.5
|
|
132
|
+
*/
|
|
133
|
+
export function ConsentBannerAccept({ onClick, ...props }: ConsentBannerAcceptProps): ReactNode {
|
|
134
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<button
|
|
138
|
+
data-action="accept"
|
|
139
|
+
data-slot="consent-action"
|
|
140
|
+
{...props}
|
|
141
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
142
|
+
onClick={(event) => {
|
|
143
|
+
onClick?.(event);
|
|
144
|
+
consent.grantAll();
|
|
145
|
+
setPending(undefined);
|
|
146
|
+
}}
|
|
147
|
+
type="button"
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @since 0.5.0-canary.5
|
|
154
|
+
*/
|
|
155
|
+
export interface ConsentBannerRejectProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @since 0.5.0-canary.5
|
|
159
|
+
*/
|
|
160
|
+
export function ConsentBannerReject({ onClick, ...props }: ConsentBannerRejectProps): ReactNode {
|
|
161
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<button
|
|
165
|
+
data-action="reject"
|
|
166
|
+
data-slot="consent-action"
|
|
167
|
+
{...props}
|
|
168
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
169
|
+
onClick={(event) => {
|
|
170
|
+
onClick?.(event);
|
|
171
|
+
consent.denyAll();
|
|
172
|
+
setPending(undefined);
|
|
173
|
+
}}
|
|
174
|
+
type="button"
|
|
175
|
+
/>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @since 0.5.0-canary.5
|
|
181
|
+
*/
|
|
182
|
+
export interface ConsentBannerCustomizeProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Opens the per-category preferences layer, starting from the current effective state so
|
|
186
|
+
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
187
|
+
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
188
|
+
*
|
|
189
|
+
* @since 0.5.0-canary.5
|
|
190
|
+
*/
|
|
191
|
+
export function ConsentBannerCustomize({ onClick, ...props }: ConsentBannerCustomizeProps): ReactNode {
|
|
192
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
|
|
193
|
+
|
|
194
|
+
if (pending !== undefined) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<button
|
|
200
|
+
data-action="customize"
|
|
201
|
+
data-slot="consent-action"
|
|
202
|
+
{...props}
|
|
203
|
+
// the part owns the layer toggle — composed so a consumer onClick still runs
|
|
204
|
+
onClick={(event) => {
|
|
205
|
+
onClick?.(event);
|
|
206
|
+
setPending(consent.effectiveConsent);
|
|
207
|
+
}}
|
|
208
|
+
type="button"
|
|
209
|
+
/>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @since 0.5.0-canary.5
|
|
215
|
+
*/
|
|
216
|
+
export interface ConsentBannerPreferencesProps extends ComponentProps<"div"> {}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
220
|
+
*
|
|
221
|
+
* @since 0.5.0-canary.5
|
|
222
|
+
*/
|
|
223
|
+
export function ConsentBannerPreferences(props: ConsentBannerPreferencesProps): ReactNode {
|
|
224
|
+
const { pending } = useConsentBannerContext("ConsentBannerPreferences");
|
|
225
|
+
|
|
226
|
+
if (pending === undefined) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return <div data-slot="consent-preferences" {...props} />;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* @since 0.5.0-canary.5
|
|
235
|
+
*/
|
|
236
|
+
export interface ConsentBannerCategoryProps extends ComponentProps<"label"> {
|
|
14
237
|
category: ConsentCategory;
|
|
15
|
-
description?: string;
|
|
16
|
-
label: string;
|
|
17
238
|
}
|
|
18
239
|
|
|
19
240
|
/**
|
|
20
|
-
*
|
|
241
|
+
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
242
|
+
*
|
|
243
|
+
* @since 0.5.0-canary.5
|
|
21
244
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
245
|
+
export function ConsentBannerCategory({ category, children, ...props }: ConsentBannerCategoryProps): ReactNode {
|
|
246
|
+
const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
|
|
247
|
+
|
|
248
|
+
if (pending === undefined) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<label data-slot="consent-category" {...props}>
|
|
254
|
+
<input
|
|
255
|
+
checked={pending[category]}
|
|
256
|
+
data-slot="consent-category-checkbox"
|
|
257
|
+
onChange={(event) => {
|
|
258
|
+
setPending({ ...pending, [category]: event.target.checked });
|
|
259
|
+
}}
|
|
260
|
+
type="checkbox"
|
|
261
|
+
/>
|
|
262
|
+
{children}
|
|
263
|
+
</label>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @since 0.5.0-canary.5
|
|
269
|
+
*/
|
|
270
|
+
export interface ConsentBannerSaveProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
44
274
|
*
|
|
45
|
-
* @since 0.5.0-canary.
|
|
275
|
+
* @since 0.5.0-canary.5
|
|
46
276
|
*/
|
|
47
|
-
export function
|
|
48
|
-
|
|
49
|
-
categories,
|
|
50
|
-
consent,
|
|
51
|
-
customizeLabel = "Customize",
|
|
52
|
-
message = "We use cookies to understand how you use this site.",
|
|
53
|
-
rejectLabel = "Reject",
|
|
54
|
-
saveLabel = "Save preferences",
|
|
55
|
-
...props
|
|
56
|
-
}: ConsentBannerProps): ReactNode {
|
|
57
|
-
// The pending per-category selection — `undefined` while the preferences layer is closed.
|
|
58
|
-
const [pending, setPending] = useState<ConsentDecision | undefined>(undefined);
|
|
277
|
+
export function ConsentBannerSave({ onClick, ...props }: ConsentBannerSaveProps): ReactNode {
|
|
278
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
|
|
59
279
|
|
|
60
|
-
if (
|
|
280
|
+
if (pending === undefined) {
|
|
61
281
|
return null;
|
|
62
282
|
}
|
|
63
283
|
|
|
64
284
|
return (
|
|
65
|
-
<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type="checkbox"
|
|
78
|
-
/>
|
|
79
|
-
<span data-slot="consent-category-label">{option.label}</span>
|
|
80
|
-
{option.description === undefined ? null : (
|
|
81
|
-
<span data-slot="consent-category-description">{option.description}</span>
|
|
82
|
-
)}
|
|
83
|
-
</label>
|
|
84
|
-
))}
|
|
85
|
-
</div>
|
|
86
|
-
) : null}
|
|
87
|
-
<div data-slot="consent-actions">
|
|
88
|
-
{pending === undefined ? (
|
|
89
|
-
<>
|
|
90
|
-
<button data-action="accept" data-slot="consent-action" onClick={consent.grantAll} type="button">
|
|
91
|
-
{acceptLabel}
|
|
92
|
-
</button>
|
|
93
|
-
<button data-action="reject" data-slot="consent-action" onClick={consent.denyAll} type="button">
|
|
94
|
-
{rejectLabel}
|
|
95
|
-
</button>
|
|
96
|
-
{categories !== undefined && categories.length > 0 ? (
|
|
97
|
-
<button
|
|
98
|
-
data-action="customize"
|
|
99
|
-
data-slot="consent-action"
|
|
100
|
-
onClick={() => {
|
|
101
|
-
// Starts from the current effective state — all-denied in opt-in regions,
|
|
102
|
-
// so no box arrives pre-ticked (pre-ticked consent is invalid under GDPR).
|
|
103
|
-
setPending(consent.effectiveConsent);
|
|
104
|
-
}}
|
|
105
|
-
type="button"
|
|
106
|
-
>
|
|
107
|
-
{customizeLabel}
|
|
108
|
-
</button>
|
|
109
|
-
) : null}
|
|
110
|
-
</>
|
|
111
|
-
) : (
|
|
112
|
-
<button
|
|
113
|
-
data-action="save"
|
|
114
|
-
data-slot="consent-action"
|
|
115
|
-
onClick={() => {
|
|
116
|
-
consent.save(pending);
|
|
117
|
-
}}
|
|
118
|
-
type="button"
|
|
119
|
-
>
|
|
120
|
-
{saveLabel}
|
|
121
|
-
</button>
|
|
122
|
-
)}
|
|
123
|
-
</div>
|
|
124
|
-
</section>
|
|
285
|
+
<button
|
|
286
|
+
data-action="save"
|
|
287
|
+
data-slot="consent-action"
|
|
288
|
+
{...props}
|
|
289
|
+
// the part owns the decision — composed so a consumer onClick still runs
|
|
290
|
+
onClick={(event) => {
|
|
291
|
+
onClick?.(event);
|
|
292
|
+
consent.saveDecision(pending);
|
|
293
|
+
setPending(undefined);
|
|
294
|
+
}}
|
|
295
|
+
type="button"
|
|
296
|
+
/>
|
|
125
297
|
);
|
|
126
298
|
}
|
|
127
299
|
|
|
@@ -129,15 +301,31 @@ export function ConsentBanner({
|
|
|
129
301
|
* @since 0.5.0-canary.4
|
|
130
302
|
*/
|
|
131
303
|
export interface ConsentToggleProps extends Omit<ComponentProps<"button">, "children" | "onClick" | "type"> {
|
|
132
|
-
|
|
304
|
+
/**
|
|
305
|
+
* Label shown while the scoped categories are denied — clicking grants them. Defaults
|
|
306
|
+
* to "Allow tracking".
|
|
307
|
+
*/
|
|
308
|
+
allowLabel?: string | undefined;
|
|
133
309
|
consent: UseConsentResult;
|
|
134
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Label shown while any scoped category is granted — clicking denies them. Defaults to
|
|
312
|
+
* CCPA/CPRA "Do Not Sell or Share My Personal Information".
|
|
313
|
+
*/
|
|
314
|
+
denyLabel?: string | undefined;
|
|
315
|
+
/**
|
|
316
|
+
* Purposes this control flips. Defaults to `["ads"]` — CCPA/CPRA "Do Not Sell or Share"
|
|
317
|
+
* mirrors GPC (ads denied, first-party analytics left alone). Pass `["analytics"]` (or
|
|
318
|
+
* both) for a broader opt-out. Distinct from `useConsent`'s `requestedCategories` (what the
|
|
319
|
+
* prompt asks about / `grantAll` grants).
|
|
320
|
+
*/
|
|
321
|
+
toggledCategories?: ReadonlyArray<ConsentCategory> | undefined;
|
|
135
322
|
}
|
|
136
323
|
|
|
137
324
|
/**
|
|
138
325
|
* Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
|
|
139
326
|
* "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
|
|
140
|
-
*
|
|
327
|
+
* By default it only flips `ads` (same scope as a GPC signal); pass `toggledCategories`
|
|
328
|
+
* to include analytics or both.
|
|
141
329
|
*
|
|
142
330
|
* @since 0.5.0-canary.4
|
|
143
331
|
*/
|
|
@@ -145,19 +333,28 @@ export function ConsentToggle({
|
|
|
145
333
|
allowLabel = "Allow tracking",
|
|
146
334
|
consent,
|
|
147
335
|
denyLabel = "Do Not Sell or Share My Personal Information",
|
|
336
|
+
toggledCategories = ["ads"],
|
|
148
337
|
...props
|
|
149
338
|
}: ConsentToggleProps): ReactNode {
|
|
150
|
-
const
|
|
339
|
+
const isAnyCategoryGranted = toggledCategories.some((category) => consent.effectiveConsent[category]);
|
|
151
340
|
|
|
152
341
|
return (
|
|
153
342
|
<button
|
|
154
343
|
data-slot="consent-toggle"
|
|
155
344
|
{...props}
|
|
156
|
-
// the toggle owns its click — flipping the
|
|
157
|
-
onClick={
|
|
345
|
+
// the toggle owns its click — flipping the scoped categories is its whole purpose
|
|
346
|
+
onClick={() => {
|
|
347
|
+
const next = { ...consent.effectiveConsent };
|
|
348
|
+
|
|
349
|
+
for (const category of toggledCategories) {
|
|
350
|
+
next[category] = !isAnyCategoryGranted;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
consent.saveDecision(next);
|
|
354
|
+
}}
|
|
158
355
|
type="button"
|
|
159
356
|
>
|
|
160
|
-
{
|
|
357
|
+
{isAnyCategoryGranted ? denyLabel : allowLabel}
|
|
161
358
|
</button>
|
|
162
359
|
);
|
|
163
360
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
* @since 1.0.0-canary.6
|
|
8
|
+
*/
|
|
9
|
+
export interface GtagConsentBootstrapProps
|
|
10
|
+
extends
|
|
11
|
+
Omit<ComponentProps<"script">, "children" | "dangerouslySetInnerHTML" | "nonce" | "src" | "type">,
|
|
12
|
+
GtagConsentBootstrapOptions {}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Framework-agnostic inline `<script>` that runs `buildGtagConsentBootstrapScript` before
|
|
16
|
+
* hydration. Pass the same `nonce` here and in `loadGtagScript` for CSP; this component
|
|
17
|
+
* only sets the attribute on the host script — the generated source also stamps it onto
|
|
18
|
+
* the injected gtag.js tag when `nonce` is provided.
|
|
19
|
+
*
|
|
20
|
+
* @since 1.0.0-canary.6
|
|
21
|
+
*/
|
|
22
|
+
export function GtagConsentBootstrap({
|
|
23
|
+
config,
|
|
24
|
+
dataLayerName,
|
|
25
|
+
defaultConsent,
|
|
26
|
+
debugMode,
|
|
27
|
+
gaMeasurementId,
|
|
28
|
+
nonce,
|
|
29
|
+
...props
|
|
30
|
+
}: GtagConsentBootstrapProps): ReactNode {
|
|
31
|
+
const bootstrapOptions: GtagConsentBootstrapOptions = {
|
|
32
|
+
config,
|
|
33
|
+
defaultConsent,
|
|
34
|
+
gaMeasurementId,
|
|
35
|
+
...(dataLayerName === undefined ? {} : { dataLayerName }),
|
|
36
|
+
...(debugMode === undefined ? {} : { debugMode }),
|
|
37
|
+
...(nonce === undefined ? {} : { nonce }),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<script
|
|
42
|
+
{...props}
|
|
43
|
+
dangerouslySetInnerHTML={{ __html: buildGtagConsentBootstrapScript(bootstrapOptions) }}
|
|
44
|
+
{...(nonce === undefined ? {} : { nonce })}
|
|
45
|
+
suppressHydrationWarning
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
}
|
package/src/react/index.ts
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
|
|
1
|
+
export type {
|
|
2
|
+
ConsentBannerAcceptProps,
|
|
3
|
+
ConsentBannerActionsProps,
|
|
4
|
+
ConsentBannerCategoryProps,
|
|
5
|
+
ConsentBannerCustomizeProps,
|
|
6
|
+
ConsentBannerDescriptionProps,
|
|
7
|
+
ConsentBannerPreferencesProps,
|
|
8
|
+
ConsentBannerProps,
|
|
9
|
+
ConsentBannerRejectProps,
|
|
10
|
+
ConsentBannerSaveProps,
|
|
11
|
+
ConsentBannerTitleProps,
|
|
12
|
+
ConsentToggleProps,
|
|
13
|
+
} from "#/react/consent-banner";
|
|
14
|
+
export {
|
|
15
|
+
ConsentBanner,
|
|
16
|
+
ConsentBannerAccept,
|
|
17
|
+
ConsentBannerActions,
|
|
18
|
+
ConsentBannerCategory,
|
|
19
|
+
ConsentBannerCustomize,
|
|
20
|
+
ConsentBannerDescription,
|
|
21
|
+
ConsentBannerPreferences,
|
|
22
|
+
ConsentBannerReject,
|
|
23
|
+
ConsentBannerSave,
|
|
24
|
+
ConsentBannerTitle,
|
|
25
|
+
ConsentToggle,
|
|
26
|
+
} from "#/react/consent-banner";
|
|
3
27
|
|
|
4
28
|
export type { UseConsentOptions, UseConsentResult } from "#/react/use-consent";
|
|
5
29
|
export { useConsent } from "#/react/use-consent";
|
|
30
|
+
|
|
31
|
+
export { useInitialConsent } from "#/react/use-initial-consent";
|
|
32
|
+
|
|
33
|
+
export type { UseGoogleConsentSyncOptions } from "#/react/use-google-consent-sync";
|
|
34
|
+
export { useGoogleConsentSync } from "#/react/use-google-consent-sync";
|
|
35
|
+
|
|
36
|
+
export type { GtagConsentBootstrapProps } from "#/react/gtag-consent-bootstrap";
|
|
37
|
+
export { GtagConsentBootstrap } from "#/react/gtag-consent-bootstrap";
|