@c15t/scripts 2.2.0 → 3.0.0-alpha.0

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 (197) hide show
  1. package/AGENTS.md +74 -48
  2. package/README.md +3 -3
  3. package/dist/e2e-test-utils.js +60 -24
  4. package/dist/engine/compile.js +45 -45
  5. package/dist/engine/runtime.js +119 -119
  6. package/dist/registry.js +186 -176
  7. package/dist/resolve.js +12 -12
  8. package/dist/vendors/_shared/attributes.js +5 -5
  9. package/dist/vendors/_shared/google-consent.js +10 -10
  10. package/dist/vendors/_shared/install-builders.js +9 -9
  11. package/dist/vendors/_shared/script-url.js +12 -12
  12. package/dist/vendors/ads-and-pixels/linkedin-insights.js +16 -16
  13. package/dist/vendors/ads-and-pixels/meta-pixel.js +82 -82
  14. package/dist/vendors/ads-and-pixels/microsoft-uet.js +57 -57
  15. package/dist/vendors/ads-and-pixels/openai-pixel.js +88 -0
  16. package/dist/vendors/ads-and-pixels/reddit-pixel.js +39 -39
  17. package/dist/vendors/ads-and-pixels/snapchat-pixel.js +25 -25
  18. package/dist/vendors/ads-and-pixels/tiktok-pixel.js +31 -31
  19. package/dist/vendors/ads-and-pixels/x-pixel.js +17 -17
  20. package/dist/vendors/analytics/adobe-analytics.js +17 -17
  21. package/dist/vendors/analytics/ahrefs-analytics.js +8 -8
  22. package/dist/vendors/analytics/amplitude.js +39 -39
  23. package/dist/vendors/analytics/clearbit.js +11 -11
  24. package/dist/vendors/analytics/cloudflare-web-analytics.js +13 -13
  25. package/dist/vendors/analytics/databuddy.js +45 -45
  26. package/dist/vendors/analytics/fathom-analytics.js +15 -15
  27. package/dist/vendors/analytics/google-tag.js +23 -23
  28. package/dist/vendors/analytics/heap.js +37 -37
  29. package/dist/vendors/analytics/hightouch.js +30 -30
  30. package/dist/vendors/analytics/hotjar.js +14 -14
  31. package/dist/vendors/analytics/logrocket.js +24 -24
  32. package/dist/vendors/analytics/matomo-analytics.js +51 -51
  33. package/dist/vendors/analytics/microsoft-clarity.js +31 -31
  34. package/dist/vendors/analytics/mixpanel-analytics.js +31 -31
  35. package/dist/vendors/analytics/pirsch.js +27 -27
  36. package/dist/vendors/analytics/plausible-analytics.js +24 -24
  37. package/dist/vendors/analytics/posthog.js +84 -79
  38. package/dist/vendors/analytics/promptwatch.js +8 -8
  39. package/dist/vendors/analytics/rudderstack.js +50 -50
  40. package/dist/vendors/analytics/rybbit-analytics.js +30 -30
  41. package/dist/vendors/analytics/segment.js +16 -16
  42. package/dist/vendors/analytics/umami-analytics.js +16 -16
  43. package/dist/vendors/analytics/vercel-analytics.js +22 -22
  44. package/dist/vendors/functional/crisp.js +49 -51
  45. package/dist/vendors/functional/intercom.js +18 -18
  46. package/dist/vendors/tag-managers/google-tag-manager.js +20 -20
  47. package/dist-types/__tests__/helpers.d.ts +10 -10
  48. package/dist-types/engine/compile.d.ts +2 -2
  49. package/dist-types/engine/runtime.d.ts +3 -3
  50. package/dist-types/registry.d.ts +182 -173
  51. package/dist-types/resolve.d.ts +2 -2
  52. package/dist-types/types.d.ts +2 -2
  53. package/dist-types/vendors/_shared/attributes.d.ts +2 -2
  54. package/dist-types/vendors/_shared/google-consent.d.ts +2 -2
  55. package/dist-types/vendors/_shared/install-builders.d.ts +1 -1
  56. package/dist-types/vendors/_shared/script-url.d.ts +6 -6
  57. package/dist-types/vendors/ads-and-pixels/linkedin-insights.d.ts +14 -14
  58. package/dist-types/vendors/ads-and-pixels/meta-pixel.d.ts +27 -27
  59. package/dist-types/vendors/ads-and-pixels/microsoft-uet.d.ts +40 -40
  60. package/dist-types/vendors/ads-and-pixels/openai-pixel.d.ts +211 -0
  61. package/dist-types/vendors/ads-and-pixels/reddit-pixel.d.ts +28 -29
  62. package/dist-types/vendors/ads-and-pixels/snapchat-pixel.d.ts +23 -23
  63. package/dist-types/vendors/ads-and-pixels/tiktok-pixel.d.ts +22 -22
  64. package/dist-types/vendors/ads-and-pixels/x-pixel.d.ts +15 -15
  65. package/dist-types/vendors/analytics/adobe-analytics.d.ts +3 -3
  66. package/dist-types/vendors/analytics/ahrefs-analytics.d.ts +5 -5
  67. package/dist-types/vendors/analytics/amplitude.d.ts +24 -24
  68. package/dist-types/vendors/analytics/clearbit.d.ts +5 -5
  69. package/dist-types/vendors/analytics/cloudflare-web-analytics.d.ts +6 -6
  70. package/dist-types/vendors/analytics/databuddy.d.ts +34 -31
  71. package/dist-types/vendors/analytics/fathom-analytics.d.ts +8 -8
  72. package/dist-types/vendors/analytics/google-tag.d.ts +17 -17
  73. package/dist-types/vendors/analytics/heap.d.ts +17 -17
  74. package/dist-types/vendors/analytics/hightouch.d.ts +15 -15
  75. package/dist-types/vendors/analytics/hotjar.d.ts +9 -9
  76. package/dist-types/vendors/analytics/logrocket.d.ts +11 -11
  77. package/dist-types/vendors/analytics/matomo-analytics.d.ts +3 -3
  78. package/dist-types/vendors/analytics/microsoft-clarity.d.ts +12 -13
  79. package/dist-types/vendors/analytics/mixpanel-analytics.d.ts +20 -20
  80. package/dist-types/vendors/analytics/pirsch.d.ts +10 -10
  81. package/dist-types/vendors/analytics/plausible-analytics.d.ts +13 -13
  82. package/dist-types/vendors/analytics/posthog.d.ts +35 -32
  83. package/dist-types/vendors/analytics/promptwatch.d.ts +5 -5
  84. package/dist-types/vendors/analytics/rudderstack.d.ts +16 -16
  85. package/dist-types/vendors/analytics/rybbit-analytics.d.ts +15 -15
  86. package/dist-types/vendors/analytics/segment.d.ts +11 -11
  87. package/dist-types/vendors/analytics/umami-analytics.d.ts +9 -9
  88. package/dist-types/vendors/analytics/vercel-analytics.d.ts +13 -13
  89. package/dist-types/vendors/functional/crisp.d.ts +9 -9
  90. package/dist-types/vendors/functional/intercom.d.ts +12 -12
  91. package/dist-types/vendors/tag-managers/google-tag-manager.d.ts +16 -16
  92. package/docs/README.md +74 -48
  93. package/docs/assets/v3/brand-bar.png +0 -0
  94. package/docs/assets/v3/brand-card.png +0 -0
  95. package/docs/assets/v3/choice-wall.png +0 -0
  96. package/docs/assets/v3/mobile-card.png +0 -0
  97. package/docs/assets/v3/preferences.png +0 -0
  98. package/docs/customization/overview.md +45 -0
  99. package/docs/customization/recipes.md +79 -0
  100. package/docs/customization/slots.md +55 -0
  101. package/docs/customization/tokens.md +76 -0
  102. package/docs/customization/translations.md +49 -0
  103. package/docs/frameworks/javascript/script-loader.md +30 -339
  104. package/docs/frameworks/next/script-loader.md +134 -467
  105. package/docs/frameworks/react/script-loader.md +35 -535
  106. package/docs/guides/consent-state.md +60 -0
  107. package/docs/guides/data-fetching.md +163 -0
  108. package/docs/guides/deployment-modes.md +63 -0
  109. package/docs/guides/troubleshooting.md +68 -0
  110. package/docs/guides/verify-consent.md +62 -0
  111. package/docs/integrations/adobe-analytics.md +239 -105
  112. package/docs/integrations/ahrefs-analytics.md +238 -104
  113. package/docs/integrations/amplitude.md +219 -157
  114. package/docs/integrations/building-integrations.md +32 -224
  115. package/docs/integrations/clearbit.md +247 -86
  116. package/docs/integrations/cloudflare-web-analytics.md +250 -84
  117. package/docs/integrations/crisp.md +251 -97
  118. package/docs/integrations/databuddy.md +259 -153
  119. package/docs/integrations/fathom-analytics.md +239 -96
  120. package/docs/integrations/google-maps.md +328 -207
  121. package/docs/integrations/google-tag-manager.md +248 -96
  122. package/docs/integrations/google-tag.md +261 -90
  123. package/docs/integrations/heap.md +222 -149
  124. package/docs/integrations/hightouch.md +225 -131
  125. package/docs/integrations/hotjar.md +239 -90
  126. package/docs/integrations/intercom.md +239 -98
  127. package/docs/integrations/linkedin-insights.md +243 -113
  128. package/docs/integrations/logrocket.md +241 -123
  129. package/docs/integrations/matomo-analytics.md +256 -111
  130. package/docs/integrations/meta-pixel.md +197 -324
  131. package/docs/integrations/microsoft-clarity.md +233 -114
  132. package/docs/integrations/microsoft-uet.md +245 -110
  133. package/docs/integrations/mixpanel-analytics.md +252 -87
  134. package/docs/integrations/openai-pixel.md +441 -0
  135. package/docs/integrations/overview.md +95 -133
  136. package/docs/integrations/pirsch.md +249 -96
  137. package/docs/integrations/plausible-analytics.md +241 -100
  138. package/docs/integrations/posthog.md +353 -214
  139. package/docs/integrations/promptwatch.md +251 -81
  140. package/docs/integrations/reddit-pixel.md +226 -173
  141. package/docs/integrations/rudderstack.md +244 -187
  142. package/docs/integrations/rybbit-analytics.md +244 -91
  143. package/docs/integrations/segment.md +238 -92
  144. package/docs/integrations/snapchat-pixel.md +240 -110
  145. package/docs/integrations/tiktok-pixel.md +249 -81
  146. package/docs/integrations/umami-analytics.md +242 -95
  147. package/docs/integrations/vercel-analytics.md +242 -90
  148. package/docs/integrations/x-pixel.md +238 -104
  149. package/docs/integrations/youtube.md +354 -142
  150. package/docs/upgrade-v3.md +334 -0
  151. package/package.json +85 -78
  152. package/readme.json +2 -2
  153. package/dist/e2e-test-utils.cjs +0 -166
  154. package/dist/engine/compile.cjs +0 -130
  155. package/dist/engine/runtime.cjs +0 -475
  156. package/dist/registry.cjs +0 -423
  157. package/dist/resolve.cjs +0 -71
  158. package/dist/types.cjs +0 -69
  159. package/dist/vendors/_shared/attributes.cjs +0 -55
  160. package/dist/vendors/_shared/google-consent.cjs +0 -69
  161. package/dist/vendors/_shared/install-builders.cjs +0 -59
  162. package/dist/vendors/_shared/script-url.cjs +0 -78
  163. package/dist/vendors/ads-and-pixels/linkedin-insights.cjs +0 -89
  164. package/dist/vendors/ads-and-pixels/meta-pixel.cjs +0 -206
  165. package/dist/vendors/ads-and-pixels/microsoft-uet.cjs +0 -151
  166. package/dist/vendors/ads-and-pixels/reddit-pixel.cjs +0 -151
  167. package/dist/vendors/ads-and-pixels/snapchat-pixel.cjs +0 -131
  168. package/dist/vendors/ads-and-pixels/tiktok-pixel.cjs +0 -130
  169. package/dist/vendors/ads-and-pixels/x-pixel.cjs +0 -92
  170. package/dist/vendors/analytics/adobe-analytics.cjs +0 -90
  171. package/dist/vendors/analytics/ahrefs-analytics.cjs +0 -68
  172. package/dist/vendors/analytics/amplitude.cjs +0 -193
  173. package/dist/vendors/analytics/clearbit.cjs +0 -69
  174. package/dist/vendors/analytics/cloudflare-web-analytics.cjs +0 -73
  175. package/dist/vendors/analytics/databuddy.cjs +0 -144
  176. package/dist/vendors/analytics/fathom-analytics.cjs +0 -76
  177. package/dist/vendors/analytics/google-tag.cjs +0 -107
  178. package/dist/vendors/analytics/heap.cjs +0 -181
  179. package/dist/vendors/analytics/hightouch.cjs +0 -153
  180. package/dist/vendors/analytics/hotjar.cjs +0 -85
  181. package/dist/vendors/analytics/logrocket.cjs +0 -99
  182. package/dist/vendors/analytics/matomo-analytics.cjs +0 -232
  183. package/dist/vendors/analytics/microsoft-clarity.cjs +0 -138
  184. package/dist/vendors/analytics/mixpanel-analytics.cjs +0 -134
  185. package/dist/vendors/analytics/pirsch.cjs +0 -108
  186. package/dist/vendors/analytics/plausible-analytics.cjs +0 -122
  187. package/dist/vendors/analytics/posthog.cjs +0 -236
  188. package/dist/vendors/analytics/promptwatch.cjs +0 -70
  189. package/dist/vendors/analytics/rudderstack.cjs +0 -227
  190. package/dist/vendors/analytics/rybbit-analytics.cjs +0 -104
  191. package/dist/vendors/analytics/segment.cjs +0 -97
  192. package/dist/vendors/analytics/umami-analytics.cjs +0 -80
  193. package/dist/vendors/analytics/vercel-analytics.cjs +0 -94
  194. package/dist/vendors/functional/crisp.cjs +0 -143
  195. package/dist/vendors/functional/intercom.cjs +0 -89
  196. package/dist/vendors/tag-managers/google-tag-manager.cjs +0 -100
  197. package/docs/shared/react/guides/script-loader.md +0 -311
@@ -1,100 +0,0 @@
1
- "use strict";
2
- var __webpack_require__ = {};
3
- (()=>{
4
- __webpack_require__.d = (exports1, getters, values)=>{
5
- var define = (defs, kind)=>{
6
- for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
- enumerable: true,
8
- [kind]: defs[key]
9
- });
10
- };
11
- define(getters, "get");
12
- define(values, "value");
13
- };
14
- })();
15
- (()=>{
16
- __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
- })();
18
- (()=>{
19
- __webpack_require__.r = (exports1)=>{
20
- if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
- value: 'Module'
22
- });
23
- Object.defineProperty(exports1, '__esModule', {
24
- value: true
25
- });
26
- };
27
- })();
28
- var __webpack_exports__ = {};
29
- __webpack_require__.r(__webpack_exports__);
30
- __webpack_require__.d(__webpack_exports__, {
31
- googleTagManager: ()=>googleTagManager,
32
- googleTagManagerManifest: ()=>googleTagManagerManifest
33
- });
34
- const external_resolve_cjs_namespaceObject = require("../../resolve.cjs");
35
- const external_types_cjs_namespaceObject = require("../../types.cjs");
36
- const google_consent_cjs_namespaceObject = require("../_shared/google-consent.cjs");
37
- const googleTagManagerManifest = {
38
- ...external_types_cjs_namespaceObject.vendorManifestContract,
39
- vendor: 'google-tag-manager',
40
- category: 'necessary',
41
- alwaysLoad: true,
42
- bootstrap: [
43
- {
44
- type: 'setGlobal',
45
- name: 'dataLayer',
46
- value: [],
47
- ifUndefined: true
48
- },
49
- {
50
- type: 'defineQueueFunction',
51
- name: 'gtag',
52
- queue: 'dataLayer',
53
- ifUndefined: true
54
- }
55
- ],
56
- install: [
57
- {
58
- type: 'pushToQueue',
59
- queue: 'dataLayer',
60
- value: {
61
- 'gtm.start': external_types_cjs_namespaceObject.runtimeTimestampValue,
62
- event: 'gtm.js'
63
- }
64
- },
65
- {
66
- type: 'loadScript',
67
- src: 'https://www.googletagmanager.com/gtm.js?id={{id}}',
68
- async: true
69
- }
70
- ],
71
- onConsentChange: [
72
- {
73
- type: 'callGlobal',
74
- global: 'gtag',
75
- args: [
76
- 'event',
77
- '{{updateEventName}}'
78
- ]
79
- }
80
- ],
81
- consentMapping: google_consent_cjs_namespaceObject.GOOGLE_CONSENT_MODE_V2_DEFAULT_MAPPING,
82
- consentSignal: 'gtag'
83
- };
84
- function googleTagManager({ id, updateEventName, consentMapping }) {
85
- const manifest = (0, google_consent_cjs_namespaceObject.withOptionalConsentMapping)(googleTagManagerManifest, consentMapping);
86
- const resolved = (0, external_resolve_cjs_namespaceObject.resolveManifest)(manifest, {
87
- id,
88
- updateEventName: updateEventName ?? 'consent-update'
89
- });
90
- return resolved;
91
- }
92
- exports.googleTagManager = __webpack_exports__.googleTagManager;
93
- exports.googleTagManagerManifest = __webpack_exports__.googleTagManagerManifest;
94
- for(var __rspack_i in __webpack_exports__)if (-1 === [
95
- "googleTagManager",
96
- "googleTagManagerManifest"
97
- ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
98
- Object.defineProperty(exports, '__esModule', {
99
- value: true
100
- });
@@ -1,311 +0,0 @@
1
- ---
2
- title: Script Loader
3
- description: Reference page for script loader.
4
- group: reference
5
- ---
6
- The script loader manages third-party JavaScript based on consent state. You declare scripts in your provider's `scripts` option, and c15t decides when each script should load, stay loaded, unload, or receive a consent update.
7
-
8
- Use it for analytics, pixels, tag managers, product analytics, and other vendor snippets that should not run until the right consent condition is satisfied. Prebuilt helpers live in [`@c15t/scripts`](/docs/integrations/overview); custom scripts can be declared directly when the vendor is specific to your app.
9
-
10
- <PackageCommandTabs mode="install" command="@c15t/scripts" />
11
-
12
- > ℹ️ **Info:**
13
- > Start with the integrations overview before writing your own script. Built-in helpers encode vendor boot order, consent updates, and common defaults so you do not have to.
14
- >
15
- > 📝 **Note:**
16
- > If you need a vendor c15t does not ship yet, see the custom integration guide. It explains when a one-off Script is enough and when to build a reusable manifest-backed helper.
17
- >
18
- > ℹ️ **Info:**
19
- > The script loader handles JavaScript tags and callback lifecycles. For iframe-only embeds, use the iframe blocking pattern. For UI components such as maps or video players, combine consent state with a component-level placeholder or a dedicated renderable integration.
20
-
21
- ## Mental Model
22
-
23
- Every script you register has the same lifecycle. c15t evaluates each script against the current consent state, then drives it through a small number of states:
24
-
25
- 1. **Pending** — registered but waiting for consent. Nothing is in the DOM yet.
26
- 2. **Loaded** — consent matched, c15t injected the script (or ran callbacks for callback-only scripts).
27
- 3. **Updated** — already loaded, consent state changed, `onConsentChange` ran so the SDK can react.
28
- 4. **Unloaded** — consent was revoked. c15t removed the script element unless you opted into persistence.
29
-
30
- Four lifecycle callbacks let you hook into transitions: `onBeforeLoad`, `onLoad`, `onConsentChange`, and `onError`. Two flags — [`alwaysLoad`](#always-load) and [`persistAfterConsentRevoked`](#persist-after-revocation) — change how c15t treats consent boundaries. Everything else (DOM placement, ad-block evasion, dynamic management) is a refinement on top of this core model.
31
-
32
- ## Choose the Right Approach
33
-
34
- Most projects mix more than one style. Pick the smallest one that keeps consent behavior obvious:
35
-
36
- |Style|Use when|
37
- |--|--|
38
- |**Built-in helper** from `@c15t/scripts`|c15t already ships the vendor. See the [integrations overview](/docs/integrations/overview).|
39
- |**Plain `Script`**|One-off app code with simple load and callback behavior.|
40
- |**Callback-only `Script`**|Another package already loaded the SDK; c15t only synchronizes consent.|
41
- |**Manifest-backed helper**|Reusable vendor integration with structured setup phases, queues, stubs, or a vendor consent API.|
42
- |**Iframe / renderable integration**|Vendor exposes an iframe or React component, not just a `<script>` tag.|
43
-
44
- ## Script Types
45
-
46
- ### Standard Scripts
47
-
48
- Standard scripts load an external JavaScript file via a `<script>` tag. This is the default for most analytics and pixel SDKs:
49
-
50
- ```tsx
51
- {
52
- id: 'analytics',
53
- src: 'https://cdn.example.com/analytics.js',
54
- category: 'measurement',
55
- }
56
- ```
57
-
58
- ### Inline Scripts
59
-
60
- Inline scripts execute JavaScript from `textContent` instead of loading a URL. Use these sparingly; a manifest-backed helper is usually better for reusable vendor code.
61
-
62
- ```tsx
63
- {
64
- id: 'gtag-config',
65
- textContent: `
66
- window.dataLayer = window.dataLayer || [];
67
- function gtag(){dataLayer.push(arguments);}
68
- gtag('js', new Date());
69
- gtag('config', 'G-XXXXXX');
70
- `,
71
- category: 'measurement',
72
- }
73
- ```
74
-
75
- ### Callback-Only Scripts
76
-
77
- Callback-only scripts do not inject a script tag. They run lifecycle callbacks when consent allows them to. Use this when another package has already loaded the SDK and c15t only needs to drive consent:
78
-
79
- ```tsx
80
- {
81
- id: 'posthog-consent',
82
- callbackOnly: true,
83
- category: 'measurement',
84
- onLoad: ({ hasConsent }) => {
85
- if (hasConsent) {
86
- posthog.opt_in_capturing();
87
- }
88
- },
89
- onConsentChange: ({ hasConsent }) => {
90
- if (hasConsent) {
91
- posthog.opt_in_capturing();
92
- } else {
93
- posthog.opt_out_capturing();
94
- }
95
- },
96
- }
97
- ```
98
-
99
- ### Manifest-Backed Helpers
100
-
101
- Built-in integrations in `@c15t/scripts` are manifest-backed. A manifest describes vendor setup as structured phases, then c15t compiles it into a `Script`. Manifests keep queue stubs, script URLs, consent signaling, and post-load work consistent across apps and they are safe to ship from a server.
102
-
103
- Use a manifest-backed helper when:
104
-
105
- * the integration should be reused across projects,
106
- * the vendor snippet has ordered setup steps,
107
- * the vendor exposes a consent API,
108
- * or you plan to contribute the integration back to c15t.
109
-
110
- Read the [custom integration guide](/docs/integrations/building-integrations) for the manifest contract, phases, and testing checklist.
111
-
112
- ### Iframe And Renderable Integrations
113
-
114
- Some vendors are not just script tags. YouTube embeds, maps, calendars, and checkout widgets often need a visible component, a placeholder, or an iframe.
115
-
116
- * For iframe-only embeds, gate the iframe `src` with the [iframe blocking](/docs/frameworks/react/iframe-blocking) pattern instead of loading a script just to hide an iframe.
117
- * For SDK-backed UI, use the script loader for the shared SDK and render the component only when consent and SDK readiness agree.
118
- * Use `YouTubeEmbed` for the iframe-only YouTube candidate and `GoogleMap` for the callback-based SDK candidate.
119
- * Use `useConsentScript()` when building custom wrappers. It registers scripts through the consent store, follows `loadedScripts`, and returns a promise-shaped readiness contract for callback-based SDKs.
120
-
121
- ## Lifecycle Callbacks
122
-
123
- Every script supports four callbacks. Each receives a `ScriptCallbackInfo` payload (id, element, hasConsent, consents):
124
-
125
- * `onBeforeLoad` — runs before the script tag is injected. Create globals, queues, or vendor stubs here.
126
- * `onLoad` — runs after the browser loads the script. Call vendor `init()` APIs here.
127
- * `onConsentChange` — runs for loaded scripts when consent changes. Forward the new consent state to the vendor SDK.
128
- * `onError` — runs when the script fails to load. Record diagnostics or render a fallback.
129
-
130
- ```tsx
131
- {
132
- id: 'analytics',
133
- src: 'https://analytics.example.com/v2.js',
134
- category: 'measurement',
135
- onBeforeLoad: ({ id }) => {
136
- window.analyticsQueue = window.analyticsQueue || [];
137
- },
138
- onLoad: () => {
139
- window.analytics.init('my-key');
140
- },
141
- onError: ({ error }) => {
142
- console.error('Failed to load analytics:', error);
143
- },
144
- onConsentChange: ({ hasConsent }) => {
145
- window.analytics.setConsent(hasConsent);
146
- },
147
- }
148
- ```
149
-
150
- ## Consent Conditions
151
-
152
- The `category` field accepts a `HasCondition`. It can be a single consent category or a logical expression:
153
-
154
- ```tsx
155
- // Simple: requires measurement consent
156
- { category: 'measurement' }
157
-
158
- // AND: requires both measurement and marketing
159
- { category: { and: ['measurement', 'marketing'] } }
160
-
161
- // OR: requires either measurement or marketing
162
- { category: { or: ['measurement', 'marketing'] } }
163
- ```
164
-
165
- Consent categories use the same names as the rest of c15t (`necessary`, `functionality`, `experience`, `measurement`, `marketing`).
166
-
167
- ## Persistence Options
168
-
169
- ### Always Load
170
-
171
- `alwaysLoad` loads the script regardless of whether its category is currently granted. Use it only when the vendor must be present early **and** has a reliable consent API of its own — Google Tag Manager with Consent Mode is the canonical example.
172
-
173
- ```tsx
174
- {
175
- id: 'google-tag-manager',
176
- src: 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXX',
177
- category: 'measurement',
178
- alwaysLoad: true,
179
- }
180
- ```
181
-
182
- When `alwaysLoad` is on, `onConsentChange` becomes mandatory: it is how the loaded SDK learns about every transition.
183
-
184
- > ⚠️ **Warning:**
185
- > alwaysLoad shifts compliance responsibility to the vendor integration. Make sure the script receives denied-by-default consent signals before it can track.
186
-
187
- ### Persist After Revocation
188
-
189
- `persistAfterConsentRevoked` keeps a script in the page after consent is revoked instead of unloading it. Use it only when the vendor exposes a runtime consent toggle — otherwise unloading is safer because removing the element guarantees the SDK stops.
190
-
191
- ```tsx
192
- {
193
- id: 'error-tracking',
194
- src: 'https://errors.example.com/track.js',
195
- category: 'measurement',
196
- persistAfterConsentRevoked: true,
197
- onConsentChange: ({ hasConsent }) => {
198
- window.ErrorTracker.setConsent(hasConsent);
199
- },
200
- }
201
- ```
202
-
203
- As with `alwaysLoad`, `onConsentChange` is how the persisted SDK learns about consent updates.
204
-
205
- ### `alwaysLoad` vs `persistAfterConsentRevoked`
206
-
207
- These two flags answer different questions. Use this table to keep them straight:
208
-
209
- |Question|`alwaysLoad`|`persistAfterConsentRevoked`|
210
- |--|--|--|
211
- |Loads before consent is granted?|Yes|No (waits for consent like a normal script)|
212
- |Stays loaded after consent is revoked?|Yes|Yes|
213
- |Requires a vendor consent API?|Yes|Yes|
214
-
215
- ## DOM Placement
216
-
217
- Control where the script is injected and whether the element id is anonymized:
218
-
219
- ```tsx
220
- {
221
- id: 'widget',
222
- src: 'https://widget.example.com/embed.js',
223
- category: 'experience',
224
- target: 'body', // 'head' (default) or 'body'
225
- anonymizeId: true, // default: true, hides the c15t script id from ad blockers
226
- nonce: 'abc123', // optional CSP nonce
227
- }
228
- ```
229
-
230
- Set `anonymizeId: false` only when another script or test needs a stable DOM id. Pass `nonce` when your CSP requires it; c15t applies it directly to the generated `<script>` element.
231
-
232
- You usually do not need a per-script `nonce`. Setting `nonce` once on the provider covers every injected script (and the theme stylesheet); a per-script value overrides it for that script alone.
233
-
234
- ## Dynamic Management
235
-
236
- Framework packages expose script-manager methods so integrations can be added, removed, or inspected at runtime. Use this for tenant-specific tools, feature-flagged scripts, or vendors that are configured after sign-in:
237
-
238
- * `setScripts(scripts)` — registers script definitions and immediately evaluates them against consent.
239
- * `removeScript(id)` — removes a definition and unloads its element if needed.
240
- * `isScriptLoaded(id)` — returns whether c15t has loaded a script.
241
- * `getLoadedScriptIds()` — returns every currently loaded script id.
242
-
243
- Dynamic scripts should still use stable ids. If the same vendor is added repeatedly with different ids, c15t treats each call as a new script.
244
-
245
- ## Calling Vendor APIs From Your App
246
-
247
- The script loader controls **when the vendor SDK loads**. It does not intercept calls your application code makes to that SDK afterwards. Whether your event calls are safe before consent is granted depends on the script's persistence flags:
248
-
249
- |Vendor pattern|What c15t does|What your app code must do|
250
- |--|--|--|
251
- |Consent-gated load, unloaded on revoke (e.g. cookieless analytics)|Script not in DOM until consent granted; removed on revoke. Global is `undefined` outside that window.|**Guard every call.** Unguarded `window.vendor.track(...)` throws when the global is absent.|
252
- |Consent-gated load with `persistAfterConsentRevoked` (e.g. Meta Pixel)|Script not in DOM until consent granted; stays after revoke. c15t calls vendor's consent-revoke API on revocation.|Guard calls only for the pre-initial-consent window. Once loaded, the SDK handles its own suppression.|
253
- |`alwaysLoad: true` with a vendor consent API (e.g. GTM, gtag, Databuddy, PostHog)|Script in DOM on page start; c15t signals consent state through the vendor's API.|Calls are safe — the vendor SDK suppresses transmission when consent is denied.|
254
- |No app-facing API (e.g. Cloudflare Web Analytics)|Script in/out of DOM based on consent. Tracking is fully automatic.|Nothing to guard.|
255
-
256
- The safe pattern in React is to read consent state through `useConsentManager().has(category)` before calling the SDK:
257
-
258
- ```tsx
259
- import { useCallback } from 'react';
260
- import { useConsentManager } from '@c15t/react';
261
-
262
- function useTrackSignup() {
263
- const { has } = useConsentManager();
264
-
265
- return useCallback(() => {
266
- if (has('measurement')) {
267
- window.fathom?.trackEvent('signup');
268
- }
269
- }, [has]);
270
- }
271
-
272
- function SignupButton() {
273
- const trackSignup = useTrackSignup();
274
-
275
- return <button onClick={trackSignup}>Sign up</button>;
276
- }
277
- ```
278
-
279
- From non-React code, read the consent store directly:
280
-
281
- ```ts
282
- import { getOrCreateConsentRuntime } from 'c15t';
283
-
284
- const { consentStore } = getOrCreateConsentRuntime();
285
-
286
- if (consentStore.getState().has('measurement')) {
287
- window.fathom?.trackEvent('signup');
288
- }
289
- ```
290
-
291
- Each [integration page](/docs/integrations/overview) includes a vendor-specific **Tracking events in your app** block that names which pattern applies.
292
-
293
- ## Debugging Checklist
294
-
295
- When a script does not behave as expected:
296
-
297
- 1. Confirm the script's `category` matches the consent that has been granted.
298
- 2. Check whether the script is `alwaysLoad` or consent-gated.
299
- 3. Confirm `onBeforeLoad` creates any globals before the vendor code reads them.
300
- 4. Confirm `onConsentChange` updates persisted or always-loaded scripts when consent changes.
301
- 5. Check whether the browser or an ad blocker blocked the request.
302
- 6. Use c15t devtools to inspect script lifecycle events when available.
303
-
304
- ## API Reference
305
-
306
- |Property|Value|
307
- |:--|:--|
308
- |Type Name|\`Script\`|
309
- |Source Path|\`./packages/core/src/libs/script-loader/types.ts\`|
310
-
311
- \*ExtractedTypeTable: Could not extract "Script" from "./packages/core/src/libs/script-loader/types.ts" using base path "/home/runner/work/c15t/c15t". Verify the path/name and that the file is included by your tsconfig.\*