c15t 2.0.4 → 2.1.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 (34) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/index.cjs +2 -2
  3. package/dist/index.js +2 -2
  4. package/dist-types/index.d.ts +1 -1
  5. package/dist-types/version.d.ts +1 -1
  6. package/docs/integrations/ahrefs-analytics.md +224 -0
  7. package/docs/integrations/cloudflare-web-analytics.md +194 -0
  8. package/docs/integrations/crisp.md +214 -0
  9. package/docs/integrations/databuddy.md +136 -65
  10. package/docs/integrations/fathom-analytics.md +221 -0
  11. package/docs/integrations/google-tag-manager.md +84 -15
  12. package/docs/integrations/google-tag.md +89 -8
  13. package/docs/integrations/hotjar.md +211 -0
  14. package/docs/integrations/intercom.md +214 -0
  15. package/docs/integrations/linkedin-insights.md +130 -11
  16. package/docs/integrations/matomo-analytics.md +246 -0
  17. package/docs/integrations/meta-pixel.md +377 -24
  18. package/docs/integrations/microsoft-clarity.md +241 -0
  19. package/docs/integrations/microsoft-uet.md +120 -9
  20. package/docs/integrations/mixpanel-analytics.md +198 -0
  21. package/docs/integrations/overview.md +69 -74
  22. package/docs/integrations/plausible-analytics.md +237 -0
  23. package/docs/integrations/posthog.md +172 -41
  24. package/docs/integrations/promptwatch.md +187 -0
  25. package/docs/integrations/reddit-pixel.md +336 -0
  26. package/docs/integrations/rybbit-analytics.md +222 -0
  27. package/docs/integrations/segment.md +213 -0
  28. package/docs/integrations/snapchat-pixel.md +244 -0
  29. package/docs/integrations/tiktok-pixel.md +88 -10
  30. package/docs/integrations/umami-analytics.md +220 -0
  31. package/docs/integrations/vercel-analytics.md +213 -0
  32. package/docs/integrations/x-pixel.md +99 -10
  33. package/docs/script-loader.md +168 -51
  34. package/package.json +3 -3
@@ -2,9 +2,9 @@
2
2
  title: Script Loader
3
3
  description: Gate third-party scripts behind consent — load Google Analytics, Meta Pixel, and other tracking scripts only when users grant permission.
4
4
  ---
5
- The script loader manages third-party scripts based on consent state. Scripts are defined in the provider's `scripts` option and are automatically loaded when their required consent category is granted, and unloaded when consent is revoked.
5
+ 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.
6
6
 
7
- c15t has a collection of premade scripts available in `@c15t/scripts`. Check the [integrations overview](/docs/integrations/overview) first before manually building a script.
7
+ 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.
8
8
 
9
9
  |Package manager|Command|
10
10
  |:--|:--|
@@ -14,13 +14,13 @@ c15t has a collection of premade scripts available in `@c15t/scripts`. Check the
14
14
  |bun|`bun add @c15t/scripts`|
15
15
 
16
16
  > ℹ️ **Info:**
17
- > We recommend using the pre-built integrations when possible.
17
+ > 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.
18
18
  >
19
19
  > ℹ️ **Info:**
20
- > If you need a vendor we do not ship yet, see the custom integration guide. It covers both one-off Script objects and reusable manifest-backed integrations.
20
+ > 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.
21
21
  >
22
22
  > ℹ️ **Info:**
23
- > For app-specific scripts, use a plain Script object. For reusable integrations, prefer a manifest-backed helper so startup phases, consent signaling, and future server-side loading support stay structured.
23
+ > 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.
24
24
 
25
25
  ## Basic Usage
26
26
 
@@ -44,30 +44,46 @@ const { consentStore } = getOrCreateConsentRuntime({
44
44
  });
45
45
  ```
46
46
 
47
- ## Choose the Right Approach
47
+ ## Mental Model
48
48
 
49
- * Use a plain `Script` for one-off app code.
50
- * Use a manifest-backed helper in `@c15t/scripts` for reusable integrations, contributions, or anything that needs structured startup behavior.
49
+ 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:
51
50
 
52
- If you are building something reusable, start with the [custom integration guide](/docs/integrations/building-integrations) before using raw callbacks.
51
+ 1. **Pending** registered but waiting for consent. Nothing is in the DOM yet.
52
+ 2. **Loaded** — consent matched, c15t injected the script (or ran callbacks for callback-only scripts).
53
+ 3. **Updated** — already loaded, consent state changed, `onConsentChange` ran so the SDK can react.
54
+ 4. **Unloaded** — consent was revoked. c15t removed the script element unless you opted into persistence.
53
55
 
54
- ## Reusable Integrations
56
+ 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.
55
57
 
56
- For app-specific use, raw `Script` objects are usually enough.
58
+ ## Choose the Right Approach
57
59
 
58
- For reusable integrations, c15t uses a manifest-backed model in `@c15t/scripts`. That keeps startup phases, consent signaling, and vendor-specific boot logic structured instead of hidden inside large callback bodies.
60
+ Most projects mix more than one style. Pick the smallest one that keeps consent behavior obvious:
59
61
 
60
- If you are building an integration for multiple apps or contributing upstream, use the [custom integration guide](/docs/integrations/building-integrations).
62
+ |Style|Use when|
63
+ |--|--|
64
+ |**Built-in helper** from `@c15t/scripts`|c15t already ships the vendor. See the [integrations overview](/docs/integrations/overview).|
65
+ |**Plain `Script`**|One-off app code with simple load and callback behavior.|
66
+ |**Callback-only `Script`**|Another package already loaded the SDK; c15t only synchronizes consent.|
67
+ |**Manifest-backed helper**|Reusable vendor integration with structured setup phases, queues, stubs, or a vendor consent API.|
68
+ |**Iframe / renderable integration**|Vendor exposes an iframe or React component, not just a `<script>` tag.|
61
69
 
62
70
  ## Script Types
63
71
 
64
72
  ### Standard Scripts
65
73
 
66
- Load an external JavaScript file via a `<script>` tag. Use `src` to specify the URL.
74
+ Standard scripts load an external JavaScript file via a `<script>` tag. This is the default for most analytics and pixel SDKs:
75
+
76
+ ```tsx
77
+ {
78
+ id: 'analytics',
79
+ src: 'https://cdn.example.com/analytics.js',
80
+ category: 'measurement',
81
+ }
82
+ ```
67
83
 
68
84
  ### Inline Scripts
69
85
 
70
- Execute inline JavaScript code. Use `textContent` instead of `src`:
86
+ 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.
71
87
 
72
88
  ```tsx
73
89
  {
@@ -84,7 +100,7 @@ Execute inline JavaScript code. Use `textContent` instead of `src`:
84
100
 
85
101
  ### Callback-Only Scripts
86
102
 
87
- Don't inject any `<script>` tag - just execute callbacks based on consent changes. Useful for controlling libraries that are already loaded:
103
+ 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:
88
104
 
89
105
  ```tsx
90
106
  {
@@ -106,31 +122,34 @@ Don't inject any `<script>` tag - just execute callbacks based on consent change
106
122
  }
107
123
  ```
108
124
 
109
- ## Consent Conditions
125
+ ### Manifest-Backed Helpers
110
126
 
111
- The `category` field accepts a `HasCondition` - either a simple string or a logical expression:
127
+ 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.
112
128
 
113
- ```tsx
114
- // Simple: requires measurement consent
115
- { category: 'measurement' }
129
+ Use a manifest-backed helper when:
116
130
 
117
- // AND: requires both measurement and marketing
118
- { category: { and: ['measurement', 'marketing'] } }
131
+ * the integration should be reused across projects,
132
+ * the vendor snippet has ordered setup steps,
133
+ * the vendor exposes a consent API,
134
+ * or you plan to contribute the integration back to c15t.
119
135
 
120
- // OR: requires either measurement or marketing
121
- { category: { or: ['measurement', 'marketing'] } }
122
- ```
136
+ Read the [custom integration guide](/docs/integrations/building-integrations) for the manifest contract, phases, and testing checklist.
123
137
 
124
- ## Script Callbacks
138
+ ### Iframe And Renderable Integrations
125
139
 
126
- Every script supports four lifecycle callbacks:
140
+ Some vendors are not just script tags. YouTube embeds, maps, calendars, and checkout widgets often need a visible component, a placeholder, or an iframe.
127
141
 
128
- |Callback|When|Use Case|
129
- |--|--|--|
130
- |`onBeforeLoad`|Before the script tag is injected|Set up global variables|
131
- |`onLoad`|Script loaded successfully|Initialize the library|
132
- |`onError`|Script failed to load|Log error, load fallback|
133
- |`onConsentChange`|Consent state changed (script already loaded)|Toggle tracking on/off|
142
+ * 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.
143
+ * For SDK-backed UI, use the script loader for the shared SDK and render the component only when consent and SDK readiness agree.
144
+
145
+ ## Lifecycle Callbacks
146
+
147
+ Every script supports four callbacks. Each receives a `ScriptCallbackInfo` payload (id, element, hasConsent, consents):
148
+
149
+ * `onBeforeLoad` — runs before the script tag is injected. Create globals, queues, or vendor stubs here.
150
+ * `onLoad` — runs after the browser loads the script. Call vendor `init()` APIs here.
151
+ * `onConsentChange` — runs for loaded scripts when consent changes. Forward the new consent state to the vendor SDK.
152
+ * `onError` — runs when the script fails to load. Record diagnostics or render a fallback.
134
153
 
135
154
  ```tsx
136
155
  {
@@ -138,38 +157,60 @@ Every script supports four lifecycle callbacks:
138
157
  src: 'https://analytics.example.com/v2.js',
139
158
  category: 'measurement',
140
159
  onBeforeLoad: ({ id }) => {
141
- console.log(`Loading script: ${id}`);
160
+ window.analyticsQueue = window.analyticsQueue || [];
142
161
  },
143
- onLoad: ({ element }) => {
162
+ onLoad: () => {
144
163
  window.analytics.init('my-key');
145
164
  },
146
165
  onError: ({ error }) => {
147
166
  console.error('Failed to load analytics:', error);
148
167
  },
149
- onConsentChange: ({ hasConsent, consents }) => {
168
+ onConsentChange: ({ hasConsent }) => {
150
169
  window.analytics.setConsent(hasConsent);
151
170
  },
152
171
  }
153
172
  ```
154
173
 
155
- ## Advanced Options
174
+ ## Consent Conditions
175
+
176
+ The `category` field accepts a `HasCondition`. It can be a single consent category or a logical expression:
177
+
178
+ ```tsx
179
+ // Simple: requires measurement consent
180
+ { category: 'measurement' }
181
+
182
+ // AND: requires both measurement and marketing
183
+ { category: { and: ['measurement', 'marketing'] } }
184
+
185
+ // OR: requires either measurement or marketing
186
+ { category: { or: ['measurement', 'marketing'] } }
187
+ ```
188
+
189
+ Consent categories use the same names as the rest of c15t (`necessary`, `functionality`, `experience`, `measurement`, `marketing`).
190
+
191
+ ## Persistence Options
156
192
 
157
193
  ### Always Load
158
194
 
159
- Scripts that manage their own consent internally (like GTM in consent mode):
195
+ `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.
160
196
 
161
197
  ```tsx
162
198
  {
163
199
  id: 'google-tag-manager',
164
200
  src: 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXX',
165
201
  category: 'measurement',
166
- alwaysLoad: true, // Loads regardless of consent state
202
+ alwaysLoad: true,
167
203
  }
168
204
  ```
169
205
 
206
+ When `alwaysLoad` is on, `onConsentChange` becomes mandatory: it is how the loaded SDK learns about every transition.
207
+
208
+ > ⚠️ **Warning:**
209
+ > alwaysLoad shifts compliance responsibility to the vendor integration. Make sure the script receives denied-by-default consent signals before it can track.
210
+
170
211
  ### Persist After Revocation
171
212
 
172
- Keep the script loaded even after consent is revoked (the page won't reload for this script):
213
+ `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.
173
214
 
174
215
  ```tsx
175
216
  {
@@ -177,35 +218,111 @@ Keep the script loaded even after consent is revoked (the page won't reload for
177
218
  src: 'https://errors.example.com/track.js',
178
219
  category: 'measurement',
179
220
  persistAfterConsentRevoked: true,
221
+ onConsentChange: ({ hasConsent }) => {
222
+ window.ErrorTracker.setConsent(hasConsent);
223
+ },
180
224
  }
181
225
  ```
182
226
 
183
- ### Script Placement
227
+ As with `alwaysLoad`, `onConsentChange` is how the persisted SDK learns about consent updates.
228
+
229
+ ### `alwaysLoad` vs `persistAfterConsentRevoked`
184
230
 
185
- Control where in the DOM the script is injected:
231
+ These two flags answer different questions. Use this table to keep them straight:
232
+
233
+ |Question|`alwaysLoad`|`persistAfterConsentRevoked`|
234
+ |--|--|--|
235
+ |Loads before consent is granted?|Yes|No (waits for consent like a normal script)|
236
+ |Stays loaded after consent is revoked?|Yes|Yes|
237
+ |Requires a vendor consent API?|Yes|Yes|
238
+
239
+ ## DOM Placement
240
+
241
+ Control where the script is injected and whether the element id is anonymized:
186
242
 
187
243
  ```tsx
188
244
  {
189
245
  id: 'widget',
190
246
  src: 'https://widget.example.com/embed.js',
191
247
  category: 'experience',
192
- target: 'body', // 'head' (default) or 'body'
248
+ target: 'body', // 'head' (default) or 'body'
249
+ anonymizeId: true, // default: true, hides the c15t script id from ad blockers
250
+ nonce: 'abc123', // optional CSP nonce
193
251
  }
194
252
  ```
195
253
 
196
- ### Ad Blocker Evasion
254
+ 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.
255
+
256
+ ## Dynamic Management
197
257
 
198
- Script element IDs are anonymized by default to avoid ad blocker pattern matching:
258
+ 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:
259
+
260
+ * `setScripts(scripts)` — registers script definitions and immediately evaluates them against consent.
261
+ * `removeScript(id)` — removes a definition and unloads its element if needed.
262
+ * `isScriptLoaded(id)` — returns whether c15t has loaded a script.
263
+ * `getLoadedScriptIds()` — returns every currently loaded script id.
264
+
265
+ 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.
266
+
267
+ ## Calling Vendor APIs From Your App
268
+
269
+ 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:
270
+
271
+ |Vendor pattern|What c15t does|What your app code must do|
272
+ |--|--|--|
273
+ |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.|
274
+ |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.|
275
+ |`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.|
276
+ |No app-facing API (e.g. Cloudflare Web Analytics)|Script in/out of DOM based on consent. Tracking is fully automatic.|Nothing to guard.|
277
+
278
+ The safe pattern in React is to read consent state through `useConsentManager().has(category)` before calling the SDK:
199
279
 
200
280
  ```tsx
201
- {
202
- id: 'analytics',
203
- src: '...',
204
- category: 'measurement',
205
- anonymizeId: true, // default: true
281
+ import { useCallback } from 'react';
282
+ import { useConsentManager } from '@c15t/react';
283
+
284
+ function useTrackSignup() {
285
+ const { has } = useConsentManager();
286
+
287
+ return useCallback(() => {
288
+ if (has('measurement')) {
289
+ window.fathom?.trackEvent('signup');
290
+ }
291
+ }, [has]);
292
+ }
293
+
294
+ function SignupButton() {
295
+ const trackSignup = useTrackSignup();
296
+
297
+ return <button onClick={trackSignup}>Sign up</button>;
206
298
  }
207
299
  ```
208
300
 
301
+ From non-React code, read the consent store directly:
302
+
303
+ ```ts
304
+ import { getOrCreateConsentRuntime } from 'c15t';
305
+
306
+ const { consentStore } = getOrCreateConsentRuntime();
307
+
308
+ if (consentStore.getState().has('measurement')) {
309
+ window.fathom?.trackEvent('signup');
310
+ }
311
+ ```
312
+
313
+ Each [integration page](/docs/integrations/overview) includes a vendor-specific **Tracking events in your app** block that names which pattern applies.
314
+
315
+ ## Debugging Checklist
316
+
317
+ When a script does not behave as expected:
318
+
319
+ 1. Confirm the script's `category` matches the consent that has been granted.
320
+ 2. Check whether the script is `alwaysLoad` or consent-gated.
321
+ 3. Confirm `onBeforeLoad` creates any globals before the vendor code reads them.
322
+ 4. Confirm `onConsentChange` updates persisted or always-loaded scripts when consent changes.
323
+ 5. Check whether the browser or an ad blocker blocked the request.
324
+ 6. Use c15t devtools to inspect script lifecycle events when available.
325
+
209
326
  ## Dynamic Script Management
210
327
 
211
328
  Add, remove, or check scripts at runtime via the store:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c15t",
3
- "version": "2.0.4",
3
+ "version": "2.1.0",
4
4
  "description": "Headless cookie banner, consent manager & preference center for JavaScript / TypeScript. GDPR, CCPA, LGPD and IAB TCF compliant.",
5
5
  "keywords": [
6
6
  "consent",
@@ -74,8 +74,8 @@
74
74
  "not op_mini all"
75
75
  ],
76
76
  "dependencies": {
77
- "@c15t/schema": "2.0.1",
78
- "@c15t/translations": "2.0.0",
77
+ "@c15t/schema": "2.1.0",
78
+ "@c15t/translations": "2.1.0",
79
79
  "zustand": "5.0.12"
80
80
  },
81
81
  "devDependencies": {