@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.
- package/AGENTS.md +74 -48
- package/README.md +3 -3
- package/dist/e2e-test-utils.js +60 -24
- package/dist/engine/compile.js +45 -45
- package/dist/engine/runtime.js +119 -119
- package/dist/registry.js +186 -176
- package/dist/resolve.js +12 -12
- package/dist/vendors/_shared/attributes.js +5 -5
- package/dist/vendors/_shared/google-consent.js +10 -10
- package/dist/vendors/_shared/install-builders.js +9 -9
- package/dist/vendors/_shared/script-url.js +12 -12
- package/dist/vendors/ads-and-pixels/linkedin-insights.js +16 -16
- package/dist/vendors/ads-and-pixels/meta-pixel.js +82 -82
- package/dist/vendors/ads-and-pixels/microsoft-uet.js +57 -57
- package/dist/vendors/ads-and-pixels/openai-pixel.js +88 -0
- package/dist/vendors/ads-and-pixels/reddit-pixel.js +39 -39
- package/dist/vendors/ads-and-pixels/snapchat-pixel.js +25 -25
- package/dist/vendors/ads-and-pixels/tiktok-pixel.js +31 -31
- package/dist/vendors/ads-and-pixels/x-pixel.js +17 -17
- package/dist/vendors/analytics/adobe-analytics.js +17 -17
- package/dist/vendors/analytics/ahrefs-analytics.js +8 -8
- package/dist/vendors/analytics/amplitude.js +39 -39
- package/dist/vendors/analytics/clearbit.js +11 -11
- package/dist/vendors/analytics/cloudflare-web-analytics.js +13 -13
- package/dist/vendors/analytics/databuddy.js +45 -45
- package/dist/vendors/analytics/fathom-analytics.js +15 -15
- package/dist/vendors/analytics/google-tag.js +23 -23
- package/dist/vendors/analytics/heap.js +37 -37
- package/dist/vendors/analytics/hightouch.js +30 -30
- package/dist/vendors/analytics/hotjar.js +14 -14
- package/dist/vendors/analytics/logrocket.js +24 -24
- package/dist/vendors/analytics/matomo-analytics.js +51 -51
- package/dist/vendors/analytics/microsoft-clarity.js +31 -31
- package/dist/vendors/analytics/mixpanel-analytics.js +31 -31
- package/dist/vendors/analytics/pirsch.js +27 -27
- package/dist/vendors/analytics/plausible-analytics.js +24 -24
- package/dist/vendors/analytics/posthog.js +84 -79
- package/dist/vendors/analytics/promptwatch.js +8 -8
- package/dist/vendors/analytics/rudderstack.js +50 -50
- package/dist/vendors/analytics/rybbit-analytics.js +30 -30
- package/dist/vendors/analytics/segment.js +16 -16
- package/dist/vendors/analytics/umami-analytics.js +16 -16
- package/dist/vendors/analytics/vercel-analytics.js +22 -22
- package/dist/vendors/functional/crisp.js +49 -51
- package/dist/vendors/functional/intercom.js +18 -18
- package/dist/vendors/tag-managers/google-tag-manager.js +20 -20
- package/dist-types/__tests__/helpers.d.ts +10 -10
- package/dist-types/engine/compile.d.ts +2 -2
- package/dist-types/engine/runtime.d.ts +3 -3
- package/dist-types/registry.d.ts +182 -173
- package/dist-types/resolve.d.ts +2 -2
- package/dist-types/types.d.ts +2 -2
- package/dist-types/vendors/_shared/attributes.d.ts +2 -2
- package/dist-types/vendors/_shared/google-consent.d.ts +2 -2
- package/dist-types/vendors/_shared/install-builders.d.ts +1 -1
- package/dist-types/vendors/_shared/script-url.d.ts +6 -6
- package/dist-types/vendors/ads-and-pixels/linkedin-insights.d.ts +14 -14
- package/dist-types/vendors/ads-and-pixels/meta-pixel.d.ts +27 -27
- package/dist-types/vendors/ads-and-pixels/microsoft-uet.d.ts +40 -40
- package/dist-types/vendors/ads-and-pixels/openai-pixel.d.ts +211 -0
- package/dist-types/vendors/ads-and-pixels/reddit-pixel.d.ts +28 -29
- package/dist-types/vendors/ads-and-pixels/snapchat-pixel.d.ts +23 -23
- package/dist-types/vendors/ads-and-pixels/tiktok-pixel.d.ts +22 -22
- package/dist-types/vendors/ads-and-pixels/x-pixel.d.ts +15 -15
- package/dist-types/vendors/analytics/adobe-analytics.d.ts +3 -3
- package/dist-types/vendors/analytics/ahrefs-analytics.d.ts +5 -5
- package/dist-types/vendors/analytics/amplitude.d.ts +24 -24
- package/dist-types/vendors/analytics/clearbit.d.ts +5 -5
- package/dist-types/vendors/analytics/cloudflare-web-analytics.d.ts +6 -6
- package/dist-types/vendors/analytics/databuddy.d.ts +34 -31
- package/dist-types/vendors/analytics/fathom-analytics.d.ts +8 -8
- package/dist-types/vendors/analytics/google-tag.d.ts +17 -17
- package/dist-types/vendors/analytics/heap.d.ts +17 -17
- package/dist-types/vendors/analytics/hightouch.d.ts +15 -15
- package/dist-types/vendors/analytics/hotjar.d.ts +9 -9
- package/dist-types/vendors/analytics/logrocket.d.ts +11 -11
- package/dist-types/vendors/analytics/matomo-analytics.d.ts +3 -3
- package/dist-types/vendors/analytics/microsoft-clarity.d.ts +12 -13
- package/dist-types/vendors/analytics/mixpanel-analytics.d.ts +20 -20
- package/dist-types/vendors/analytics/pirsch.d.ts +10 -10
- package/dist-types/vendors/analytics/plausible-analytics.d.ts +13 -13
- package/dist-types/vendors/analytics/posthog.d.ts +35 -32
- package/dist-types/vendors/analytics/promptwatch.d.ts +5 -5
- package/dist-types/vendors/analytics/rudderstack.d.ts +16 -16
- package/dist-types/vendors/analytics/rybbit-analytics.d.ts +15 -15
- package/dist-types/vendors/analytics/segment.d.ts +11 -11
- package/dist-types/vendors/analytics/umami-analytics.d.ts +9 -9
- package/dist-types/vendors/analytics/vercel-analytics.d.ts +13 -13
- package/dist-types/vendors/functional/crisp.d.ts +9 -9
- package/dist-types/vendors/functional/intercom.d.ts +12 -12
- package/dist-types/vendors/tag-managers/google-tag-manager.d.ts +16 -16
- package/docs/README.md +74 -48
- package/docs/assets/v3/brand-bar.png +0 -0
- package/docs/assets/v3/brand-card.png +0 -0
- package/docs/assets/v3/choice-wall.png +0 -0
- package/docs/assets/v3/mobile-card.png +0 -0
- package/docs/assets/v3/preferences.png +0 -0
- package/docs/customization/overview.md +45 -0
- package/docs/customization/recipes.md +79 -0
- package/docs/customization/slots.md +55 -0
- package/docs/customization/tokens.md +76 -0
- package/docs/customization/translations.md +49 -0
- package/docs/frameworks/javascript/script-loader.md +30 -339
- package/docs/frameworks/next/script-loader.md +134 -467
- package/docs/frameworks/react/script-loader.md +35 -535
- package/docs/guides/consent-state.md +60 -0
- package/docs/guides/data-fetching.md +163 -0
- package/docs/guides/deployment-modes.md +63 -0
- package/docs/guides/troubleshooting.md +68 -0
- package/docs/guides/verify-consent.md +62 -0
- package/docs/integrations/adobe-analytics.md +239 -105
- package/docs/integrations/ahrefs-analytics.md +238 -104
- package/docs/integrations/amplitude.md +219 -157
- package/docs/integrations/building-integrations.md +32 -224
- package/docs/integrations/clearbit.md +247 -86
- package/docs/integrations/cloudflare-web-analytics.md +250 -84
- package/docs/integrations/crisp.md +251 -97
- package/docs/integrations/databuddy.md +259 -153
- package/docs/integrations/fathom-analytics.md +239 -96
- package/docs/integrations/google-maps.md +328 -207
- package/docs/integrations/google-tag-manager.md +248 -96
- package/docs/integrations/google-tag.md +261 -90
- package/docs/integrations/heap.md +222 -149
- package/docs/integrations/hightouch.md +225 -131
- package/docs/integrations/hotjar.md +239 -90
- package/docs/integrations/intercom.md +239 -98
- package/docs/integrations/linkedin-insights.md +243 -113
- package/docs/integrations/logrocket.md +241 -123
- package/docs/integrations/matomo-analytics.md +256 -111
- package/docs/integrations/meta-pixel.md +197 -324
- package/docs/integrations/microsoft-clarity.md +233 -114
- package/docs/integrations/microsoft-uet.md +245 -110
- package/docs/integrations/mixpanel-analytics.md +252 -87
- package/docs/integrations/openai-pixel.md +441 -0
- package/docs/integrations/overview.md +95 -133
- package/docs/integrations/pirsch.md +249 -96
- package/docs/integrations/plausible-analytics.md +241 -100
- package/docs/integrations/posthog.md +353 -214
- package/docs/integrations/promptwatch.md +251 -81
- package/docs/integrations/reddit-pixel.md +226 -173
- package/docs/integrations/rudderstack.md +244 -187
- package/docs/integrations/rybbit-analytics.md +244 -91
- package/docs/integrations/segment.md +238 -92
- package/docs/integrations/snapchat-pixel.md +240 -110
- package/docs/integrations/tiktok-pixel.md +249 -81
- package/docs/integrations/umami-analytics.md +242 -95
- package/docs/integrations/vercel-analytics.md +242 -90
- package/docs/integrations/x-pixel.md +238 -104
- package/docs/integrations/youtube.md +354 -142
- package/docs/upgrade-v3.md +334 -0
- package/package.json +85 -78
- package/readme.json +2 -2
- package/dist/e2e-test-utils.cjs +0 -166
- package/dist/engine/compile.cjs +0 -130
- package/dist/engine/runtime.cjs +0 -475
- package/dist/registry.cjs +0 -423
- package/dist/resolve.cjs +0 -71
- package/dist/types.cjs +0 -69
- package/dist/vendors/_shared/attributes.cjs +0 -55
- package/dist/vendors/_shared/google-consent.cjs +0 -69
- package/dist/vendors/_shared/install-builders.cjs +0 -59
- package/dist/vendors/_shared/script-url.cjs +0 -78
- package/dist/vendors/ads-and-pixels/linkedin-insights.cjs +0 -89
- package/dist/vendors/ads-and-pixels/meta-pixel.cjs +0 -206
- package/dist/vendors/ads-and-pixels/microsoft-uet.cjs +0 -151
- package/dist/vendors/ads-and-pixels/reddit-pixel.cjs +0 -151
- package/dist/vendors/ads-and-pixels/snapchat-pixel.cjs +0 -131
- package/dist/vendors/ads-and-pixels/tiktok-pixel.cjs +0 -130
- package/dist/vendors/ads-and-pixels/x-pixel.cjs +0 -92
- package/dist/vendors/analytics/adobe-analytics.cjs +0 -90
- package/dist/vendors/analytics/ahrefs-analytics.cjs +0 -68
- package/dist/vendors/analytics/amplitude.cjs +0 -193
- package/dist/vendors/analytics/clearbit.cjs +0 -69
- package/dist/vendors/analytics/cloudflare-web-analytics.cjs +0 -73
- package/dist/vendors/analytics/databuddy.cjs +0 -144
- package/dist/vendors/analytics/fathom-analytics.cjs +0 -76
- package/dist/vendors/analytics/google-tag.cjs +0 -107
- package/dist/vendors/analytics/heap.cjs +0 -181
- package/dist/vendors/analytics/hightouch.cjs +0 -153
- package/dist/vendors/analytics/hotjar.cjs +0 -85
- package/dist/vendors/analytics/logrocket.cjs +0 -99
- package/dist/vendors/analytics/matomo-analytics.cjs +0 -232
- package/dist/vendors/analytics/microsoft-clarity.cjs +0 -138
- package/dist/vendors/analytics/mixpanel-analytics.cjs +0 -134
- package/dist/vendors/analytics/pirsch.cjs +0 -108
- package/dist/vendors/analytics/plausible-analytics.cjs +0 -122
- package/dist/vendors/analytics/posthog.cjs +0 -236
- package/dist/vendors/analytics/promptwatch.cjs +0 -70
- package/dist/vendors/analytics/rudderstack.cjs +0 -227
- package/dist/vendors/analytics/rybbit-analytics.cjs +0 -104
- package/dist/vendors/analytics/segment.cjs +0 -97
- package/dist/vendors/analytics/umami-analytics.cjs +0 -80
- package/dist/vendors/analytics/vercel-analytics.cjs +0 -94
- package/dist/vendors/functional/crisp.cjs +0 -143
- package/dist/vendors/functional/intercom.cjs +0 -89
- package/dist/vendors/tag-managers/google-tag-manager.cjs +0 -100
- package/docs/shared/react/guides/script-loader.md +0 -311
|
@@ -1,356 +1,47 @@
|
|
|
1
1
|
---
|
|
2
|
-
title:
|
|
3
|
-
description:
|
|
4
|
-
|
|
2
|
+
title: JavaScript script loading
|
|
3
|
+
description: Attach a script loader to the consent kernel and dispose it with
|
|
4
|
+
the application.
|
|
5
5
|
group: frameworks
|
|
6
6
|
---
|
|
7
|
-
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.
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
## Attach the loader before initialization
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
> ℹ️ **Info:**
|
|
14
|
-
> 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.
|
|
15
|
-
>
|
|
16
|
-
> 📝 **Note:**
|
|
17
|
-
> 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.
|
|
18
|
-
>
|
|
19
|
-
> ℹ️ **Info:**
|
|
20
|
-
> 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.
|
|
21
|
-
|
|
22
|
-
## Basic Usage
|
|
23
|
-
|
|
24
|
-
Pass an array of `Script` objects to the runtime options:
|
|
10
|
+
Install `@c15t/scripts` alongside `c15t`. This browser example adds a
|
|
11
|
+
marketing integration to a hosted kernel:
|
|
25
12
|
|
|
26
13
|
```ts
|
|
27
|
-
import {
|
|
14
|
+
import { createConsentKernel, createHostedTransport } from 'c15t';
|
|
15
|
+
import { createPersistence } from 'c15t/modules/persistence';
|
|
16
|
+
import { createScriptLoader } from 'c15t/modules/script-loader';
|
|
28
17
|
import { metaPixel } from '@c15t/scripts/meta-pixel';
|
|
29
18
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
backendURL: 'https://your-instance.c15t.dev',
|
|
33
|
-
scripts: [
|
|
34
|
-
metaPixel({ pixelId: '123456' }),
|
|
35
|
-
{
|
|
36
|
-
id: 'custom-analytics',
|
|
37
|
-
src: 'https://cdn.example.com/analytics.js',
|
|
38
|
-
category: 'measurement',
|
|
39
|
-
},
|
|
40
|
-
],
|
|
19
|
+
const kernel = createConsentKernel({
|
|
20
|
+
transport: createHostedTransport({ backendURL: 'https://your-project.inth.app' }),
|
|
41
21
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
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:
|
|
47
|
-
|
|
48
|
-
1. **Pending** — registered but waiting for consent. Nothing is in the DOM yet.
|
|
49
|
-
2. **Loaded** — consent matched, c15t injected the script (or ran callbacks for callback-only scripts).
|
|
50
|
-
3. **Updated** — already loaded, consent state changed, `onConsentChange` ran so the SDK can react.
|
|
51
|
-
4. **Unloaded** — consent was revoked. c15t removed the script element unless you opted into persistence.
|
|
52
|
-
|
|
53
|
-
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.
|
|
54
|
-
|
|
55
|
-
## Choose the Right Approach
|
|
56
|
-
|
|
57
|
-
Most projects mix more than one style. Pick the smallest one that keeps consent behavior obvious:
|
|
58
|
-
|
|
59
|
-
|Style|Use when|
|
|
60
|
-
|--|--|
|
|
61
|
-
|**Built-in helper** from `@c15t/scripts`|c15t already ships the vendor. See the [integrations overview](/docs/integrations/overview).|
|
|
62
|
-
|**Plain `Script`**|One-off app code with simple load and callback behavior.|
|
|
63
|
-
|**Callback-only `Script`**|Another package already loaded the SDK; c15t only synchronizes consent.|
|
|
64
|
-
|**Manifest-backed helper**|Reusable vendor integration with structured setup phases, queues, stubs, or a vendor consent API.|
|
|
65
|
-
|**Iframe / renderable integration**|Vendor exposes an iframe or React component, not just a `<script>` tag.|
|
|
66
|
-
|
|
67
|
-
## Script Types
|
|
68
|
-
|
|
69
|
-
### Standard Scripts
|
|
70
|
-
|
|
71
|
-
Standard scripts load an external JavaScript file via a `<script>` tag. This is the default for most analytics and pixel SDKs:
|
|
72
|
-
|
|
73
|
-
```tsx
|
|
74
|
-
{
|
|
75
|
-
id: 'analytics',
|
|
76
|
-
src: 'https://cdn.example.com/analytics.js',
|
|
77
|
-
category: 'measurement',
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Inline Scripts
|
|
82
|
-
|
|
83
|
-
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.
|
|
84
|
-
|
|
85
|
-
```tsx
|
|
86
|
-
{
|
|
87
|
-
id: 'gtag-config',
|
|
88
|
-
textContent: `
|
|
89
|
-
window.dataLayer = window.dataLayer || [];
|
|
90
|
-
function gtag(){dataLayer.push(arguments);}
|
|
91
|
-
gtag('js', new Date());
|
|
92
|
-
gtag('config', 'G-XXXXXX');
|
|
93
|
-
`,
|
|
94
|
-
category: 'measurement',
|
|
95
|
-
}
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Callback-Only Scripts
|
|
99
|
-
|
|
100
|
-
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:
|
|
101
|
-
|
|
102
|
-
```tsx
|
|
103
|
-
{
|
|
104
|
-
id: 'posthog-consent',
|
|
105
|
-
callbackOnly: true,
|
|
106
|
-
category: 'measurement',
|
|
107
|
-
onLoad: ({ hasConsent }) => {
|
|
108
|
-
if (hasConsent) {
|
|
109
|
-
posthog.opt_in_capturing();
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
onConsentChange: ({ hasConsent }) => {
|
|
113
|
-
if (hasConsent) {
|
|
114
|
-
posthog.opt_in_capturing();
|
|
115
|
-
} else {
|
|
116
|
-
posthog.opt_out_capturing();
|
|
117
|
-
}
|
|
118
|
-
},
|
|
119
|
-
}
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### Manifest-Backed Helpers
|
|
123
|
-
|
|
124
|
-
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.
|
|
125
|
-
|
|
126
|
-
Use a manifest-backed helper when:
|
|
127
|
-
|
|
128
|
-
* the integration should be reused across projects,
|
|
129
|
-
* the vendor snippet has ordered setup steps,
|
|
130
|
-
* the vendor exposes a consent API,
|
|
131
|
-
* or you plan to contribute the integration back to c15t.
|
|
132
|
-
|
|
133
|
-
Read the [custom integration guide](/docs/integrations/building-integrations) for the manifest contract, phases, and testing checklist.
|
|
134
|
-
|
|
135
|
-
### Iframe And Renderable Integrations
|
|
136
|
-
|
|
137
|
-
Some vendors are not just script tags. YouTube embeds, maps, calendars, and checkout widgets often need a visible component, a placeholder, or an iframe.
|
|
138
|
-
|
|
139
|
-
* 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.
|
|
140
|
-
* For SDK-backed UI, use the script loader for the shared SDK and render the component only when consent and SDK readiness agree.
|
|
141
|
-
* Use `YouTubeEmbed` for the iframe-only YouTube candidate and `GoogleMap` for the callback-based SDK candidate.
|
|
142
|
-
* 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.
|
|
143
|
-
|
|
144
|
-
## Lifecycle Callbacks
|
|
145
|
-
|
|
146
|
-
Every script supports four callbacks. Each receives a `ScriptCallbackInfo` payload (id, element, hasConsent, consents):
|
|
147
|
-
|
|
148
|
-
* `onBeforeLoad` — runs before the script tag is injected. Create globals, queues, or vendor stubs here.
|
|
149
|
-
* `onLoad` — runs after the browser loads the script. Call vendor `init()` APIs here.
|
|
150
|
-
* `onConsentChange` — runs for loaded scripts when consent changes. Forward the new consent state to the vendor SDK.
|
|
151
|
-
* `onError` — runs when the script fails to load. Record diagnostics or render a fallback.
|
|
152
|
-
|
|
153
|
-
```tsx
|
|
154
|
-
{
|
|
155
|
-
id: 'analytics',
|
|
156
|
-
src: 'https://analytics.example.com/v2.js',
|
|
157
|
-
category: 'measurement',
|
|
158
|
-
onBeforeLoad: ({ id }) => {
|
|
159
|
-
window.analyticsQueue = window.analyticsQueue || [];
|
|
160
|
-
},
|
|
161
|
-
onLoad: () => {
|
|
162
|
-
window.analytics.init('my-key');
|
|
163
|
-
},
|
|
164
|
-
onError: ({ error }) => {
|
|
165
|
-
console.error('Failed to load analytics:', error);
|
|
166
|
-
},
|
|
167
|
-
onConsentChange: ({ hasConsent }) => {
|
|
168
|
-
window.analytics.setConsent(hasConsent);
|
|
169
|
-
},
|
|
170
|
-
}
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## Consent Conditions
|
|
174
|
-
|
|
175
|
-
The `category` field accepts a `HasCondition`. It can be a single consent category or a logical expression:
|
|
176
|
-
|
|
177
|
-
```tsx
|
|
178
|
-
// Simple: requires measurement consent
|
|
179
|
-
{ category: 'measurement' }
|
|
180
|
-
|
|
181
|
-
// AND: requires both measurement and marketing
|
|
182
|
-
{ category: { and: ['measurement', 'marketing'] } }
|
|
183
|
-
|
|
184
|
-
// OR: requires either measurement or marketing
|
|
185
|
-
{ category: { or: ['measurement', 'marketing'] } }
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Consent categories use the same names as the rest of c15t (`necessary`, `functionality`, `experience`, `measurement`, `marketing`).
|
|
189
|
-
|
|
190
|
-
## Persistence Options
|
|
191
|
-
|
|
192
|
-
### Always Load
|
|
193
|
-
|
|
194
|
-
`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.
|
|
195
|
-
|
|
196
|
-
```tsx
|
|
197
|
-
{
|
|
198
|
-
id: 'google-tag-manager',
|
|
199
|
-
src: 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXX',
|
|
200
|
-
category: 'measurement',
|
|
201
|
-
alwaysLoad: true,
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
When `alwaysLoad` is on, `onConsentChange` becomes mandatory: it is how the loaded SDK learns about every transition.
|
|
206
|
-
|
|
207
|
-
> ⚠️ **Warning:**
|
|
208
|
-
> alwaysLoad shifts compliance responsibility to the vendor integration. Make sure the script receives denied-by-default consent signals before it can track.
|
|
209
|
-
|
|
210
|
-
### Persist After Revocation
|
|
211
|
-
|
|
212
|
-
`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.
|
|
213
|
-
|
|
214
|
-
```tsx
|
|
215
|
-
{
|
|
216
|
-
id: 'error-tracking',
|
|
217
|
-
src: 'https://errors.example.com/track.js',
|
|
218
|
-
category: 'measurement',
|
|
219
|
-
persistAfterConsentRevoked: true,
|
|
220
|
-
onConsentChange: ({ hasConsent }) => {
|
|
221
|
-
window.ErrorTracker.setConsent(hasConsent);
|
|
222
|
-
},
|
|
223
|
-
}
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
As with `alwaysLoad`, `onConsentChange` is how the persisted SDK learns about consent updates.
|
|
227
|
-
|
|
228
|
-
### `alwaysLoad` vs `persistAfterConsentRevoked`
|
|
229
|
-
|
|
230
|
-
These two flags answer different questions. Use this table to keep them straight:
|
|
231
|
-
|
|
232
|
-
|Question|`alwaysLoad`|`persistAfterConsentRevoked`|
|
|
233
|
-
|--|--|--|
|
|
234
|
-
|Loads before consent is granted?|Yes|No (waits for consent like a normal script)|
|
|
235
|
-
|Stays loaded after consent is revoked?|Yes|Yes|
|
|
236
|
-
|Requires a vendor consent API?|Yes|Yes|
|
|
237
|
-
|
|
238
|
-
## DOM Placement
|
|
239
|
-
|
|
240
|
-
Control where the script is injected and whether the element id is anonymized:
|
|
241
|
-
|
|
242
|
-
```tsx
|
|
243
|
-
{
|
|
244
|
-
id: 'widget',
|
|
245
|
-
src: 'https://widget.example.com/embed.js',
|
|
246
|
-
category: 'experience',
|
|
247
|
-
target: 'body', // 'head' (default) or 'body'
|
|
248
|
-
anonymizeId: true, // default: true, hides the c15t script id from ad blockers
|
|
249
|
-
nonce: 'abc123', // optional CSP nonce
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
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.
|
|
254
|
-
|
|
255
|
-
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.
|
|
256
|
-
|
|
257
|
-
## Dynamic Management
|
|
258
|
-
|
|
259
|
-
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:
|
|
260
|
-
|
|
261
|
-
* `setScripts(scripts)` — registers script definitions and immediately evaluates them against consent.
|
|
262
|
-
* `removeScript(id)` — removes a definition and unloads its element if needed.
|
|
263
|
-
* `isScriptLoaded(id)` — returns whether c15t has loaded a script.
|
|
264
|
-
* `getLoadedScriptIds()` — returns every currently loaded script id.
|
|
265
|
-
|
|
266
|
-
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.
|
|
267
|
-
|
|
268
|
-
## Calling Vendor APIs From Your App
|
|
269
|
-
|
|
270
|
-
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:
|
|
22
|
+
const persistence = createPersistence({ kernel });
|
|
23
|
+
const loader = createScriptLoader({ kernel, scripts: [metaPixel({ pixelId: '123456789012345' })] });
|
|
24
|
+
await kernel.commands.init();
|
|
271
25
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|`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.|
|
|
277
|
-
|No app-facing API (e.g. Cloudflare Web Analytics)|Script in/out of DOM based on consent. Tracking is fully automatic.|Nothing to guard.|
|
|
278
|
-
|
|
279
|
-
The safe pattern in React is to read consent state through `useConsentManager().has(category)` before calling the SDK:
|
|
280
|
-
|
|
281
|
-
```tsx
|
|
282
|
-
import { useCallback } from 'react';
|
|
283
|
-
import { useConsentManager } from '@c15t/react';
|
|
284
|
-
|
|
285
|
-
function useTrackSignup() {
|
|
286
|
-
const { has } = useConsentManager();
|
|
287
|
-
|
|
288
|
-
return useCallback(() => {
|
|
289
|
-
if (has('measurement')) {
|
|
290
|
-
window.fathom?.trackEvent('signup');
|
|
291
|
-
}
|
|
292
|
-
}, [has]);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function SignupButton() {
|
|
296
|
-
const trackSignup = useTrackSignup();
|
|
297
|
-
|
|
298
|
-
return <button onClick={trackSignup}>Sign up</button>;
|
|
299
|
-
}
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
From non-React code, read the consent store directly:
|
|
303
|
-
|
|
304
|
-
```ts
|
|
305
|
-
import { getOrCreateConsentRuntime } from 'c15t';
|
|
306
|
-
|
|
307
|
-
const { consentStore } = getOrCreateConsentRuntime();
|
|
308
|
-
|
|
309
|
-
if (consentStore.getState().has('measurement')) {
|
|
310
|
-
window.fathom?.trackEvent('signup');
|
|
26
|
+
function dispose() {
|
|
27
|
+
loader.dispose();
|
|
28
|
+
persistence.dispose();
|
|
29
|
+
kernel.dispose();
|
|
311
30
|
}
|
|
312
31
|
```
|
|
313
32
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
When a script does not behave as expected:
|
|
319
|
-
|
|
320
|
-
1. Confirm the script's `category` matches the consent that has been granted.
|
|
321
|
-
2. Check whether the script is `alwaysLoad` or consent-gated.
|
|
322
|
-
3. Confirm `onBeforeLoad` creates any globals before the vendor code reads them.
|
|
323
|
-
4. Confirm `onConsentChange` updates persisted or always-loaded scripts when consent changes.
|
|
324
|
-
5. Check whether the browser or an ad blocker blocked the request.
|
|
325
|
-
6. Use c15t devtools to inspect script lifecycle events when available.
|
|
326
|
-
|
|
327
|
-
## Dynamic Script Management
|
|
328
|
-
|
|
329
|
-
Add, remove, or check scripts at runtime via the store:
|
|
330
|
-
|
|
331
|
-
```ts
|
|
332
|
-
const state = consentStore.getState();
|
|
333
|
-
|
|
334
|
-
// Add scripts dynamically
|
|
335
|
-
state.setScripts([
|
|
336
|
-
{ id: 'dynamic', src: 'https://cdn.example.com/widget.js', category: 'measurement' },
|
|
337
|
-
]);
|
|
338
|
-
|
|
339
|
-
// Remove a script
|
|
340
|
-
state.removeScript('dynamic');
|
|
341
|
-
|
|
342
|
-
// Check if a script is loaded
|
|
343
|
-
const loaded = state.isScriptLoaded('custom-analytics');
|
|
344
|
-
|
|
345
|
-
// Get all loaded script IDs
|
|
346
|
-
const allLoaded = state.getLoadedScriptIds();
|
|
347
|
-
```
|
|
33
|
+
Replace the URL and pixel ID. Call `dispose()` when your application tears down.
|
|
34
|
+
The headless kernel does not render a banner; connect a policy-aware UI before
|
|
35
|
+
shipping this integration. A framework provider already manages these modules,
|
|
36
|
+
so do not attach a second loader to a provider-owned kernel.
|
|
348
37
|
|
|
349
|
-
##
|
|
38
|
+
## Keep one owner per vendor
|
|
350
39
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
40
|
+
Use stable script IDs and remove the vendor's original snippet. Ordinary scripts
|
|
41
|
+
wait for effective permission. Helpers with `alwaysLoad` instead load and signal
|
|
42
|
+
permission through the vendor API. Read the individual integration guide before
|
|
43
|
+
assuming all helpers have the same network behavior.
|
|
355
44
|
|
|
356
|
-
|
|
45
|
+
The loader exposes `updateScripts`, `getLoadedScriptIds` and `dispose`.
|
|
46
|
+
Unloading an element cannot reverse requests or code that already ran. Test
|
|
47
|
+
revocation and vendor cleanup with [verification](../../guides/verify-consent.md).
|