@codefast/tracking 0.5.0-canary.5 → 0.5.0-canary.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +469 -0
- package/README.md +176 -126
- package/dist/adapters/tanstack-start.d.ts +80 -0
- package/dist/adapters/tanstack-start.d.ts.map +1 -0
- package/dist/adapters/tanstack-start.js +74 -0
- package/dist/adapters/tanstack-start.js.map +1 -0
- package/dist/client/ad-framework-consent.d.ts +62 -0
- package/dist/client/ad-framework-consent.d.ts.map +1 -0
- package/dist/client/ad-framework-consent.js +40 -0
- package/dist/client/ad-framework-consent.js.map +1 -0
- package/dist/client/browser-cookie.d.ts +9 -0
- package/dist/client/browser-cookie.d.ts.map +1 -0
- package/dist/client/browser-cookie.js +15 -0
- package/dist/client/browser-cookie.js.map +1 -0
- package/dist/client/consent-runtime.d.ts +53 -0
- package/dist/client/consent-runtime.d.ts.map +1 -0
- package/dist/client/consent-runtime.js +33 -0
- package/dist/client/consent-runtime.js.map +1 -0
- package/dist/client/{consent-storage.d.mts → consent-storage.d.ts} +8 -6
- package/dist/client/consent-storage.d.ts.map +1 -0
- package/dist/client/consent-storage.js +109 -0
- package/dist/client/consent-storage.js.map +1 -0
- package/dist/client/consent-withdrawal.d.ts +18 -0
- package/dist/client/consent-withdrawal.d.ts.map +1 -0
- package/dist/client/consent-withdrawal.js +17 -0
- package/dist/client/consent-withdrawal.js.map +1 -0
- package/dist/client/cookie-anonymous-id.d.ts +41 -0
- package/dist/client/cookie-anonymous-id.d.ts.map +1 -0
- package/dist/client/cookie-anonymous-id.js +65 -0
- package/dist/client/cookie-anonymous-id.js.map +1 -0
- package/dist/client/create-client-tracker.d.ts +60 -0
- package/dist/client/create-client-tracker.d.ts.map +1 -0
- package/dist/client/create-client-tracker.js +91 -0
- package/dist/client/create-client-tracker.js.map +1 -0
- package/dist/client/gpc.d.ts +10 -0
- package/dist/client/gpc.d.ts.map +1 -0
- package/dist/client/gpc.js +12 -0
- package/dist/client/gpc.js.map +1 -0
- package/dist/client/initial-consent-store.d.ts +63 -0
- package/dist/client/initial-consent-store.d.ts.map +1 -0
- package/dist/client/initial-consent-store.js +138 -0
- package/dist/client/initial-consent-store.js.map +1 -0
- package/dist/client/is-analytics-allowed.d.ts +26 -0
- package/dist/client/is-analytics-allowed.d.ts.map +1 -0
- package/dist/client/is-analytics-allowed.js +22 -0
- package/dist/client/is-analytics-allowed.js.map +1 -0
- package/dist/client/server-persisted-anonymous-id.d.ts +30 -0
- package/dist/client/server-persisted-anonymous-id.d.ts.map +1 -0
- package/dist/client/server-persisted-anonymous-id.js +72 -0
- package/dist/client/server-persisted-anonymous-id.js.map +1 -0
- package/dist/core/consent-config.d.ts +24 -0
- package/dist/core/consent-config.d.ts.map +1 -0
- package/dist/core/consent-config.js +9 -0
- package/dist/core/consent-config.js.map +1 -0
- package/dist/core/consent-receipt.d.ts +71 -0
- package/dist/core/consent-receipt.d.ts.map +1 -0
- package/dist/core/consent-receipt.js +39 -0
- package/dist/core/consent-receipt.js.map +1 -0
- package/dist/core/consent.d.ts +182 -0
- package/dist/core/consent.d.ts.map +1 -0
- package/dist/core/consent.js +159 -0
- package/dist/core/consent.js.map +1 -0
- package/dist/core/cookie.d.ts +15 -0
- package/dist/core/cookie.d.ts.map +1 -0
- package/dist/core/cookie.js +26 -0
- package/dist/core/cookie.js.map +1 -0
- package/dist/core/destination.d.ts +29 -0
- package/dist/core/destination.d.ts.map +1 -0
- package/dist/core/destination.js +2 -0
- package/dist/core/destination.js.map +1 -0
- package/dist/core/event-catalog.d.ts +35 -0
- package/dist/core/event-catalog.d.ts.map +1 -0
- package/dist/core/event-catalog.js +30 -0
- package/dist/core/event-catalog.js.map +1 -0
- package/dist/core/event-id.d.ts +7 -0
- package/dist/core/event-id.d.ts.map +1 -0
- package/dist/core/event-id.js +9 -0
- package/dist/core/event-id.js.map +1 -0
- package/dist/core/tracked-event.d.ts +15 -0
- package/dist/core/tracked-event.d.ts.map +1 -0
- package/dist/core/tracked-event.js +2 -0
- package/dist/core/tracked-event.js.map +1 -0
- package/dist/destinations/ad-consent.d.ts +23 -0
- package/dist/destinations/ad-consent.d.ts.map +1 -0
- package/dist/destinations/ad-consent.js +12 -0
- package/dist/destinations/ad-consent.js.map +1 -0
- package/dist/destinations/google-analytics.d.ts +148 -0
- package/dist/destinations/google-analytics.d.ts.map +1 -0
- package/dist/destinations/google-analytics.js +156 -0
- package/dist/destinations/google-analytics.js.map +1 -0
- package/dist/destinations/google-consent.d.ts +62 -0
- package/dist/destinations/google-consent.d.ts.map +1 -0
- package/dist/destinations/google-consent.js +109 -0
- package/dist/destinations/google-consent.js.map +1 -0
- package/dist/destinations/meta.d.ts +63 -0
- package/dist/destinations/meta.d.ts.map +1 -0
- package/dist/destinations/meta.js +44 -0
- package/dist/destinations/meta.js.map +1 -0
- package/dist/destinations/microsoft-uet.d.ts +56 -0
- package/dist/destinations/microsoft-uet.d.ts.map +1 -0
- package/dist/destinations/microsoft-uet.js +41 -0
- package/dist/destinations/microsoft-uet.js.map +1 -0
- package/dist/destinations/shared.d.ts +18 -0
- package/dist/destinations/shared.d.ts.map +1 -0
- package/dist/destinations/shared.js +19 -0
- package/dist/destinations/shared.js.map +1 -0
- package/dist/destinations/tiktok.d.ts +55 -0
- package/dist/destinations/tiktok.d.ts.map +1 -0
- package/dist/destinations/tiktok.js +40 -0
- package/dist/destinations/tiktok.js.map +1 -0
- package/dist/destinations/vercel-analytics.d.ts +25 -0
- package/dist/destinations/vercel-analytics.d.ts.map +1 -0
- package/dist/destinations/vercel-analytics.js +23 -0
- package/dist/destinations/vercel-analytics.js.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/react/consent-banner.d.ts +155 -0
- package/dist/react/consent-banner.d.ts.map +1 -0
- package/dist/react/consent-banner.js +171 -0
- package/dist/react/consent-banner.js.map +1 -0
- package/dist/react/gtag-consent-bootstrap.d.ts +17 -0
- package/dist/react/gtag-consent-bootstrap.d.ts.map +1 -0
- package/dist/react/gtag-consent-bootstrap.js +22 -0
- package/dist/react/gtag-consent-bootstrap.js.map +1 -0
- package/dist/react/use-consent.d.ts +42 -0
- package/dist/react/use-consent.d.ts.map +1 -0
- package/dist/react/use-consent.js +60 -0
- package/dist/react/use-consent.js.map +1 -0
- package/dist/react/use-google-consent-sync.d.ts +20 -0
- package/dist/react/use-google-consent-sync.d.ts.map +1 -0
- package/dist/react/use-google-consent-sync.js +22 -0
- package/dist/react/use-google-consent-sync.js.map +1 -0
- package/dist/react/use-initial-consent.d.ts +11 -0
- package/dist/react/use-initial-consent.d.ts.map +1 -0
- package/dist/react/use-initial-consent.js +18 -0
- package/dist/react/use-initial-consent.js.map +1 -0
- package/dist/server/anonymous-id-cookie.d.ts +67 -0
- package/dist/server/anonymous-id-cookie.d.ts.map +1 -0
- package/dist/server/anonymous-id-cookie.js +71 -0
- package/dist/server/anonymous-id-cookie.js.map +1 -0
- package/dist/server/consent-receipt-store.d.ts +56 -0
- package/dist/server/consent-receipt-store.d.ts.map +1 -0
- package/dist/server/consent-receipt-store.js +44 -0
- package/dist/server/consent-receipt-store.js.map +1 -0
- package/dist/server/consent-receipt.d.ts +41 -0
- package/dist/server/consent-receipt.d.ts.map +1 -0
- package/dist/server/consent-receipt.js +60 -0
- package/dist/server/consent-receipt.js.map +1 -0
- package/dist/server/initial-consent.d.ts +26 -0
- package/dist/server/initial-consent.d.ts.map +1 -0
- package/dist/server/initial-consent.js +31 -0
- package/dist/server/initial-consent.js.map +1 -0
- package/dist/server/measurement-protocol.d.ts +69 -0
- package/dist/server/measurement-protocol.d.ts.map +1 -0
- package/dist/server/measurement-protocol.js +61 -0
- package/dist/server/measurement-protocol.js.map +1 -0
- package/dist/server/region.d.ts +27 -0
- package/dist/server/region.d.ts.map +1 -0
- package/dist/server/region.js +69 -0
- package/dist/server/region.js.map +1 -0
- package/dist/server/user-deletion.d.ts +64 -0
- package/dist/server/user-deletion.d.ts.map +1 -0
- package/dist/server/user-deletion.js +54 -0
- package/dist/server/user-deletion.js.map +1 -0
- package/dist/tooling/import-protection.d.ts +19 -0
- package/dist/tooling/import-protection.d.ts.map +1 -0
- package/dist/tooling/import-protection.js +23 -0
- package/dist/tooling/import-protection.js.map +1 -0
- package/package.json +205 -111
- package/src/adapters/tanstack-start.ts +135 -0
- package/src/client/ad-framework-consent.ts +86 -0
- 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 +112 -0
- package/src/client/create-client-tracker.ts +128 -86
- package/src/client/gpc.ts +4 -2
- 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 +102 -0
- package/src/core/consent-config.ts +27 -0
- package/src/core/consent-receipt.ts +115 -0
- package/src/core/consent.ts +164 -10
- package/src/core/cookie.ts +29 -0
- package/src/core/destination.ts +17 -8
- package/src/core/event-catalog.ts +29 -15
- package/src/core/event-id.ts +1 -2
- package/src/core/tracked-event.ts +4 -4
- package/src/destinations/ad-consent.ts +29 -0
- package/src/destinations/google-analytics.ts +196 -133
- package/src/destinations/google-consent.ts +164 -0
- package/src/destinations/meta.ts +96 -0
- package/src/destinations/microsoft-uet.ts +86 -0
- package/src/destinations/shared.ts +45 -0
- package/src/destinations/tiktok.ts +85 -0
- package/src/destinations/vercel-analytics.ts +23 -24
- package/src/index.ts +165 -8
- package/src/react/consent-banner.tsx +40 -14
- package/src/react/gtag-consent-bootstrap.tsx +48 -0
- 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 +103 -0
- package/src/server/consent-receipt-store.ts +79 -0
- package/src/server/consent-receipt.ts +89 -0
- package/src/server/initial-consent.ts +46 -0
- package/src/server/measurement-protocol.ts +114 -0
- package/src/server/region.ts +22 -15
- package/src/server/user-deletion.ts +93 -0
- package/src/tooling/import-protection.ts +22 -0
- package/dist/client/consent-storage.mjs +0 -63
- package/dist/client/create-client-tracker.d.mts +0 -37
- package/dist/client/create-client-tracker.mjs +0 -66
- package/dist/client/gpc.d.mts +0 -10
- package/dist/client/gpc.mjs +0 -12
- package/dist/client/index.d.mts +0 -8
- package/dist/client/index.mjs +0 -8
- 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/core/consent.d.mts +0 -89
- package/dist/core/consent.mjs +0 -57
- package/dist/core/destination.d.mts +0 -23
- package/dist/core/destination.mjs +0 -1
- package/dist/core/event-catalog.d.mts +0 -36
- package/dist/core/event-catalog.mjs +0 -12
- package/dist/core/event-id.d.mts +0 -10
- package/dist/core/event-id.mjs +0 -12
- package/dist/core/index.d.mts +0 -6
- package/dist/core/index.mjs +0 -4
- package/dist/core/tracked-event.d.mts +0 -17
- package/dist/core/tracked-event.mjs +0 -1
- package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
- package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
- package/dist/destinations/google-analytics.d.mts +0 -98
- package/dist/destinations/google-analytics.mjs +0 -126
- package/dist/destinations/http-destination.d.mts +0 -20
- package/dist/destinations/http-destination.mjs +0 -25
- package/dist/destinations/index.d.mts +0 -5
- package/dist/destinations/index.mjs +0 -5
- package/dist/destinations/vercel-analytics.d.mts +0 -13
- package/dist/destinations/vercel-analytics.mjs +0 -30
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -4
- package/dist/react/consent-banner.d.mts +0 -163
- package/dist/react/consent-banner.mjs +0 -209
- package/dist/react/index.d.mts +0 -3
- package/dist/react/index.mjs +0 -3
- package/dist/react/use-consent.d.mts +0 -49
- package/dist/react/use-consent.mjs +0 -54
- package/dist/server/create-server-tracker.d.mts +0 -42
- package/dist/server/create-server-tracker.mjs +0 -67
- package/dist/server/index.d.mts +0 -3
- package/dist/server/index.mjs +0 -3
- package/dist/server/region.d.mts +0 -20
- package/dist/server/region.mjs +0 -56
- package/src/client/index.ts +0 -17
- 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/core/index.ts +0 -24
- package/src/destinations/ga4-measurement-protocol.ts +0 -166
- package/src/destinations/http-destination.ts +0 -33
- package/src/destinations/index.ts +0 -20
- package/src/react/index.ts +0 -29
- package/src/server/create-server-tracker.ts +0 -128
- package/src/server/index.ts +0 -4
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
4
|
+
const ConsentBannerContext = createContext(null);
|
|
5
|
+
function useConsentBannerContext(part) {
|
|
6
|
+
const context = useContext(ConsentBannerContext);
|
|
7
|
+
if (context === null) {
|
|
8
|
+
throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
|
|
9
|
+
}
|
|
10
|
+
return context;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Non-blocking opt-in prompt for GDPR/PDPL (VN) regions, as composable parts — the root owns
|
|
14
|
+
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
15
|
+
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
16
|
+
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
17
|
+
* exists or the region defaults to opt-out (`consent.isPromptNeeded`), unless `open` says
|
|
18
|
+
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
19
|
+
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
20
|
+
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
21
|
+
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
22
|
+
* between `prompt` and `preferences` for state-dependent styling.
|
|
23
|
+
*
|
|
24
|
+
* @since 0.5.0-canary.4
|
|
25
|
+
*/
|
|
26
|
+
export function ConsentBanner({ children, consent, open, ...props }) {
|
|
27
|
+
const [pending, setPending] = useState(undefined);
|
|
28
|
+
const isVisible = open ?? consent.isPromptNeeded;
|
|
29
|
+
// Closing must reset the layer — a reopened banner starts at the prompt, not mid-preferences.
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!isVisible) {
|
|
32
|
+
setPending(undefined);
|
|
33
|
+
}
|
|
34
|
+
}, [isVisible]);
|
|
35
|
+
if (!isVisible) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return (_jsx(ConsentBannerContext.Provider, { value: { consent, pending, setPending }, children: _jsx("section", { "aria-label": "Cookie consent", "data-slot": "consent-banner", "data-state": pending === undefined ? "prompt" : "preferences", ...props, children: children }) }));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @since 0.5.0-canary.5
|
|
42
|
+
*/
|
|
43
|
+
export function ConsentBannerTitle({ children, ...props }) {
|
|
44
|
+
return (_jsx("h2", { "data-slot": "consent-title", ...props, children: children }));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Hosts the message and the privacy-policy link informed consent requires.
|
|
48
|
+
*
|
|
49
|
+
* @since 0.5.0-canary.5
|
|
50
|
+
*/
|
|
51
|
+
export function ConsentBannerDescription(props) {
|
|
52
|
+
return _jsx("p", { "data-slot": "consent-description", ...props });
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @since 0.5.0-canary.5
|
|
56
|
+
*/
|
|
57
|
+
export function ConsentBannerActions(props) {
|
|
58
|
+
return _jsx("div", { "data-slot": "consent-actions", ...props });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Grants the categories the app requested (`useConsent`'s `requestedCategories`) — never more.
|
|
62
|
+
*
|
|
63
|
+
* @since 0.5.0-canary.5
|
|
64
|
+
*/
|
|
65
|
+
export function ConsentBannerAccept({ onClick, ...props }) {
|
|
66
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
|
|
67
|
+
return (_jsx("button", { "data-action": "accept", "data-slot": "consent-action", ...props,
|
|
68
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
69
|
+
onClick: (event) => {
|
|
70
|
+
onClick?.(event);
|
|
71
|
+
consent.grantAll();
|
|
72
|
+
setPending(undefined);
|
|
73
|
+
}, type: "button" }));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @since 0.5.0-canary.5
|
|
77
|
+
*/
|
|
78
|
+
export function ConsentBannerReject({ onClick, ...props }) {
|
|
79
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
|
|
80
|
+
return (_jsx("button", { "data-action": "reject", "data-slot": "consent-action", ...props,
|
|
81
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
82
|
+
onClick: (event) => {
|
|
83
|
+
onClick?.(event);
|
|
84
|
+
consent.denyAll();
|
|
85
|
+
setPending(undefined);
|
|
86
|
+
}, type: "button" }));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Opens the per-category preferences layer, starting from the current effective state so
|
|
90
|
+
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
91
|
+
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
92
|
+
*
|
|
93
|
+
* @since 0.5.0-canary.5
|
|
94
|
+
*/
|
|
95
|
+
export function ConsentBannerCustomize({ onClick, ...props }) {
|
|
96
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
|
|
97
|
+
if (pending !== undefined) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return (_jsx("button", { "data-action": "customize", "data-slot": "consent-action", ...props,
|
|
101
|
+
// the part owns the layer toggle — composed so a consumer onClick still runs
|
|
102
|
+
onClick: (event) => {
|
|
103
|
+
onClick?.(event);
|
|
104
|
+
setPending(consent.effectiveConsent);
|
|
105
|
+
}, type: "button" }));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
109
|
+
*
|
|
110
|
+
* @since 0.5.0-canary.5
|
|
111
|
+
*/
|
|
112
|
+
export function ConsentBannerPreferences(props) {
|
|
113
|
+
const { pending } = useConsentBannerContext("ConsentBannerPreferences");
|
|
114
|
+
if (pending === undefined) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return _jsx("div", { "data-slot": "consent-preferences", ...props });
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
121
|
+
*
|
|
122
|
+
* @since 0.5.0-canary.5
|
|
123
|
+
*/
|
|
124
|
+
export function ConsentBannerCategory({ category, children, ...props }) {
|
|
125
|
+
const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
|
|
126
|
+
if (pending === undefined) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return (_jsxs("label", { "data-slot": "consent-category", ...props, children: [_jsx("input", { checked: pending[category], "data-slot": "consent-category-checkbox", onChange: (event) => {
|
|
130
|
+
setPending({ ...pending, [category]: event.target.checked });
|
|
131
|
+
}, type: "checkbox" }), children] }));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
135
|
+
*
|
|
136
|
+
* @since 0.5.0-canary.5
|
|
137
|
+
*/
|
|
138
|
+
export function ConsentBannerSave({ onClick, ...props }) {
|
|
139
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
|
|
140
|
+
if (pending === undefined) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return (_jsx("button", { "data-action": "save", "data-slot": "consent-action", ...props,
|
|
144
|
+
// the part owns the decision — composed so a consumer onClick still runs
|
|
145
|
+
onClick: (event) => {
|
|
146
|
+
onClick?.(event);
|
|
147
|
+
consent.saveDecision(pending);
|
|
148
|
+
setPending(undefined);
|
|
149
|
+
}, type: "button" }));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
|
|
153
|
+
* "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
|
|
154
|
+
* By default it only flips `ads` (same scope as a GPC signal); pass `toggledCategories`
|
|
155
|
+
* to include analytics or both.
|
|
156
|
+
*
|
|
157
|
+
* @since 0.5.0-canary.4
|
|
158
|
+
*/
|
|
159
|
+
export function ConsentToggle({ allowLabel = "Allow tracking", consent, denyLabel = "Do Not Sell or Share My Personal Information", toggledCategories = ["ads"], ...props }) {
|
|
160
|
+
const isAnyCategoryGranted = toggledCategories.some((category) => consent.effectiveConsent[category]);
|
|
161
|
+
return (_jsx("button", { "data-slot": "consent-toggle", ...props,
|
|
162
|
+
// the toggle owns its click — flipping the scoped categories is its whole purpose
|
|
163
|
+
onClick: () => {
|
|
164
|
+
const next = { ...consent.effectiveConsent };
|
|
165
|
+
for (const category of toggledCategories) {
|
|
166
|
+
next[category] = !isAnyCategoryGranted;
|
|
167
|
+
}
|
|
168
|
+
consent.saveDecision(next);
|
|
169
|
+
}, type: "button", children: isAnyCategoryGranted ? denyLabel : allowLabel }));
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=consent-banner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consent-banner.js","sourceRoot":"","sources":["../../src/react/consent-banner.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAGb,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,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C;gBACzC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,oBAAoB,CAAC;YACzC,CAAC;YAED,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC,EACD,IAAI,EAAC,QAAQ,YAEZ,oBAAoB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,GACvC,CACV,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
2
|
+
import type { GtagConsentBootstrapOptions } from "#/destinations/google-analytics";
|
|
3
|
+
/**
|
|
4
|
+
* @since 1.0.0-canary.6
|
|
5
|
+
*/
|
|
6
|
+
export interface GtagConsentBootstrapProps extends Omit<ComponentProps<"script">, "children" | "dangerouslySetInnerHTML" | "nonce" | "src" | "type">, GtagConsentBootstrapOptions {
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Framework-agnostic inline `<script>` that runs `buildGtagConsentBootstrapScript` before
|
|
10
|
+
* hydration. Pass the same `nonce` here and in `loadGtagScript` for CSP; this component
|
|
11
|
+
* only sets the attribute on the host script — the generated source also stamps it onto
|
|
12
|
+
* the injected gtag.js tag when `nonce` is provided.
|
|
13
|
+
*
|
|
14
|
+
* @since 1.0.0-canary.6
|
|
15
|
+
*/
|
|
16
|
+
export declare function GtagConsentBootstrap({ config, dataLayerName, defaultConsent, debugMode, gaMeasurementId, nonce, ...props }: GtagConsentBootstrapProps): ReactNode;
|
|
17
|
+
//# sourceMappingURL=gtag-consent-bootstrap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtag-consent-bootstrap.d.ts","sourceRoot":"","sources":["../../src/react/gtag-consent-bootstrap.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AAGnF;;GAEG;AACH,MAAM,WAAW,yBACf,SACE,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE,UAAU,GAAG,yBAAyB,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC,EACjG,2BAA2B;CAAG;AAElC;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,MAAM,EACN,aAAa,EACb,cAAc,EACd,SAAS,EACT,eAAe,EACf,KAAK,EACL,GAAG,KAAK,EACT,EAAE,yBAAyB,GAAG,SAAS,CAkBvC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { buildGtagConsentBootstrapScript } from "#/destinations/google-analytics";
|
|
3
|
+
/**
|
|
4
|
+
* Framework-agnostic inline `<script>` that runs `buildGtagConsentBootstrapScript` before
|
|
5
|
+
* hydration. Pass the same `nonce` here and in `loadGtagScript` for CSP; this component
|
|
6
|
+
* only sets the attribute on the host script — the generated source also stamps it onto
|
|
7
|
+
* the injected gtag.js tag when `nonce` is provided.
|
|
8
|
+
*
|
|
9
|
+
* @since 1.0.0-canary.6
|
|
10
|
+
*/
|
|
11
|
+
export function GtagConsentBootstrap({ config, dataLayerName, defaultConsent, debugMode, gaMeasurementId, nonce, ...props }) {
|
|
12
|
+
const bootstrapOptions = {
|
|
13
|
+
config,
|
|
14
|
+
defaultConsent,
|
|
15
|
+
gaMeasurementId,
|
|
16
|
+
...(dataLayerName === undefined ? {} : { dataLayerName }),
|
|
17
|
+
...(debugMode === undefined ? {} : { debugMode }),
|
|
18
|
+
...(nonce === undefined ? {} : { nonce }),
|
|
19
|
+
};
|
|
20
|
+
return (_jsx("script", { ...props, dangerouslySetInnerHTML: { __html: buildGtagConsentBootstrapScript(bootstrapOptions) }, ...(nonce === undefined ? {} : { nonce }), suppressHydrationWarning: true }));
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=gtag-consent-bootstrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtag-consent-bootstrap.js","sourceRoot":"","sources":["../../src/react/gtag-consent-bootstrap.tsx"],"names":[],"mappings":";AAGA,OAAO,EAAE,+BAA+B,EAAE,MAAM,iCAAiC,CAAC;AAUlF;;;;;;;GAOG;AACH,MAAM,UAAU,oBAAoB,CAAC,EACnC,MAAM,EACN,aAAa,EACb,cAAc,EACd,SAAS,EACT,eAAe,EACf,KAAK,EACL,GAAG,KAAK,EACkB;IAC1B,MAAM,gBAAgB,GAAgC;QACpD,MAAM;QACN,cAAc;QACd,eAAe;QACf,GAAG,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC;QACzD,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC;QACjD,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KAC1C,CAAC;IAEF,OAAO,CACL,oBACM,KAAK,EACT,uBAAuB,EAAE,EAAE,MAAM,EAAE,+BAA+B,CAAC,gBAAgB,CAAC,EAAE,KAClF,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAC1C,wBAAwB,SACxB,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ConsentDecision, ConsentMode, ConsentStorage } from "#/core/consent";
|
|
2
|
+
import type { ConsentConfig } from "#/core/consent-config";
|
|
3
|
+
/**
|
|
4
|
+
* @since 0.5.0-canary.4
|
|
5
|
+
*/
|
|
6
|
+
export interface UseConsentOptions {
|
|
7
|
+
/** Must be the same object the tracker gate and the tag bootstraps receive — one config, every surface. */
|
|
8
|
+
config: ConsentConfig;
|
|
9
|
+
hasGlobalPrivacyControlSignal?: boolean | undefined;
|
|
10
|
+
mode: ConsentMode;
|
|
11
|
+
onDecision?: ((decision: ConsentDecision) => void) | undefined;
|
|
12
|
+
/** Must be a stable reference (module-level or memoized) — a new object per render resubscribes every render. */
|
|
13
|
+
storage: ConsentStorage;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @since 0.5.0-canary.4
|
|
17
|
+
*/
|
|
18
|
+
export interface UseConsentResult {
|
|
19
|
+
/** The stored decision under the current policy version — `undefined` until the visitor makes one. */
|
|
20
|
+
decision: ConsentDecision | undefined;
|
|
21
|
+
denyAll: () => void;
|
|
22
|
+
/** What tags must obey right now — the stored decision, or the region default before one exists. */
|
|
23
|
+
effectiveConsent: ConsentDecision;
|
|
24
|
+
grantAll: () => void;
|
|
25
|
+
/** Effective `analytics` consent — gates this package's own tracker pipeline. */
|
|
26
|
+
isAnalyticsAllowed: boolean;
|
|
27
|
+
/** True only for opt-in regions with no stored decision yet — drives whether to render the banner. */
|
|
28
|
+
isPromptNeeded: boolean;
|
|
29
|
+
/** Persist a granular per-category choice, e.g. from a preferences panel. */
|
|
30
|
+
saveDecision: (decision: ConsentDecision) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Bridges `resolveConsentMode`/`resolveDefaultConsent` (core, region-aware) to React via
|
|
34
|
+
* `useSyncExternalStore`: the stored record is the single source of truth, hydration is
|
|
35
|
+
* safe by construction (the server snapshot is always "no decision yet", matching what
|
|
36
|
+
* prerendered HTML could know), and a decision made in another tab syncs through the
|
|
37
|
+
* storage subscription.
|
|
38
|
+
*
|
|
39
|
+
* @since 0.5.0-canary.4
|
|
40
|
+
*/
|
|
41
|
+
export declare function useConsent(options: UseConsentOptions): UseConsentResult;
|
|
42
|
+
//# sourceMappingURL=use-consent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-consent.d.ts","sourceRoot":"","sources":["../../src/react/use-consent.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEnF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2GAA2G;IAC3G,MAAM,EAAE,aAAa,CAAC;IACtB,6BAA6B,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACpD,IAAI,EAAE,WAAW,CAAC;IAClB,UAAU,CAAC,EAAE,CAAC,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;IAC/D,iHAAiH;IACjH,OAAO,EAAE,cAAc,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sGAAsG;IACtG,QAAQ,EAAE,eAAe,GAAG,SAAS,CAAC;IACtC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,oGAAoG;IACpG,gBAAgB,EAAE,eAAe,CAAC;IAClC,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,iFAAiF;IACjF,kBAAkB,EAAE,OAAO,CAAC;IAC5B,sGAAsG;IACtG,cAAc,EAAE,OAAO,CAAC;IACxB,6EAA6E;IAC7E,YAAY,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,CAAC;CACnD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,gBAAgB,CAsEvE"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useMemo, useRef, useSyncExternalStore } from "react";
|
|
3
|
+
import { CONSENT_CATEGORIES, createConsentDecision, readStoredDecision, resolveDefaultConsent } from "#/core/consent";
|
|
4
|
+
/**
|
|
5
|
+
* Bridges `resolveConsentMode`/`resolveDefaultConsent` (core, region-aware) to React via
|
|
6
|
+
* `useSyncExternalStore`: the stored record is the single source of truth, hydration is
|
|
7
|
+
* safe by construction (the server snapshot is always "no decision yet", matching what
|
|
8
|
+
* prerendered HTML could know), and a decision made in another tab syncs through the
|
|
9
|
+
* storage subscription.
|
|
10
|
+
*
|
|
11
|
+
* @since 0.5.0-canary.4
|
|
12
|
+
*/
|
|
13
|
+
export function useConsent(options) {
|
|
14
|
+
const { mode, onDecision, storage } = options;
|
|
15
|
+
const { policyVersion, requestedCategories } = options.config;
|
|
16
|
+
const hasGlobalPrivacyControlSignal = options.hasGlobalPrivacyControlSignal ?? false;
|
|
17
|
+
// useSyncExternalStore needs a referentially stable snapshot, but JSON-backed storages
|
|
18
|
+
// parse a fresh record per load — so the last valid decision is cached by value.
|
|
19
|
+
const cachedDecision = useRef(undefined);
|
|
20
|
+
const decision = useSyncExternalStore(storage.subscribe, () => {
|
|
21
|
+
// Only a well-formed decision under the current policy version counts — the store
|
|
22
|
+
// is tamperable plain JSON, and a garbage value must re-prompt, not silently deny.
|
|
23
|
+
const stored = readStoredDecision(storage, policyVersion);
|
|
24
|
+
if (stored === undefined) {
|
|
25
|
+
cachedDecision.current = undefined;
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const cached = cachedDecision.current;
|
|
29
|
+
if (cached !== undefined && CONSENT_CATEGORIES.every((category) => cached[category] === stored[category])) {
|
|
30
|
+
return cached;
|
|
31
|
+
}
|
|
32
|
+
cachedDecision.current = stored;
|
|
33
|
+
return cachedDecision.current;
|
|
34
|
+
}, () => undefined);
|
|
35
|
+
// Kept stable across renders (when their own inputs don't change) so a consumer that
|
|
36
|
+
// passes this hook's result down as a prop/effect-dep doesn't get a fresh identity —
|
|
37
|
+
// and therefore an unnecessary re-render/effect-rerun — every render.
|
|
38
|
+
const saveDecision = useCallback((next) => {
|
|
39
|
+
// No local state — the save notifies the subscription, which re-renders with the new snapshot.
|
|
40
|
+
storage.save({ decision: next, policyVersion, timestamp: Date.now() });
|
|
41
|
+
onDecision?.(next);
|
|
42
|
+
}, [onDecision, policyVersion, storage]);
|
|
43
|
+
const denyAll = useCallback(() => {
|
|
44
|
+
saveDecision(createConsentDecision([]));
|
|
45
|
+
}, [saveDecision]);
|
|
46
|
+
const grantAll = useCallback(() => {
|
|
47
|
+
saveDecision(createConsentDecision(requestedCategories));
|
|
48
|
+
}, [requestedCategories, saveDecision]);
|
|
49
|
+
const effectiveConsent = decision ?? resolveDefaultConsent({ hasGlobalPrivacyControlSignal, mode, requestedCategories });
|
|
50
|
+
return useMemo(() => ({
|
|
51
|
+
decision,
|
|
52
|
+
denyAll,
|
|
53
|
+
effectiveConsent,
|
|
54
|
+
grantAll,
|
|
55
|
+
isAnalyticsAllowed: effectiveConsent.analytics,
|
|
56
|
+
isPromptNeeded: mode === "opt-in" && decision === undefined,
|
|
57
|
+
saveDecision,
|
|
58
|
+
}), [decision, denyAll, effectiveConsent, grantAll, mode, saveDecision]);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=use-consent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-consent.js","sourceRoot":"","sources":["../../src/react/use-consent.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAG3E,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAkCtH;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,OAA0B;IACnD,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAC9C,MAAM,EAAE,aAAa,EAAE,mBAAmB,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9D,MAAM,6BAA6B,GAAG,OAAO,CAAC,6BAA6B,IAAI,KAAK,CAAC;IAErF,uFAAuF;IACvF,iFAAiF;IACjF,MAAM,cAAc,GAAG,MAAM,CAA8B,SAAS,C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,GAAG,WAAW,CAAC,GAAG,EAAE;QAChC,YAAY,CAAC,qBAAqB,CAAC,mBAAmB,CAAC,CAAC,CAAC;IAC3D,CAAC,EAAE,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC,CAAC;IAExC,MAAM,gBAAgB,GACpB,QAAQ,IAAI,qBAAqB,CAAC,EAAE,6BAA6B,EAAE,IAAI,EAAE,mBAAmB,EAAE,CAAC,CAAC;IAElG,OAAO,OAAO,CACZ,GAAG,EAAE,CAAC,CAAC;QACL,QAAQ;QACR,OAAO;QACP,gBAAgB;QAChB,QAAQ;QACR,kBAAkB,EAAE,gBAAgB,CAAC,SAAS;QAC9C,cAAc,EAAE,IAAI,KAAK,QAAQ,IAAI,QAAQ,KAAK,SAAS;QAC3D,YAAY;KACb,CAAC,EACF,CAAC,QAAQ,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,CAAC,CACpE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { UseConsentResult } from "#/react/use-consent";
|
|
2
|
+
/**
|
|
3
|
+
* @since 1.0.0-canary.6
|
|
4
|
+
*/
|
|
5
|
+
export interface UseGoogleConsentSyncOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Idempotent gtag.js load — advanced Consent Mode already injects the tag from a
|
|
8
|
+
* bootstrap; pass a loader as a safety net after a runtime grant.
|
|
9
|
+
*/
|
|
10
|
+
loadGtagScript?: (() => void) | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Keeps Google Consent Mode v2 in sync with `useConsent`: emits `update` when a stored
|
|
14
|
+
* decision exists (including cross-tab / privacy-page saves), and optionally loads gtag
|
|
15
|
+
* when analytics is effective. Mount once on a page-wide surface (e.g. the footer gate).
|
|
16
|
+
*
|
|
17
|
+
* @since 1.0.0-canary.6
|
|
18
|
+
*/
|
|
19
|
+
export declare function useGoogleConsentSync(consent: UseConsentResult, options?: UseGoogleConsentSyncOptions): void;
|
|
20
|
+
//# sourceMappingURL=use-google-consent-sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-google-consent-sync.d.ts","sourceRoot":"","sources":["../../src/react/use-google-consent-sync.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAAC;CAC3C;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,EAAE,OAAO,GAAE,2BAAgC,GAAG,IAAI,CAY/G"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { updateGoogleConsent } from "#/destinations/google-analytics";
|
|
4
|
+
/**
|
|
5
|
+
* Keeps Google Consent Mode v2 in sync with `useConsent`: emits `update` when a stored
|
|
6
|
+
* decision exists (including cross-tab / privacy-page saves), and optionally loads gtag
|
|
7
|
+
* when analytics is effective. Mount once on a page-wide surface (e.g. the footer gate).
|
|
8
|
+
*
|
|
9
|
+
* @since 1.0.0-canary.6
|
|
10
|
+
*/
|
|
11
|
+
export function useGoogleConsentSync(consent, options = {}) {
|
|
12
|
+
const { loadGtagScript } = options;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (consent.decision !== undefined) {
|
|
15
|
+
updateGoogleConsent(consent.decision);
|
|
16
|
+
}
|
|
17
|
+
if (consent.effectiveConsent.analytics) {
|
|
18
|
+
loadGtagScript?.();
|
|
19
|
+
}
|
|
20
|
+
}, [consent.decision, consent.effectiveConsent.analytics, loadGtagScript]);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=use-google-consent-sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-google-consent-sync.js","sourceRoot":"","sources":["../../src/react/use-google-consent-sync.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AActE;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAyB,EAAE,OAAO,GAAgC,EAAE;IACvG,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAEnC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACnC,mBAAmB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACxC,CAAC;QAED,IAAI,OAAO,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC;YACvC,cAAc,EAAE,EAAE,CAAC;QACrB,CAAC;IACH,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,gBAAgB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { InitialConsentSnapshot, InitialConsentStore } from "#/client/initial-consent-store";
|
|
2
|
+
/**
|
|
3
|
+
* Reactive view of a `createInitialConsentStore` — kicks off resolution on mount (a no-op
|
|
4
|
+
* when the app already did so at router creation) and re-renders once the server lane
|
|
5
|
+
* answers. SSR and the first client render read the strictest snapshot, so hydration can
|
|
6
|
+
* never mismatch on region.
|
|
7
|
+
*
|
|
8
|
+
* @since 1.0.0-canary.6
|
|
9
|
+
*/
|
|
10
|
+
export declare function useInitialConsent(store: InitialConsentStore): InitialConsentSnapshot;
|
|
11
|
+
//# sourceMappingURL=use-initial-consent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-initial-consent.d.ts","sourceRoot":"","sources":["../../src/react/use-initial-consent.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAElG;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,mBAAmB,GAAG,sBAAsB,CAOpF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useSyncExternalStore } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Reactive view of a `createInitialConsentStore` — kicks off resolution on mount (a no-op
|
|
5
|
+
* when the app already did so at router creation) and re-renders once the server lane
|
|
6
|
+
* answers. SSR and the first client render read the strictest snapshot, so hydration can
|
|
7
|
+
* never mismatch on region.
|
|
8
|
+
*
|
|
9
|
+
* @since 1.0.0-canary.6
|
|
10
|
+
*/
|
|
11
|
+
export function useInitialConsent(store) {
|
|
12
|
+
// Post-hydration safety net — idempotent with an earlier, pre-hydration kick.
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
store.ensureResolved();
|
|
15
|
+
}, [store]);
|
|
16
|
+
return useSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=use-initial-consent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-initial-consent.js","sourceRoot":"","sources":["../../src/react/use-initial-consent.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAIxD;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAA0B;IAC1D,8EAA8E;IAC9E,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,CAAC,cAAc,EAAE,CAAC;IACzB,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,oBAAoB,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAC3F,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guards the cookie name against anything outside the RFC 6265 token set.
|
|
3
|
+
*
|
|
4
|
+
* @throws Error when `cookieName` is not a valid token.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0-canary.7
|
|
7
|
+
*/
|
|
8
|
+
export declare function assertValidAnonymousIdCookieName(cookieName: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Whether a value is safe to persist as an anonymous id. The persist endpoint is public and
|
|
11
|
+
* writes its input into a `Set-Cookie`, so only an exactly-UUID-shaped value may pass —
|
|
12
|
+
* anything else is a header-injection attempt or corruption, never a valid id.
|
|
13
|
+
*
|
|
14
|
+
* @since 1.0.0-canary.6
|
|
15
|
+
*/
|
|
16
|
+
export declare function isValidAnonymousId(value: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* @since 1.0.0-canary.6
|
|
19
|
+
*/
|
|
20
|
+
export interface AnonymousIdCookieOptions {
|
|
21
|
+
/** Cookie name — must match what the client tracker reads. */
|
|
22
|
+
cookieName: string;
|
|
23
|
+
/** The client-minted id to persist — throws unless it is exactly UUID-shaped. */
|
|
24
|
+
id: string;
|
|
25
|
+
/** How long the id survives a return visit. Defaults to one year. */
|
|
26
|
+
maxAgeSeconds?: number | undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The validated name/value plus cookie attributes for persisting a client-minted anonymous
|
|
30
|
+
* id from the server — feed straight into the framework's cookie helper (`setCookie`).
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* Always `Secure` — on a plain-HTTP dev origin a browser may drop it, and the
|
|
34
|
+
* client-written cookie (`createServerPersistedAnonymousId`'s optimistic write) still
|
|
35
|
+
* covers that session. Not `httpOnly`: the client tracker must read the id to stamp it onto
|
|
36
|
+
* events. A server-set cookie escapes Safari ITP's 7-day cap on `document.cookie` writes, so
|
|
37
|
+
* the id actually lives its full max-age. The server only ever re-sets an id the client
|
|
38
|
+
* hands it — minting stays client-side, after consent, by design.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error when `id` is not UUID-shaped or `cookieName` is not a valid token.
|
|
41
|
+
*
|
|
42
|
+
* @since 1.0.0-canary.6
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveAnonymousIdCookie(options: AnonymousIdCookieOptions): {
|
|
45
|
+
maxAge: number;
|
|
46
|
+
name: string;
|
|
47
|
+
path: string;
|
|
48
|
+
sameSite: "lax";
|
|
49
|
+
secure: true;
|
|
50
|
+
value: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Name plus attributes for expiring the anonymous-id cookie — the server half of a consent
|
|
54
|
+
* withdrawal. Attributes must match {@link resolveAnonymousIdCookie} so the browser targets
|
|
55
|
+
* the same cookie. Feed into the framework's `deleteCookie`.
|
|
56
|
+
*
|
|
57
|
+
* @throws Error when `cookieName` is not a valid token.
|
|
58
|
+
*
|
|
59
|
+
* @since 1.0.0-canary.6
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveClearAnonymousIdCookie(cookieName: string): {
|
|
62
|
+
name: string;
|
|
63
|
+
path: string;
|
|
64
|
+
sameSite: "lax";
|
|
65
|
+
secure: true;
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=anonymous-id-cookie.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anonymous-id-cookie.d.ts","sourceRoot":"","sources":["../../src/server/anonymous-id-cookie.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAIzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,8DAA8D;IAC9D,UAAU,EAAE,MAAM,CAAC;IACnB,iFAAiF;IACjF,EAAE,EAAE,MAAM,CAAC;IACX,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,wBAAwB,GAAG;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,KAAK,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAeA;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,KAAK,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;CACd,CAIA"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ONE_YEAR_IN_SECONDS } from "#/core/cookie";
|
|
2
|
+
// The only shape the client ever mints (crypto.randomUUID) — anything else is rejected.
|
|
3
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
4
|
+
// Conservative subset of the RFC 6265 cookie-name token.
|
|
5
|
+
const COOKIE_NAME_PATTERN = /^[\w-]+$/;
|
|
6
|
+
/**
|
|
7
|
+
* Guards the cookie name against anything outside the RFC 6265 token set.
|
|
8
|
+
*
|
|
9
|
+
* @throws Error when `cookieName` is not a valid token.
|
|
10
|
+
*
|
|
11
|
+
* @since 1.0.0-canary.7
|
|
12
|
+
*/
|
|
13
|
+
export function assertValidAnonymousIdCookieName(cookieName) {
|
|
14
|
+
if (!COOKIE_NAME_PATTERN.test(cookieName)) {
|
|
15
|
+
throw new Error(`Invalid anonymous-id cookie name: ${JSON.stringify(cookieName)}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Whether a value is safe to persist as an anonymous id. The persist endpoint is public and
|
|
20
|
+
* writes its input into a `Set-Cookie`, so only an exactly-UUID-shaped value may pass —
|
|
21
|
+
* anything else is a header-injection attempt or corruption, never a valid id.
|
|
22
|
+
*
|
|
23
|
+
* @since 1.0.0-canary.6
|
|
24
|
+
*/
|
|
25
|
+
export function isValidAnonymousId(value) {
|
|
26
|
+
return UUID_PATTERN.test(value);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The validated name/value plus cookie attributes for persisting a client-minted anonymous
|
|
30
|
+
* id from the server — feed straight into the framework's cookie helper (`setCookie`).
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* Always `Secure` — on a plain-HTTP dev origin a browser may drop it, and the
|
|
34
|
+
* client-written cookie (`createServerPersistedAnonymousId`'s optimistic write) still
|
|
35
|
+
* covers that session. Not `httpOnly`: the client tracker must read the id to stamp it onto
|
|
36
|
+
* events. A server-set cookie escapes Safari ITP's 7-day cap on `document.cookie` writes, so
|
|
37
|
+
* the id actually lives its full max-age. The server only ever re-sets an id the client
|
|
38
|
+
* hands it — minting stays client-side, after consent, by design.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error when `id` is not UUID-shaped or `cookieName` is not a valid token.
|
|
41
|
+
*
|
|
42
|
+
* @since 1.0.0-canary.6
|
|
43
|
+
*/
|
|
44
|
+
export function resolveAnonymousIdCookie(options) {
|
|
45
|
+
assertValidAnonymousIdCookieName(options.cookieName);
|
|
46
|
+
if (!isValidAnonymousId(options.id)) {
|
|
47
|
+
throw new Error("Invalid anonymous id: expected a UUID-shaped value");
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
maxAge: options.maxAgeSeconds ?? ONE_YEAR_IN_SECONDS,
|
|
51
|
+
name: options.cookieName,
|
|
52
|
+
path: "/",
|
|
53
|
+
sameSite: "lax",
|
|
54
|
+
secure: true,
|
|
55
|
+
value: options.id,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Name plus attributes for expiring the anonymous-id cookie — the server half of a consent
|
|
60
|
+
* withdrawal. Attributes must match {@link resolveAnonymousIdCookie} so the browser targets
|
|
61
|
+
* the same cookie. Feed into the framework's `deleteCookie`.
|
|
62
|
+
*
|
|
63
|
+
* @throws Error when `cookieName` is not a valid token.
|
|
64
|
+
*
|
|
65
|
+
* @since 1.0.0-canary.6
|
|
66
|
+
*/
|
|
67
|
+
export function resolveClearAnonymousIdCookie(cookieName) {
|
|
68
|
+
assertValidAnonymousIdCookieName(cookieName);
|
|
69
|
+
return { name: cookieName, path: "/", sameSite: "lax", secure: true };
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=anonymous-id-cookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anonymous-id-cookie.js","sourceRoot":"","sources":["../../src/server/anonymous-id-cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,wFAAwF;AACxF,MAAM,YAAY,GAAG,iEAAiE,CAAC;AAEvF,yDAAyD;AACzD,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAEvC;;;;;;GAMG;AACH,MAAM,UAAU,gCAAgC,CAAC,UAAkB;IACjE,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,qCAAqC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC;AAcD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,wBAAwB,CAAC,OAAiC;IAQxE,gCAAgC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAErD,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IACxE,CAAC;IAED,OAAO;QACL,MAAM,EAAE,OAAO,CAAC,aAAa,IAAI,mBAAmB;QACpD,IAAI,EAAE,OAAO,CAAC,UAAU;QACxB,IAAI,EAAE,GAAG;QACT,QAAQ,EAAE,KAAK;QACf,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,OAAO,CAAC,EAAE;KAClB,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,6BAA6B,CAAC,UAAkB;IAM9D,gCAAgC,CAAC,UAAU,CAAC,CAAC;IAE7C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ConsentReceipt } from "#/core/consent-receipt";
|
|
2
|
+
/**
|
|
3
|
+
* Persistence backend for consent receipts — the durable store is the consumer's to supply
|
|
4
|
+
* (a database, an append-only log). The store is **append-only**: `append` never overwrites,
|
|
5
|
+
* and an update/withdrawal is a new receipt referencing the prior one (spec-consent-receipts §3).
|
|
6
|
+
* May be async so a real backend can await its write.
|
|
7
|
+
*
|
|
8
|
+
* @since 1.0.0-canary.7
|
|
9
|
+
*/
|
|
10
|
+
export interface ReceiptStore {
|
|
11
|
+
append: (receipt: ConsentReceipt) => Promise<void> | void;
|
|
12
|
+
get: (receiptId: string) => ConsentReceipt | undefined | Promise<ConsentReceipt | undefined>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* An in-memory {@link ReceiptStore} for development and tests — **not** durable across a
|
|
16
|
+
* process restart, and not a lawful production store on its own. A real deployment supplies
|
|
17
|
+
* a database-backed implementation; this exists so the endpoint is runnable end-to-end
|
|
18
|
+
* without standing up storage first.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Append is idempotent-by-id: re-appending the same `receiptId` is ignored, preserving the
|
|
22
|
+
* append-only guarantee even if a client retries.
|
|
23
|
+
*
|
|
24
|
+
* @since 1.0.0-canary.7
|
|
25
|
+
*/
|
|
26
|
+
export declare function createInMemoryReceiptStore(): ReceiptStore;
|
|
27
|
+
/**
|
|
28
|
+
* The minimal id-keyed primitive a durable backend must expose for {@link createDurableReceiptStore}.
|
|
29
|
+
* Keeping it this small lets an integrator back receipts with any store (Vercel KV, Postgres,
|
|
30
|
+
* an append-only log) by implementing two methods rather than the full {@link ReceiptStore}.
|
|
31
|
+
*
|
|
32
|
+
* @since 1.0.0-canary.7
|
|
33
|
+
*/
|
|
34
|
+
export interface ReceiptStoreBackend {
|
|
35
|
+
get: (receiptId: string) => Promise<ConsentReceipt | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Persist one receipt under its id. MUST be idempotent-by-id and never overwrite — a
|
|
38
|
+
* duplicate `receiptId` is a no-op — so the append-only guarantee holds atomically even
|
|
39
|
+
* under a client retry or concurrent write (e.g. KV set-if-absent, Postgres
|
|
40
|
+
* `INSERT … ON CONFLICT DO NOTHING`). The frame delegates rather than doing a racy
|
|
41
|
+
* get-then-put itself.
|
|
42
|
+
*/
|
|
43
|
+
put: (receiptId: string, receipt: ConsentReceipt) => Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A durable {@link ReceiptStore} over an injected {@link ReceiptStoreBackend} — the package
|
|
47
|
+
* supplies the append-only contract and adaptation, the deployment supplies the backend
|
|
48
|
+
* client (no database dependency is baked in). Pair it with a real backend in production,
|
|
49
|
+
* where {@link createInMemoryReceiptStore} is not a lawful store on its own.
|
|
50
|
+
*
|
|
51
|
+
* @since 1.0.0-canary.7
|
|
52
|
+
*/
|
|
53
|
+
export declare function createDurableReceiptStore(options: {
|
|
54
|
+
backend: ReceiptStoreBackend;
|
|
55
|
+
}): ReceiptStore;
|
|
56
|
+
//# sourceMappingURL=consent-receipt-store.d.ts.map
|