@codefast/tracking 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +235 -105
  3. package/dist/adapters/tanstack-start.d.ts +12 -9
  4. package/dist/adapters/tanstack-start.js +5 -6
  5. package/dist/client/ad-framework-consent.d.ts +9 -8
  6. package/dist/client/ad-framework-consent.js +4 -5
  7. package/dist/client/browser-cookie.d.ts +2 -3
  8. package/dist/client/browser-cookie.js +2 -3
  9. package/dist/client/consent-runtime.d.ts +6 -5
  10. package/dist/client/consent-runtime.js +2 -3
  11. package/dist/client/consent-storage.d.ts +1 -2
  12. package/dist/client/consent-storage.js +1 -2
  13. package/dist/client/consent-withdrawal.d.ts +5 -4
  14. package/dist/client/consent-withdrawal.js +2 -3
  15. package/dist/client/cookie-anonymous-id.d.ts +8 -5
  16. package/dist/client/cookie-anonymous-id.js +2 -3
  17. package/dist/client/create-client-tracker.d.ts +8 -3
  18. package/dist/client/create-client-tracker.js +3 -2
  19. package/dist/client/gpc.d.ts +1 -2
  20. package/dist/client/gpc.js +1 -2
  21. package/dist/client/initial-consent-store.d.ts +7 -6
  22. package/dist/client/initial-consent-store.js +2 -3
  23. package/dist/client/is-analytics-allowed.d.ts +5 -4
  24. package/dist/client/is-analytics-allowed.js +2 -3
  25. package/dist/client/server-persisted-anonymous-id.d.ts +5 -4
  26. package/dist/client/server-persisted-anonymous-id.js +2 -3
  27. package/dist/core/consent-config.d.ts +3 -4
  28. package/dist/core/consent-config.js +2 -3
  29. package/dist/core/consent-receipt.d.ts +7 -8
  30. package/dist/core/consent-receipt.js +2 -3
  31. package/dist/core/consent.d.ts +24 -13
  32. package/dist/core/consent.js +13 -10
  33. package/dist/core/cookie.d.ts +3 -4
  34. package/dist/core/cookie.js +3 -4
  35. package/dist/core/destination.d.ts +1 -2
  36. package/dist/core/destination.js +1 -2
  37. package/dist/core/event-catalog.d.ts +2 -3
  38. package/dist/core/event-catalog.js +2 -3
  39. package/dist/core/event-id.d.ts +1 -2
  40. package/dist/core/event-id.js +1 -2
  41. package/dist/core/tracked-event.d.ts +1 -2
  42. package/dist/core/tracked-event.js +1 -2
  43. package/dist/destinations/ad-consent.d.ts +3 -4
  44. package/dist/destinations/ad-consent.js +2 -3
  45. package/dist/destinations/google-analytics.d.ts +17 -10
  46. package/dist/destinations/google-analytics.js +5 -6
  47. package/dist/destinations/google-consent.d.ts +10 -9
  48. package/dist/destinations/google-consent.js +6 -7
  49. package/dist/destinations/meta.d.ts +8 -7
  50. package/dist/destinations/meta.js +3 -4
  51. package/dist/destinations/microsoft-uet.d.ts +8 -7
  52. package/dist/destinations/microsoft-uet.js +3 -4
  53. package/dist/destinations/shared.d.ts +8 -5
  54. package/dist/destinations/shared.js +4 -3
  55. package/dist/destinations/tiktok.d.ts +8 -7
  56. package/dist/destinations/tiktok.js +3 -4
  57. package/dist/destinations/vercel-analytics.d.ts +4 -3
  58. package/dist/destinations/vercel-analytics.js +1 -2
  59. package/dist/index.d.ts +1 -2
  60. package/dist/index.js +1 -2
  61. package/dist/react/consent-banner.d.ts +29 -2
  62. package/dist/react/consent-banner.js +12 -9
  63. package/dist/react/gtag-consent-bootstrap.d.ts +5 -4
  64. package/dist/react/gtag-consent-bootstrap.js +2 -3
  65. package/dist/react/use-consent.d.ts +5 -2
  66. package/dist/react/use-consent.js +1 -2
  67. package/dist/react/use-google-consent-sync.d.ts +5 -4
  68. package/dist/react/use-google-consent-sync.js +2 -3
  69. package/dist/react/use-initial-consent.d.ts +2 -3
  70. package/dist/react/use-initial-consent.js +2 -3
  71. package/dist/server/anonymous-id-cookie.d.ts +8 -7
  72. package/dist/server/anonymous-id-cookie.js +5 -6
  73. package/dist/server/consent-receipt-store.d.ts +5 -6
  74. package/dist/server/consent-receipt-store.js +3 -4
  75. package/dist/server/consent-receipt.d.ts +5 -6
  76. package/dist/server/consent-receipt.js +4 -5
  77. package/dist/server/initial-consent.d.ts +5 -4
  78. package/dist/server/initial-consent.js +2 -3
  79. package/dist/server/measurement-protocol.d.ts +10 -9
  80. package/dist/server/measurement-protocol.js +4 -5
  81. package/dist/server/region.d.ts +3 -4
  82. package/dist/server/region.js +3 -4
  83. package/dist/server/user-deletion.d.ts +10 -7
  84. package/dist/server/user-deletion.js +3 -4
  85. package/dist/tooling/import-protection.d.ts +2 -3
  86. package/dist/tooling/import-protection.js +2 -3
  87. package/package.json +11 -62
  88. package/dist/adapters/tanstack-start.d.ts.map +0 -1
  89. package/dist/adapters/tanstack-start.js.map +0 -1
  90. package/dist/client/ad-framework-consent.d.ts.map +0 -1
  91. package/dist/client/ad-framework-consent.js.map +0 -1
  92. package/dist/client/browser-cookie.d.ts.map +0 -1
  93. package/dist/client/browser-cookie.js.map +0 -1
  94. package/dist/client/consent-runtime.d.ts.map +0 -1
  95. package/dist/client/consent-runtime.js.map +0 -1
  96. package/dist/client/consent-storage.d.ts.map +0 -1
  97. package/dist/client/consent-storage.js.map +0 -1
  98. package/dist/client/consent-withdrawal.d.ts.map +0 -1
  99. package/dist/client/consent-withdrawal.js.map +0 -1
  100. package/dist/client/cookie-anonymous-id.d.ts.map +0 -1
  101. package/dist/client/cookie-anonymous-id.js.map +0 -1
  102. package/dist/client/create-client-tracker.d.ts.map +0 -1
  103. package/dist/client/create-client-tracker.js.map +0 -1
  104. package/dist/client/gpc.d.ts.map +0 -1
  105. package/dist/client/gpc.js.map +0 -1
  106. package/dist/client/initial-consent-store.d.ts.map +0 -1
  107. package/dist/client/initial-consent-store.js.map +0 -1
  108. package/dist/client/is-analytics-allowed.d.ts.map +0 -1
  109. package/dist/client/is-analytics-allowed.js.map +0 -1
  110. package/dist/client/server-persisted-anonymous-id.d.ts.map +0 -1
  111. package/dist/client/server-persisted-anonymous-id.js.map +0 -1
  112. package/dist/core/consent-config.d.ts.map +0 -1
  113. package/dist/core/consent-config.js.map +0 -1
  114. package/dist/core/consent-receipt.d.ts.map +0 -1
  115. package/dist/core/consent-receipt.js.map +0 -1
  116. package/dist/core/consent.d.ts.map +0 -1
  117. package/dist/core/consent.js.map +0 -1
  118. package/dist/core/cookie.d.ts.map +0 -1
  119. package/dist/core/cookie.js.map +0 -1
  120. package/dist/core/destination.d.ts.map +0 -1
  121. package/dist/core/destination.js.map +0 -1
  122. package/dist/core/event-catalog.d.ts.map +0 -1
  123. package/dist/core/event-catalog.js.map +0 -1
  124. package/dist/core/event-id.d.ts.map +0 -1
  125. package/dist/core/event-id.js.map +0 -1
  126. package/dist/core/tracked-event.d.ts.map +0 -1
  127. package/dist/core/tracked-event.js.map +0 -1
  128. package/dist/destinations/ad-consent.d.ts.map +0 -1
  129. package/dist/destinations/ad-consent.js.map +0 -1
  130. package/dist/destinations/google-analytics.d.ts.map +0 -1
  131. package/dist/destinations/google-analytics.js.map +0 -1
  132. package/dist/destinations/google-consent.d.ts.map +0 -1
  133. package/dist/destinations/google-consent.js.map +0 -1
  134. package/dist/destinations/meta.d.ts.map +0 -1
  135. package/dist/destinations/meta.js.map +0 -1
  136. package/dist/destinations/microsoft-uet.d.ts.map +0 -1
  137. package/dist/destinations/microsoft-uet.js.map +0 -1
  138. package/dist/destinations/shared.d.ts.map +0 -1
  139. package/dist/destinations/shared.js.map +0 -1
  140. package/dist/destinations/tiktok.d.ts.map +0 -1
  141. package/dist/destinations/tiktok.js.map +0 -1
  142. package/dist/destinations/vercel-analytics.d.ts.map +0 -1
  143. package/dist/destinations/vercel-analytics.js.map +0 -1
  144. package/dist/index.d.ts.map +0 -1
  145. package/dist/index.js.map +0 -1
  146. package/dist/react/consent-banner.d.ts.map +0 -1
  147. package/dist/react/consent-banner.js.map +0 -1
  148. package/dist/react/gtag-consent-bootstrap.d.ts.map +0 -1
  149. package/dist/react/gtag-consent-bootstrap.js.map +0 -1
  150. package/dist/react/use-consent.d.ts.map +0 -1
  151. package/dist/react/use-consent.js.map +0 -1
  152. package/dist/react/use-google-consent-sync.d.ts.map +0 -1
  153. package/dist/react/use-google-consent-sync.js.map +0 -1
  154. package/dist/react/use-initial-consent.d.ts.map +0 -1
  155. package/dist/react/use-initial-consent.js.map +0 -1
  156. package/dist/server/anonymous-id-cookie.d.ts.map +0 -1
  157. package/dist/server/anonymous-id-cookie.js.map +0 -1
  158. package/dist/server/consent-receipt-store.d.ts.map +0 -1
  159. package/dist/server/consent-receipt-store.js.map +0 -1
  160. package/dist/server/consent-receipt.d.ts.map +0 -1
  161. package/dist/server/consent-receipt.js.map +0 -1
  162. package/dist/server/initial-consent.d.ts.map +0 -1
  163. package/dist/server/initial-consent.js.map +0 -1
  164. package/dist/server/measurement-protocol.d.ts.map +0 -1
  165. package/dist/server/measurement-protocol.js.map +0 -1
  166. package/dist/server/region.d.ts.map +0 -1
  167. package/dist/server/region.js.map +0 -1
  168. package/dist/server/user-deletion.d.ts.map +0 -1
  169. package/dist/server/user-deletion.js.map +0 -1
  170. package/dist/tooling/import-protection.d.ts.map +0 -1
  171. package/dist/tooling/import-protection.js.map +0 -1
  172. package/src/adapters/tanstack-start.ts +0 -135
  173. package/src/client/ad-framework-consent.ts +0 -86
  174. package/src/client/browser-cookie.ts +0 -16
  175. package/src/client/consent-runtime.ts +0 -80
  176. package/src/client/consent-storage.ts +0 -125
  177. package/src/client/consent-withdrawal.ts +0 -32
  178. package/src/client/cookie-anonymous-id.ts +0 -112
  179. package/src/client/create-client-tracker.ts +0 -168
  180. package/src/client/gpc.ts +0 -15
  181. package/src/client/initial-consent-store.ts +0 -218
  182. package/src/client/is-analytics-allowed.ts +0 -43
  183. package/src/client/server-persisted-anonymous-id.ts +0 -102
  184. package/src/core/consent-config.ts +0 -27
  185. package/src/core/consent-receipt.ts +0 -115
  186. package/src/core/consent.ts +0 -286
  187. package/src/core/cookie.ts +0 -29
  188. package/src/core/destination.ts +0 -29
  189. package/src/core/event-catalog.ts +0 -54
  190. package/src/core/event-id.ts +0 -8
  191. package/src/core/tracked-event.ts +0 -14
  192. package/src/css/consent.css +0 -119
  193. package/src/destinations/ad-consent.ts +0 -29
  194. package/src/destinations/google-analytics.ts +0 -296
  195. package/src/destinations/google-consent.ts +0 -164
  196. package/src/destinations/meta.ts +0 -96
  197. package/src/destinations/microsoft-uet.ts +0 -86
  198. package/src/destinations/shared.ts +0 -45
  199. package/src/destinations/tiktok.ts +0 -85
  200. package/src/destinations/vercel-analytics.ts +0 -39
  201. package/src/index.ts +0 -171
  202. package/src/react/consent-banner.tsx +0 -360
  203. package/src/react/gtag-consent-bootstrap.tsx +0 -48
  204. package/src/react/use-consent.ts +0 -119
  205. package/src/react/use-google-consent-sync.ts +0 -38
  206. package/src/react/use-initial-consent.ts +0 -22
  207. package/src/server/anonymous-id-cookie.ts +0 -103
  208. package/src/server/consent-receipt-store.ts +0 -79
  209. package/src/server/consent-receipt.ts +0 -89
  210. package/src/server/initial-consent.ts +0 -46
  211. package/src/server/measurement-protocol.ts +0 -114
  212. package/src/server/region.ts +0 -77
  213. package/src/server/user-deletion.ts +0 -93
  214. package/src/tooling/import-protection.ts +0 -22
@@ -1,85 +0,0 @@
1
- import type { ConsentDecision } from "#/core/consent";
2
- import type { Destination } from "#/core/destination";
3
- import { toAdConsentState } from "#/destinations/ad-consent";
4
- import type { FlatPropertyValue } from "#/destinations/shared";
5
- import { flattenEventProps } from "#/destinations/shared";
6
-
7
- /**
8
- * TikTok consent. TikTok uses a **single Limited Data Use boolean**, not Meta's
9
- * `dataProcessingOptions` structure and no signal object (spec-destinations §5). Its
10
- * transmission gate (`ttq.grantConsent()`/`revokeConsent()`) follows `analytics` and is the
11
- * tracker's job; this models the `ads`-driven LDU lever.
12
- *
13
- * @since 1.0.0-canary.7
14
- */
15
- export interface TiktokConsent {
16
- limited_data_use: boolean;
17
- }
18
-
19
- /**
20
- * Maps a `ConsentDecision` to TikTok's LDU boolean — on when `ads` is denied.
21
- *
22
- * @since 1.0.0-canary.7
23
- */
24
- export function toTiktokConsent(decision: ConsentDecision): TiktokConsent {
25
- return { limited_data_use: toAdConsentState(decision).limitedDataUse };
26
- }
27
-
28
- /**
29
- * The mapped per-event payload handed to a TikTok transport (`ttq.track` / Events API).
30
- *
31
- * @since 1.0.0-canary.7
32
- */
33
- export interface TiktokEventPayload {
34
- consent: TiktokConsent;
35
- name: string;
36
- properties: Record<string, FlatPropertyValue>;
37
- }
38
-
39
- /**
40
- * @since 1.0.0-canary.7
41
- */
42
- export interface TiktokDestinationOptions {
43
- /** Erasure cookie-clear seam — TikTok exposes no per-visitor deletion API, so erasure is cookie-clear + stop-send (DSR-V4). */
44
- clearCookies?: (() => void) | undefined;
45
- /** Read per event so an `ads` change flips LDU without recreating the destination. */
46
- getDecision: () => ConsentDecision;
47
- name?: string | undefined;
48
- /**
49
- * Delivers one mapped event — the transport seam. Wire it to `ttq.track(...)` (Pixel) or the
50
- * Events API; this package ships the consent mapping, never the pixel id or network client.
51
- */
52
- transport: (payload: TiktokEventPayload) => Promise<void> | void;
53
- }
54
-
55
- /**
56
- * Reference ad destination for TikTok: maps each event and the live `ads` decision to
57
- * TikTok's LDU boolean, then hands it to an injected transport. Consent-restriction mapping
58
- * only — the pixel id and `ttq` transport are the integrator's. `consentRequirement` stays
59
- * `"required"`.
60
- *
61
- * @since 1.0.0-canary.7
62
- */
63
- export function createTiktokDestination(options: TiktokDestinationOptions): Destination {
64
- let stopped = false;
65
-
66
- return {
67
- consentRequirement: "required",
68
- name: options.name ?? "tiktok",
69
- onErasure() {
70
- stopped = true;
71
- options.clearCookies?.();
72
- },
73
- async send(event) {
74
- if (stopped) {
75
- return;
76
- }
77
-
78
- await options.transport({
79
- consent: toTiktokConsent(options.getDecision()),
80
- name: event.name,
81
- properties: flattenEventProps(event.properties),
82
- });
83
- },
84
- };
85
- }
@@ -1,39 +0,0 @@
1
- import { track } from "@vercel/analytics";
2
-
3
- import type { Destination } from "#/core/destination";
4
- import { flattenEventProps } from "#/destinations/shared";
5
-
6
- /**
7
- * @since 1.0.0-canary.6
8
- */
9
- export interface VercelAnalyticsDestinationOptions {
10
- /**
11
- * Vercel Analytics is cookieless and its events carry no identifier from this
12
- * package, so it qualifies for `"exempt"` — it then keeps receiving events while the
13
- * tracker's analytics gate is closed. Defaults to `"required"` so pre-consent delivery
14
- * stays an explicit opt-in.
15
- */
16
- consentRequirement?: "exempt" | "required" | undefined;
17
- name?: string | undefined;
18
- }
19
-
20
- /**
21
- * `track()` only pushes onto Vercel's own in-page queue (`window.va`/`window.vaq`), which
22
- * handles batching and unload delivery itself. Requires the Vercel Analytics script to be
23
- * loaded once elsewhere in the app (`<Analytics />` from `@vercel/analytics/react`, or the
24
- * framework-agnostic `inject()`) — this destination only queues events, so it imports the
25
- * base `@vercel/analytics` package and has no React dependency of its own.
26
- *
27
- * @since 0.5.0-canary.4
28
- */
29
- export function createVercelAnalyticsDestination(options: VercelAnalyticsDestinationOptions = {}): Destination {
30
- return {
31
- consentRequirement: options.consentRequirement ?? "required",
32
- name: options.name ?? "vercel-analytics",
33
- // Synchronous today, but declared async so a throw here rejects the returned Promise
34
- // instead of escaping as a synchronous throw — matches Destination.send's contract.
35
- async send(event) {
36
- track(event.name, flattenEventProps(event.properties, { allowNull: true }));
37
- },
38
- };
39
- }
package/src/index.ts DELETED
@@ -1,171 +0,0 @@
1
- /**
2
- * \@codefast/tracking
3
- *
4
- * Consent-gated, type-safe event tracking for TanStack Start apps: define an event
5
- * catalog over any Standard Schema library, wire a `ConsentConfig` + `createConsentRuntime`,
6
- * and fan tracked events out to gtag / Vercel Analytics destinations.
7
- *
8
- * This root is the client entry: it re-exports the isomorphic core plus the whole
9
- * browser-side surface (tracker, consent runtime, React bindings, client destinations).
10
- * Server-only lanes are their own subpaths and never belong here — `@codefast/tracking/server/*`
11
- * and `@codefast/tracking/adapters/*` (deny them in the client environment via
12
- * `SERVER_ONLY_SUBPATHS`). The Vercel destination stays on
13
- * `@codefast/tracking/destinations/vercel-analytics` so its `@vercel/analytics` peer is only
14
- * pulled in when used.
15
- */
16
-
17
- // ── Core (isomorphic) ────────────────────────────────────────────────────────────────────────────────────────────────
18
- export type {
19
- ConsentCategory,
20
- ConsentDecision,
21
- ConsentMode,
22
- ConsentRecord,
23
- ConsentRegion,
24
- ConsentStorage,
25
- InitialConsent,
26
- ResolveDefaultConsentOptions,
27
- ResolveEffectiveConsentOptions,
28
- } from "#/core/consent";
29
- export {
30
- CONSENT_CATEGORIES,
31
- CONSENT_REGIONS,
32
- createConsentDecision,
33
- isConsentDecision,
34
- isConsentRecord,
35
- isConsentRegion,
36
- isInitialConsent,
37
- normalizeConsentDecision,
38
- readStoredDecision,
39
- resolveConsentMode,
40
- resolveDefaultConsent,
41
- resolveEffectiveConsent,
42
- STRICTEST_INITIAL_CONSENT,
43
- } from "#/core/consent";
44
-
45
- export type { ConsentConfig } from "#/core/consent-config";
46
- export { defineConsentConfig } from "#/core/consent-config";
47
-
48
- export type {
49
- ConsentReceipt,
50
- ConsentReceiptEventType,
51
- ConsentReceiptInput,
52
- ConsentReceiptMethod,
53
- ConsentReceiptSubjectIdType,
54
- } from "#/core/consent-receipt";
55
- export { isConsentReceiptInput } from "#/core/consent-receipt";
56
-
57
- export { readCookieValue } from "#/core/cookie";
58
-
59
- export type { Destination } from "#/core/destination";
60
-
61
- export type { EventCatalog, EventDefinition } from "#/core/event-catalog";
62
- export { assertValidEventProperties, defineEventCatalog } from "#/core/event-catalog";
63
-
64
- export { generateEventId } from "#/core/event-id";
65
-
66
- export type { TrackedEvent } from "#/core/tracked-event";
67
-
68
- // ── Client ───────────────────────────────────────────────────────────────────────────────────────────────────────────
69
- export type { CmpConsentSignal, CmpStatus, ReconcileAdFrameworkConsentOptions } from "#/client/ad-framework-consent";
70
- export { hasGppApi, hasTcfApi, reconcileAdFrameworkConsent } from "#/client/ad-framework-consent";
71
-
72
- export type { ConsentRuntime, ConsentRuntimeOptions } from "#/client/consent-runtime";
73
- export { createConsentRuntime } from "#/client/consent-runtime";
74
-
75
- export { createLocalStorageConsentStorage } from "#/client/consent-storage";
76
-
77
- export type { ConsentWithdrawalHandlerOptions } from "#/client/consent-withdrawal";
78
- export { createConsentWithdrawalHandler } from "#/client/consent-withdrawal";
79
-
80
- export type { CookieAnonymousId, CookieAnonymousIdOptions } from "#/client/cookie-anonymous-id";
81
- export { createCookieAnonymousId } from "#/client/cookie-anonymous-id";
82
-
83
- export type { ClientTracker, ClientTrackerOptions, DeliveryErrorContext } from "#/client/create-client-tracker";
84
- export { createClientTracker } from "#/client/create-client-tracker";
85
-
86
- export { hasGlobalPrivacyControlSignal } from "#/client/gpc";
87
-
88
- export type {
89
- InitialConsentSnapshot,
90
- InitialConsentStore,
91
- InitialConsentStoreOptions,
92
- } from "#/client/initial-consent-store";
93
- export { createInitialConsentStore } from "#/client/initial-consent-store";
94
-
95
- export type { IsAnalyticsAllowedOptions } from "#/client/is-analytics-allowed";
96
- export { createIsAnalyticsAllowed } from "#/client/is-analytics-allowed";
97
-
98
- export type { ServerPersistedAnonymousIdOptions } from "#/client/server-persisted-anonymous-id";
99
- export { createServerPersistedAnonymousId } from "#/client/server-persisted-anonymous-id";
100
-
101
- // ── React ────────────────────────────────────────────────────────────────────────────────────────────────────────────
102
- export type {
103
- ConsentBannerAcceptProps,
104
- ConsentBannerActionsProps,
105
- ConsentBannerCategoryProps,
106
- ConsentBannerCustomizeProps,
107
- ConsentBannerDescriptionProps,
108
- ConsentBannerPreferencesProps,
109
- ConsentBannerProps,
110
- ConsentBannerRejectProps,
111
- ConsentBannerSaveProps,
112
- ConsentBannerTitleProps,
113
- ConsentToggleProps,
114
- } from "#/react/consent-banner";
115
- export {
116
- ConsentBanner,
117
- ConsentBannerAccept,
118
- ConsentBannerActions,
119
- ConsentBannerCategory,
120
- ConsentBannerCustomize,
121
- ConsentBannerDescription,
122
- ConsentBannerPreferences,
123
- ConsentBannerReject,
124
- ConsentBannerSave,
125
- ConsentBannerTitle,
126
- ConsentToggle,
127
- } from "#/react/consent-banner";
128
-
129
- export type { GtagConsentBootstrapProps } from "#/react/gtag-consent-bootstrap";
130
- export { GtagConsentBootstrap } from "#/react/gtag-consent-bootstrap";
131
-
132
- export type { UseConsentOptions, UseConsentResult } from "#/react/use-consent";
133
- export { useConsent } from "#/react/use-consent";
134
-
135
- export type { UseGoogleConsentSyncOptions } from "#/react/use-google-consent-sync";
136
- export { useGoogleConsentSync } from "#/react/use-google-consent-sync";
137
-
138
- export { useInitialConsent } from "#/react/use-initial-consent";
139
-
140
- // ── Destinations (client-lane; Vercel stays on its own subpath) ──────────────────────────────────────────────────────
141
- export type { AdConsentState } from "#/destinations/ad-consent";
142
- export { toAdConsentState } from "#/destinations/ad-consent";
143
-
144
- export type {
145
- EnsureGtagOptions,
146
- GoogleAnalyticsDestinationOptions,
147
- GtagConsentBootstrapOptions,
148
- GtagFunction,
149
- LoadGtagScriptOptions,
150
- } from "#/destinations/google-analytics";
151
- export {
152
- buildGtagConsentBootstrapScript,
153
- clearGoogleAnalyticsCookies,
154
- createGoogleAnalyticsDestination,
155
- ensureGtag,
156
- loadGtagScript,
157
- updateGoogleConsent,
158
- } from "#/destinations/google-analytics";
159
-
160
- export type { MetaDataProcessingOptions, MetaDestinationOptions, MetaEventPayload } from "#/destinations/meta";
161
- export { createMetaDestination, toMetaDataProcessingOptions } from "#/destinations/meta";
162
-
163
- export type {
164
- MicrosoftUetConsent,
165
- MicrosoftUetDestinationOptions,
166
- MicrosoftUetEventPayload,
167
- } from "#/destinations/microsoft-uet";
168
- export { createMicrosoftUetDestination, toMicrosoftUetConsent } from "#/destinations/microsoft-uet";
169
-
170
- export type { TiktokConsent, TiktokDestinationOptions, TiktokEventPayload } from "#/destinations/tiktok";
171
- export { createTiktokDestination, toTiktokConsent } from "#/destinations/tiktok";
@@ -1,360 +0,0 @@
1
- "use client";
2
-
3
- import type { ComponentProps, Dispatch, ReactNode, SetStateAction } from "react";
4
- import { createContext, useContext, useEffect, useState } from "react";
5
-
6
- import type { ConsentCategory, ConsentDecision } from "#/core/consent";
7
- import type { UseConsentResult } from "#/react/use-consent";
8
-
9
- interface ConsentBannerContextValue {
10
- consent: UseConsentResult;
11
- /** The in-progress per-category selection — `undefined` while the preferences layer is closed. */
12
- pending: ConsentDecision | undefined;
13
- setPending: Dispatch<SetStateAction<ConsentDecision | undefined>>;
14
- }
15
-
16
- const ConsentBannerContext = createContext<ConsentBannerContextValue | null>(null);
17
-
18
- function useConsentBannerContext(part: string): ConsentBannerContextValue {
19
- const context = useContext(ConsentBannerContext);
20
-
21
- if (context === null) {
22
- throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
23
- }
24
-
25
- return context;
26
- }
27
-
28
- /**
29
- * @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
-
37
- /**
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.
48
- *
49
- * @since 0.5.0-canary.4
50
- */
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"> {
237
- category: ConsentCategory;
238
- }
239
-
240
- /**
241
- * One checkbox row of the preferences layer — `children` is the visitor-facing wording.
242
- *
243
- * @since 0.5.0-canary.5
244
- */
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.
274
- *
275
- * @since 0.5.0-canary.5
276
- */
277
- export function ConsentBannerSave({ onClick, ...props }: ConsentBannerSaveProps): ReactNode {
278
- const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
279
-
280
- if (pending === undefined) {
281
- return null;
282
- }
283
-
284
- return (
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
- />
297
- );
298
- }
299
-
300
- /**
301
- * @since 0.5.0-canary.4
302
- */
303
- export interface ConsentToggleProps extends Omit<ComponentProps<"button">, "children" | "onClick" | "type"> {
304
- /**
305
- * Label shown while the scoped categories are denied — clicking grants them. Defaults
306
- * to "Allow tracking".
307
- */
308
- allowLabel?: string | undefined;
309
- consent: UseConsentResult;
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;
322
- }
323
-
324
- /**
325
- * Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
326
- * "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
327
- * By default it only flips `ads` (same scope as a GPC signal); pass `toggledCategories`
328
- * to include analytics or both.
329
- *
330
- * @since 0.5.0-canary.4
331
- */
332
- export function ConsentToggle({
333
- allowLabel = "Allow tracking",
334
- consent,
335
- denyLabel = "Do Not Sell or Share My Personal Information",
336
- toggledCategories = ["ads"],
337
- ...props
338
- }: ConsentToggleProps): ReactNode {
339
- const isAnyCategoryGranted = toggledCategories.some((category) => consent.effectiveConsent[category]);
340
-
341
- return (
342
- <button
343
- data-slot="consent-toggle"
344
- {...props}
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
- }}
355
- type="button"
356
- >
357
- {isAnyCategoryGranted ? denyLabel : allowLabel}
358
- </button>
359
- );
360
- }