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