@c15t/scripts 2.1.0 → 2.2.0-canary-20260804162155
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 +62 -0
- package/README.md +32 -20
- package/dist/e2e-test-utils.cjs +51 -10
- package/dist/e2e-test-utils.js +37 -3
- package/dist/engine/compile.cjs +9 -5
- package/dist/engine/runtime.cjs +103 -17
- package/dist/engine/runtime.js +94 -12
- package/dist/registry.cjs +97 -12
- package/dist/registry.js +80 -0
- package/dist/resolve.cjs +9 -5
- package/dist/types.cjs +17 -13
- package/dist/vendors/_shared/attributes.cjs +13 -9
- package/dist/vendors/_shared/google-consent.cjs +14 -9
- package/dist/vendors/_shared/install-builders.cjs +12 -8
- package/dist/vendors/_shared/script-url.cjs +16 -12
- package/dist/vendors/ads-and-pixels/linkedin-insights.cjs +9 -5
- package/dist/vendors/ads-and-pixels/meta-pixel.cjs +13 -9
- package/dist/vendors/ads-and-pixels/microsoft-uet.cjs +11 -7
- package/dist/vendors/ads-and-pixels/reddit-pixel.cjs +9 -5
- package/dist/vendors/ads-and-pixels/snapchat-pixel.cjs +11 -7
- package/dist/vendors/ads-and-pixels/tiktok-pixel.cjs +9 -5
- package/dist/vendors/ads-and-pixels/x-pixel.cjs +9 -5
- package/dist/vendors/analytics/adobe-analytics.cjs +90 -0
- package/dist/vendors/analytics/adobe-analytics.js +49 -0
- package/dist/vendors/analytics/ahrefs-analytics.cjs +11 -7
- package/dist/vendors/analytics/amplitude.cjs +193 -0
- package/dist/vendors/analytics/amplitude.js +134 -0
- package/dist/vendors/analytics/clearbit.cjs +69 -0
- package/dist/vendors/analytics/clearbit.js +28 -0
- package/dist/vendors/analytics/cloudflare-web-analytics.cjs +11 -7
- package/dist/vendors/analytics/databuddy.cjs +11 -7
- package/dist/vendors/analytics/fathom-analytics.cjs +11 -7
- package/dist/vendors/analytics/google-tag.cjs +11 -7
- package/dist/vendors/analytics/heap.cjs +181 -0
- package/dist/vendors/analytics/heap.js +134 -0
- package/dist/vendors/analytics/hightouch.cjs +153 -0
- package/dist/vendors/analytics/hightouch.js +109 -0
- package/dist/vendors/analytics/hotjar.cjs +11 -7
- package/dist/vendors/analytics/logrocket.cjs +99 -0
- package/dist/vendors/analytics/logrocket.js +58 -0
- package/dist/vendors/analytics/matomo-analytics.cjs +9 -5
- package/dist/vendors/analytics/microsoft-clarity.cjs +75 -47
- package/dist/vendors/analytics/microsoft-clarity.js +64 -40
- package/dist/vendors/analytics/mixpanel-analytics.cjs +31 -16
- package/dist/vendors/analytics/mixpanel-analytics.js +22 -11
- package/dist/vendors/analytics/pirsch.cjs +108 -0
- package/dist/vendors/analytics/pirsch.js +67 -0
- package/dist/vendors/analytics/plausible-analytics.cjs +9 -5
- package/dist/vendors/analytics/posthog.cjs +63 -27
- package/dist/vendors/analytics/posthog.js +52 -20
- package/dist/vendors/analytics/promptwatch.cjs +11 -7
- package/dist/vendors/analytics/rudderstack.cjs +227 -0
- package/dist/vendors/analytics/rudderstack.js +183 -0
- package/dist/vendors/analytics/rybbit-analytics.cjs +9 -5
- package/dist/vendors/analytics/segment.cjs +9 -5
- package/dist/vendors/analytics/umami-analytics.cjs +9 -5
- package/dist/vendors/analytics/vercel-analytics.cjs +11 -7
- package/dist/vendors/functional/crisp.cjs +9 -5
- package/dist/vendors/functional/intercom.cjs +11 -7
- package/dist/vendors/tag-managers/google-tag-manager.cjs +11 -7
- package/dist-types/registry.d.ts +313 -241
- package/dist-types/types.d.ts +46 -4
- package/dist-types/vendors/ads-and-pixels/linkedin-insights.d.ts +18 -18
- package/dist-types/vendors/ads-and-pixels/meta-pixel.d.ts +21 -21
- package/dist-types/vendors/ads-and-pixels/microsoft-uet.d.ts +32 -32
- package/dist-types/vendors/ads-and-pixels/reddit-pixel.d.ts +19 -19
- package/dist-types/vendors/ads-and-pixels/snapchat-pixel.d.ts +16 -16
- package/dist-types/vendors/ads-and-pixels/tiktok-pixel.d.ts +28 -28
- package/dist-types/vendors/ads-and-pixels/x-pixel.d.ts +13 -13
- package/dist-types/vendors/analytics/adobe-analytics.d.ts +75 -0
- package/dist-types/vendors/analytics/ahrefs-analytics.d.ts +7 -7
- package/dist-types/vendors/analytics/amplitude.d.ts +234 -0
- package/dist-types/vendors/analytics/clearbit.d.ts +60 -0
- package/dist-types/vendors/analytics/cloudflare-web-analytics.d.ts +7 -7
- package/dist-types/vendors/analytics/databuddy.d.ts +25 -25
- package/dist-types/vendors/analytics/fathom-analytics.d.ts +11 -11
- package/dist-types/vendors/analytics/google-tag.d.ts +17 -17
- package/dist-types/vendors/analytics/heap.d.ts +316 -0
- package/dist-types/vendors/analytics/hightouch.d.ts +285 -0
- package/dist-types/vendors/analytics/hotjar.d.ts +14 -14
- package/dist-types/vendors/analytics/logrocket.d.ts +101 -0
- package/dist-types/vendors/analytics/microsoft-clarity.d.ts +42 -30
- package/dist-types/vendors/analytics/mixpanel-analytics.d.ts +40 -28
- package/dist-types/vendors/analytics/pirsch.d.ts +96 -0
- package/dist-types/vendors/analytics/plausible-analytics.d.ts +13 -13
- package/dist-types/vendors/analytics/posthog.d.ts +63 -45
- package/dist-types/vendors/analytics/promptwatch.d.ts +7 -7
- package/dist-types/vendors/analytics/rudderstack.d.ts +330 -0
- package/dist-types/vendors/analytics/rybbit-analytics.d.ts +18 -18
- package/dist-types/vendors/analytics/segment.d.ts +13 -13
- package/dist-types/vendors/analytics/umami-analytics.d.ts +12 -12
- package/dist-types/vendors/analytics/vercel-analytics.d.ts +16 -16
- package/dist-types/vendors/functional/crisp.d.ts +11 -11
- package/dist-types/vendors/functional/intercom.d.ts +16 -16
- package/dist-types/vendors/tag-managers/google-tag-manager.d.ts +18 -18
- package/docs/README.md +62 -0
- package/docs/frameworks/javascript/script-loader.md +356 -0
- package/docs/frameworks/next/script-loader.md +501 -0
- package/docs/frameworks/react/script-loader.md +555 -0
- package/docs/integrations/adobe-analytics.md +160 -0
- package/docs/integrations/ahrefs-analytics.md +157 -0
- package/docs/integrations/amplitude.md +230 -0
- package/docs/integrations/building-integrations.md +239 -0
- package/docs/integrations/clearbit.md +130 -0
- package/docs/integrations/cloudflare-web-analytics.md +126 -0
- package/docs/integrations/crisp.md +140 -0
- package/docs/integrations/databuddy.md +205 -0
- package/docs/integrations/fathom-analytics.md +150 -0
- package/docs/integrations/google-maps.md +263 -0
- package/docs/integrations/google-tag-manager.md +154 -0
- package/docs/integrations/google-tag.md +134 -0
- package/docs/integrations/heap.md +219 -0
- package/docs/integrations/hightouch.md +199 -0
- package/docs/integrations/hotjar.md +143 -0
- package/docs/integrations/intercom.md +144 -0
- package/docs/integrations/linkedin-insights.md +161 -0
- package/docs/integrations/logrocket.md +175 -0
- package/docs/integrations/matomo-analytics.md +171 -0
- package/docs/integrations/meta-pixel.md +412 -0
- package/docs/integrations/microsoft-clarity.md +173 -0
- package/docs/integrations/microsoft-uet.md +156 -0
- package/docs/integrations/mixpanel-analytics.md +130 -0
- package/docs/integrations/overview.md +149 -0
- package/docs/integrations/pirsch.md +142 -0
- package/docs/integrations/plausible-analytics.md +154 -0
- package/docs/integrations/posthog.md +258 -0
- package/docs/integrations/promptwatch.md +121 -0
- package/docs/integrations/reddit-pixel.md +241 -0
- package/docs/integrations/rudderstack.md +271 -0
- package/docs/integrations/rybbit-analytics.md +143 -0
- package/docs/integrations/segment.md +146 -0
- package/docs/integrations/snapchat-pixel.md +163 -0
- package/docs/integrations/tiktok-pixel.md +123 -0
- package/docs/integrations/umami-analytics.md +148 -0
- package/docs/integrations/vercel-analytics.md +141 -0
- package/docs/integrations/x-pixel.md +157 -0
- package/docs/integrations/youtube.md +193 -0
- package/docs/shared/react/guides/script-loader.md +311 -0
- package/package.json +61 -7
- package/readme.json +8 -4
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Script Loader
|
|
3
|
+
description: Gate third-party scripts behind consent in React — load Google
|
|
4
|
+
Analytics, Meta Pixel, and other tracking scripts only when users grant
|
|
5
|
+
permission.
|
|
6
|
+
group: frameworks
|
|
7
|
+
---
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
<PackageCommandTabs mode="install" command="@c15t/scripts" />
|
|
13
|
+
|
|
14
|
+
> ℹ️ **Info:**
|
|
15
|
+
> 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.
|
|
16
|
+
>
|
|
17
|
+
> 📝 **Note:**
|
|
18
|
+
> 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.
|
|
19
|
+
>
|
|
20
|
+
> ℹ️ **Info:**
|
|
21
|
+
> 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.
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
Pass an array of scripts to `ConsentManagerProvider`. Built-in helpers from `@c15t/scripts` return plain `Script` objects, so they sit beside app-specific scripts:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { type ReactNode } from 'react';
|
|
29
|
+
import { ConsentManagerProvider } from '@c15t/react';
|
|
30
|
+
import { metaPixel } from '@c15t/scripts/meta-pixel';
|
|
31
|
+
|
|
32
|
+
export function ConsentManager({ children }: { children: ReactNode }) {
|
|
33
|
+
return (
|
|
34
|
+
<ConsentManagerProvider
|
|
35
|
+
options={{
|
|
36
|
+
mode: 'hosted',
|
|
37
|
+
backendURL: 'https://your-instance.c15t.dev',
|
|
38
|
+
scripts: [
|
|
39
|
+
metaPixel({ pixelId: '123456' }),
|
|
40
|
+
{
|
|
41
|
+
id: 'custom-analytics',
|
|
42
|
+
src: 'https://cdn.example.com/analytics.js',
|
|
43
|
+
category: 'measurement',
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{children}
|
|
49
|
+
</ConsentManagerProvider>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The provider registers those scripts when the consent runtime starts. From that point on, c15t owns the lifecycle: it checks consent, injects eligible scripts, unloads them when consent is revoked, and runs `onConsentChange` for scripts that stay loaded.
|
|
55
|
+
|
|
56
|
+
## Recommended Structure
|
|
57
|
+
|
|
58
|
+
Define your script list once and keep it next to the consent provider so vendor setup stays declarative:
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
import { type ReactNode } from 'react';
|
|
62
|
+
import { ConsentManagerProvider } from '@c15t/react';
|
|
63
|
+
import { gtag } from '@c15t/scripts/google-tag';
|
|
64
|
+
import { metaPixel } from '@c15t/scripts/meta-pixel';
|
|
65
|
+
|
|
66
|
+
const scripts = [
|
|
67
|
+
gtag({ id: 'G-XXXXXXX' }),
|
|
68
|
+
metaPixel({ pixelId: '123456' }),
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
export function ConsentProvider({ children }: { children: ReactNode }) {
|
|
72
|
+
return (
|
|
73
|
+
<ConsentManagerProvider
|
|
74
|
+
options={{
|
|
75
|
+
mode: 'hosted',
|
|
76
|
+
backendURL: 'https://your-instance.c15t.dev',
|
|
77
|
+
scripts,
|
|
78
|
+
}}
|
|
79
|
+
>
|
|
80
|
+
{children}
|
|
81
|
+
</ConsentManagerProvider>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
If an integration is route-specific or tenant-specific, use [dynamic script management](#dynamic-script-management) instead of conditionally building this list per render.
|
|
87
|
+
|
|
88
|
+
## Mental Model
|
|
89
|
+
|
|
90
|
+
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:
|
|
91
|
+
|
|
92
|
+
1. **Pending** — registered but waiting for consent. Nothing is in the DOM yet.
|
|
93
|
+
2. **Loaded** — consent matched, c15t injected the script (or ran callbacks for callback-only scripts).
|
|
94
|
+
3. **Updated** — already loaded, consent state changed, `onConsentChange` ran so the SDK can react.
|
|
95
|
+
4. **Unloaded** — consent was revoked. c15t removed the script element unless you opted into persistence.
|
|
96
|
+
|
|
97
|
+
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.
|
|
98
|
+
|
|
99
|
+
## Choose the Right Approach
|
|
100
|
+
|
|
101
|
+
Most projects mix more than one style. Pick the smallest one that keeps consent behavior obvious:
|
|
102
|
+
|
|
103
|
+
|Style|Use when|
|
|
104
|
+
|--|--|
|
|
105
|
+
|**Built-in helper** from `@c15t/scripts`|c15t already ships the vendor. See the [integrations overview](/docs/integrations/overview).|
|
|
106
|
+
|**Plain `Script`**|One-off app code with simple load and callback behavior.|
|
|
107
|
+
|**Callback-only `Script`**|Another package already loaded the SDK; c15t only synchronizes consent.|
|
|
108
|
+
|**Manifest-backed helper**|Reusable vendor integration with structured setup phases, queues, stubs, or a vendor consent API.|
|
|
109
|
+
|**Iframe / renderable integration**|Vendor exposes an iframe or React component, not just a `<script>` tag.|
|
|
110
|
+
|
|
111
|
+
## Script Types
|
|
112
|
+
|
|
113
|
+
### Standard Scripts
|
|
114
|
+
|
|
115
|
+
Standard scripts load an external JavaScript file via a `<script>` tag. This is the default for most analytics and pixel SDKs:
|
|
116
|
+
|
|
117
|
+
```tsx
|
|
118
|
+
{
|
|
119
|
+
id: 'analytics',
|
|
120
|
+
src: 'https://cdn.example.com/analytics.js',
|
|
121
|
+
category: 'measurement',
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Inline Scripts
|
|
126
|
+
|
|
127
|
+
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.
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
{
|
|
131
|
+
id: 'gtag-config',
|
|
132
|
+
textContent: `
|
|
133
|
+
window.dataLayer = window.dataLayer || [];
|
|
134
|
+
function gtag(){dataLayer.push(arguments);}
|
|
135
|
+
gtag('js', new Date());
|
|
136
|
+
gtag('config', 'G-XXXXXX');
|
|
137
|
+
`,
|
|
138
|
+
category: 'measurement',
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Callback-Only Scripts
|
|
143
|
+
|
|
144
|
+
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:
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
{
|
|
148
|
+
id: 'posthog-consent',
|
|
149
|
+
callbackOnly: true,
|
|
150
|
+
category: 'measurement',
|
|
151
|
+
onLoad: ({ hasConsent }) => {
|
|
152
|
+
if (hasConsent) {
|
|
153
|
+
posthog.opt_in_capturing();
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
onConsentChange: ({ hasConsent }) => {
|
|
157
|
+
if (hasConsent) {
|
|
158
|
+
posthog.opt_in_capturing();
|
|
159
|
+
} else {
|
|
160
|
+
posthog.opt_out_capturing();
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Manifest-Backed Helpers
|
|
167
|
+
|
|
168
|
+
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.
|
|
169
|
+
|
|
170
|
+
Use a manifest-backed helper when:
|
|
171
|
+
|
|
172
|
+
* the integration should be reused across projects,
|
|
173
|
+
* the vendor snippet has ordered setup steps,
|
|
174
|
+
* the vendor exposes a consent API,
|
|
175
|
+
* or you plan to contribute the integration back to c15t.
|
|
176
|
+
|
|
177
|
+
Read the [custom integration guide](/docs/integrations/building-integrations) for the manifest contract, phases, and testing checklist.
|
|
178
|
+
|
|
179
|
+
### Iframe And Renderable Integrations
|
|
180
|
+
|
|
181
|
+
Some vendors are not just script tags. YouTube embeds, maps, calendars, and checkout widgets often need a visible component, a placeholder, or an iframe.
|
|
182
|
+
|
|
183
|
+
* 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.
|
|
184
|
+
* For SDK-backed UI, use the script loader for the shared SDK and render the component only when consent and SDK readiness agree.
|
|
185
|
+
* Use `YouTubeEmbed` for the iframe-only YouTube candidate and `GoogleMap` for the callback-based SDK candidate.
|
|
186
|
+
* 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.
|
|
187
|
+
|
|
188
|
+
## Lifecycle Callbacks
|
|
189
|
+
|
|
190
|
+
Every script supports four callbacks. Each receives a `ScriptCallbackInfo` payload (id, element, hasConsent, consents):
|
|
191
|
+
|
|
192
|
+
* `onBeforeLoad` — runs before the script tag is injected. Create globals, queues, or vendor stubs here.
|
|
193
|
+
* `onLoad` — runs after the browser loads the script. Call vendor `init()` APIs here.
|
|
194
|
+
* `onConsentChange` — runs for loaded scripts when consent changes. Forward the new consent state to the vendor SDK.
|
|
195
|
+
* `onError` — runs when the script fails to load. Record diagnostics or render a fallback.
|
|
196
|
+
|
|
197
|
+
```tsx
|
|
198
|
+
{
|
|
199
|
+
id: 'analytics',
|
|
200
|
+
src: 'https://analytics.example.com/v2.js',
|
|
201
|
+
category: 'measurement',
|
|
202
|
+
onBeforeLoad: ({ id }) => {
|
|
203
|
+
window.analyticsQueue = window.analyticsQueue || [];
|
|
204
|
+
},
|
|
205
|
+
onLoad: () => {
|
|
206
|
+
window.analytics.init('my-key');
|
|
207
|
+
},
|
|
208
|
+
onError: ({ error }) => {
|
|
209
|
+
console.error('Failed to load analytics:', error);
|
|
210
|
+
},
|
|
211
|
+
onConsentChange: ({ hasConsent }) => {
|
|
212
|
+
window.analytics.setConsent(hasConsent);
|
|
213
|
+
},
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Consent Conditions
|
|
218
|
+
|
|
219
|
+
The `category` field accepts a `HasCondition`. It can be a single consent category or a logical expression:
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
// Simple: requires measurement consent
|
|
223
|
+
{ category: 'measurement' }
|
|
224
|
+
|
|
225
|
+
// AND: requires both measurement and marketing
|
|
226
|
+
{ category: { and: ['measurement', 'marketing'] } }
|
|
227
|
+
|
|
228
|
+
// OR: requires either measurement or marketing
|
|
229
|
+
{ category: { or: ['measurement', 'marketing'] } }
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Consent categories use the same names as the rest of c15t (`necessary`, `functionality`, `experience`, `measurement`, `marketing`).
|
|
233
|
+
|
|
234
|
+
## Persistence Options
|
|
235
|
+
|
|
236
|
+
### Always Load
|
|
237
|
+
|
|
238
|
+
`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.
|
|
239
|
+
|
|
240
|
+
```tsx
|
|
241
|
+
{
|
|
242
|
+
id: 'google-tag-manager',
|
|
243
|
+
src: 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXX',
|
|
244
|
+
category: 'measurement',
|
|
245
|
+
alwaysLoad: true,
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
When `alwaysLoad` is on, `onConsentChange` becomes mandatory: it is how the loaded SDK learns about every transition.
|
|
250
|
+
|
|
251
|
+
> ⚠️ **Warning:**
|
|
252
|
+
> alwaysLoad shifts compliance responsibility to the vendor integration. Make sure the script receives denied-by-default consent signals before it can track.
|
|
253
|
+
|
|
254
|
+
### Persist After Revocation
|
|
255
|
+
|
|
256
|
+
`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.
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
{
|
|
260
|
+
id: 'error-tracking',
|
|
261
|
+
src: 'https://errors.example.com/track.js',
|
|
262
|
+
category: 'measurement',
|
|
263
|
+
persistAfterConsentRevoked: true,
|
|
264
|
+
onConsentChange: ({ hasConsent }) => {
|
|
265
|
+
window.ErrorTracker.setConsent(hasConsent);
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
As with `alwaysLoad`, `onConsentChange` is how the persisted SDK learns about consent updates.
|
|
271
|
+
|
|
272
|
+
### `alwaysLoad` vs `persistAfterConsentRevoked`
|
|
273
|
+
|
|
274
|
+
These two flags answer different questions. Use this table to keep them straight:
|
|
275
|
+
|
|
276
|
+
|Question|`alwaysLoad`|`persistAfterConsentRevoked`|
|
|
277
|
+
|--|--|--|
|
|
278
|
+
|Loads before consent is granted?|Yes|No (waits for consent like a normal script)|
|
|
279
|
+
|Stays loaded after consent is revoked?|Yes|Yes|
|
|
280
|
+
|Requires a vendor consent API?|Yes|Yes|
|
|
281
|
+
|
|
282
|
+
## DOM Placement
|
|
283
|
+
|
|
284
|
+
Control where the script is injected and whether the element id is anonymized:
|
|
285
|
+
|
|
286
|
+
```tsx
|
|
287
|
+
{
|
|
288
|
+
id: 'widget',
|
|
289
|
+
src: 'https://widget.example.com/embed.js',
|
|
290
|
+
category: 'experience',
|
|
291
|
+
target: 'body', // 'head' (default) or 'body'
|
|
292
|
+
anonymizeId: true, // default: true, hides the c15t script id from ad blockers
|
|
293
|
+
nonce: 'abc123', // optional CSP nonce
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
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.
|
|
298
|
+
|
|
299
|
+
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.
|
|
300
|
+
|
|
301
|
+
## Dynamic Management
|
|
302
|
+
|
|
303
|
+
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:
|
|
304
|
+
|
|
305
|
+
* `setScripts(scripts)` — registers script definitions and immediately evaluates them against consent.
|
|
306
|
+
* `removeScript(id)` — removes a definition and unloads its element if needed.
|
|
307
|
+
* `isScriptLoaded(id)` — returns whether c15t has loaded a script.
|
|
308
|
+
* `getLoadedScriptIds()` — returns every currently loaded script id.
|
|
309
|
+
|
|
310
|
+
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.
|
|
311
|
+
|
|
312
|
+
## Calling Vendor APIs From Your App
|
|
313
|
+
|
|
314
|
+
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:
|
|
315
|
+
|
|
316
|
+
|Vendor pattern|What c15t does|What your app code must do|
|
|
317
|
+
|--|--|--|
|
|
318
|
+
|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.|
|
|
319
|
+
|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.|
|
|
320
|
+
|`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.|
|
|
321
|
+
|No app-facing API (e.g. Cloudflare Web Analytics)|Script in/out of DOM based on consent. Tracking is fully automatic.|Nothing to guard.|
|
|
322
|
+
|
|
323
|
+
The safe pattern in React is to read consent state through `useConsentManager().has(category)` before calling the SDK:
|
|
324
|
+
|
|
325
|
+
```tsx
|
|
326
|
+
import { useCallback } from 'react';
|
|
327
|
+
import { useConsentManager } from '@c15t/react';
|
|
328
|
+
|
|
329
|
+
function useTrackSignup() {
|
|
330
|
+
const { has } = useConsentManager();
|
|
331
|
+
|
|
332
|
+
return useCallback(() => {
|
|
333
|
+
if (has('measurement')) {
|
|
334
|
+
window.fathom?.trackEvent('signup');
|
|
335
|
+
}
|
|
336
|
+
}, [has]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function SignupButton() {
|
|
340
|
+
const trackSignup = useTrackSignup();
|
|
341
|
+
|
|
342
|
+
return <button onClick={trackSignup}>Sign up</button>;
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
From non-React code, read the consent store directly:
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
import { getOrCreateConsentRuntime } from 'c15t';
|
|
350
|
+
|
|
351
|
+
const { consentStore } = getOrCreateConsentRuntime();
|
|
352
|
+
|
|
353
|
+
if (consentStore.getState().has('measurement')) {
|
|
354
|
+
window.fathom?.trackEvent('signup');
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Each [integration page](/docs/integrations/overview) includes a vendor-specific **Tracking events in your app** block that names which pattern applies.
|
|
359
|
+
|
|
360
|
+
## Debugging Checklist
|
|
361
|
+
|
|
362
|
+
When a script does not behave as expected:
|
|
363
|
+
|
|
364
|
+
1. Confirm the script's `category` matches the consent that has been granted.
|
|
365
|
+
2. Check whether the script is `alwaysLoad` or consent-gated.
|
|
366
|
+
3. Confirm `onBeforeLoad` creates any globals before the vendor code reads them.
|
|
367
|
+
4. Confirm `onConsentChange` updates persisted or always-loaded scripts when consent changes.
|
|
368
|
+
5. Check whether the browser or an ad blocker blocked the request.
|
|
369
|
+
6. Use c15t devtools to inspect script lifecycle events when available.
|
|
370
|
+
|
|
371
|
+
## Dynamic Script Management
|
|
372
|
+
|
|
373
|
+
The shared guide above lists what the script-manager methods do. In React they are exposed through `useConsentManager()`:
|
|
374
|
+
|
|
375
|
+
```tsx
|
|
376
|
+
import { useConsentManager } from '@c15t/react';
|
|
377
|
+
|
|
378
|
+
function ScriptManager() {
|
|
379
|
+
const {
|
|
380
|
+
setScripts,
|
|
381
|
+
removeScript,
|
|
382
|
+
isScriptLoaded,
|
|
383
|
+
getLoadedScriptIds,
|
|
384
|
+
} = useConsentManager();
|
|
385
|
+
|
|
386
|
+
// ...
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
Register dynamic scripts from an effect or event handler — never directly in the render body. Effects guarantee the call runs once per dependency change and gives you a tear-down path:
|
|
391
|
+
|
|
392
|
+
```tsx
|
|
393
|
+
import { useEffect } from 'react';
|
|
394
|
+
import { useConsentManager } from '@c15t/react';
|
|
395
|
+
|
|
396
|
+
export function TenantAnalytics({ siteId }: { siteId: string }) {
|
|
397
|
+
const { setScripts, removeScript } = useConsentManager();
|
|
398
|
+
|
|
399
|
+
useEffect(() => {
|
|
400
|
+
const scriptId = `tenant-analytics-${siteId}`;
|
|
401
|
+
|
|
402
|
+
setScripts([
|
|
403
|
+
{
|
|
404
|
+
id: scriptId,
|
|
405
|
+
src: `https://cdn.example.com/${siteId}.js`,
|
|
406
|
+
category: 'measurement',
|
|
407
|
+
},
|
|
408
|
+
]);
|
|
409
|
+
|
|
410
|
+
return () => {
|
|
411
|
+
removeScript(scriptId);
|
|
412
|
+
};
|
|
413
|
+
}, [siteId, setScripts, removeScript]);
|
|
414
|
+
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Renderable Integrations
|
|
420
|
+
|
|
421
|
+
Some vendors need a render surface as well as a script — maps, video players, calendars, and checkout widgets all fall in this bucket.
|
|
422
|
+
|
|
423
|
+
Use the built-in [Google Maps](/docs/integrations/google-maps) and
|
|
424
|
+
[YouTube](/docs/integrations/youtube) renderable helpers when they fit:
|
|
425
|
+
|
|
426
|
+
```tsx
|
|
427
|
+
import { GoogleMap, YouTubeEmbed } from '@c15t/react';
|
|
428
|
+
|
|
429
|
+
function Page() {
|
|
430
|
+
const googleMapsApiKey = import.meta.env.VITE_GOOGLE_MAPS_API_KEY;
|
|
431
|
+
|
|
432
|
+
return (
|
|
433
|
+
<>
|
|
434
|
+
{googleMapsApiKey && (
|
|
435
|
+
<GoogleMap
|
|
436
|
+
apiKey={googleMapsApiKey}
|
|
437
|
+
authReferrerPolicy="origin"
|
|
438
|
+
center={{ lat: 40.7128, lng: -74.006 }}
|
|
439
|
+
className="overflow-hidden rounded-xl"
|
|
440
|
+
consentCategory="measurement"
|
|
441
|
+
mapId="YOUR_MAP_ID"
|
|
442
|
+
zoom={12}
|
|
443
|
+
/>
|
|
444
|
+
)}
|
|
445
|
+
|
|
446
|
+
<YouTubeEmbed
|
|
447
|
+
consentCategory="marketing"
|
|
448
|
+
params={{ controls: true, playsinline: true }}
|
|
449
|
+
title="Product demo"
|
|
450
|
+
videoId="dQw4w9WgXcQ"
|
|
451
|
+
/>
|
|
452
|
+
</>
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
The design splits the problem into three layers:
|
|
458
|
+
|
|
459
|
+
1. Use the script loader to gate and load the shared SDK once.
|
|
460
|
+
2. Use `useConsentScript()` for SDK readiness when a vendor uses a callback-style loader.
|
|
461
|
+
3. Create the widget instance only after the SDK is ready and clean it up on unmount.
|
|
462
|
+
|
|
463
|
+
Google Maps is SDK-backed, so `GoogleMap` registers one shared Google Maps script through the c15t script manager and waits for the callback-shaped SDK readiness signal before creating each map instance. It uses the official Google Maps types and accepts the direct-loader options `libraries`, `language`, `region`, `version`, `authReferrerPolicy`, `mapIds`, `channel`, and `solutionChannel`.
|
|
464
|
+
|
|
465
|
+
YouTube is iframe-only, so `YouTubeEmbed` builds on the existing `Frame` consent boundary instead of loading the YouTube iframe API. That keeps iframe media separate from SDK widgets and avoids a second source of truth for script readiness.
|
|
466
|
+
|
|
467
|
+
`GoogleMap` has a default height of `320px`; override `style.height` or supply a class that sets an inline-compatible height when your layout needs another size. `mapId` is construction-time configuration in the Google Maps API, so changing it recreates the map. Other controlled values such as `center`, `zoom`, and updateable `options` are applied to the existing instance.
|
|
468
|
+
|
|
469
|
+
The Maps JavaScript API is a page singleton. Before consent, c15t makes no Maps script request. After the first approved load, the SDK registration is retained for the consent-manager lifetime to avoid Google's duplicate-loader warning; map instances are still destroyed as soon as the component unmounts or consent becomes unavailable. All `GoogleMap` instances that share a `scriptId` must therefore use the same loader options. If another part of the application already loaded Maps, the component adopts that global API without injecting a second script.
|
|
470
|
+
|
|
471
|
+
`GoogleMap` reports network, timeout, constructor, and Google's global authentication failures through `onError` and `errorFallback`. Authentication errors commonly mean the browser key, billing, or allowed referrers need attention.
|
|
472
|
+
|
|
473
|
+
`YouTubeEmbed` defaults to a responsive, borderless 16:9 frame with stable
|
|
474
|
+
placeholder dimensions, a localized loading state, native lazy loading, and
|
|
475
|
+
the privacy-enhanced `youtube-nocookie.com` host. For overrides, `className` and
|
|
476
|
+
the forwarded ref target the iframe while `wrapperClassName` and `frameProps`
|
|
477
|
+
target the consent-gated `Frame`. Boolean values in `params` are serialized as
|
|
478
|
+
YouTube's documented `1` and `0` values.
|
|
479
|
+
|
|
480
|
+
For custom SDK-backed widgets, call `useConsentScript()` with a c15t `Script`
|
|
481
|
+
object. The hook uses the consent manager as the source of truth, shares
|
|
482
|
+
compatible registrations inside that provider, and exposes status, consent,
|
|
483
|
+
the ready SDK value, errors, and a promise:
|
|
484
|
+
|
|
485
|
+
```tsx
|
|
486
|
+
import { useState } from 'react';
|
|
487
|
+
import { useConsentScript } from '@c15t/react';
|
|
488
|
+
|
|
489
|
+
export function VendorWidget() {
|
|
490
|
+
const [retryKey, setRetryKey] = useState(0);
|
|
491
|
+
const vendor = useConsentScript({
|
|
492
|
+
script: {
|
|
493
|
+
id: 'vendor-sdk',
|
|
494
|
+
src: 'https://cdn.example.com/sdk.js',
|
|
495
|
+
category: 'measurement',
|
|
496
|
+
},
|
|
497
|
+
resolveReady: () => window.vendorSdk ?? false,
|
|
498
|
+
registerReadyCallback: ({ resolve }) => {
|
|
499
|
+
window.onVendorReady = () => resolve(window.vendorSdk);
|
|
500
|
+
|
|
501
|
+
return () => {
|
|
502
|
+
delete window.onVendorReady;
|
|
503
|
+
};
|
|
504
|
+
},
|
|
505
|
+
readinessKey: 'vendor-sdk-v1',
|
|
506
|
+
retryKey,
|
|
507
|
+
timeoutMs: 10_000,
|
|
508
|
+
unmountBehavior: 'keep',
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
if (vendor.status === 'blocked') {
|
|
512
|
+
return <p>Enable Analytics consent to use this widget.</p>;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (vendor.status === 'loading') {
|
|
516
|
+
return <p role="status">Loading widget…</p>;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (vendor.status === 'error') {
|
|
520
|
+
return (
|
|
521
|
+
<div role="alert">
|
|
522
|
+
<p>The widget could not be loaded.</p>
|
|
523
|
+
<button type="button" onClick={() => setRetryKey((key) => key + 1)}>
|
|
524
|
+
Retry
|
|
525
|
+
</button>
|
|
526
|
+
</div>
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (vendor.status !== 'ready' || !vendor.readyValue) {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return <div ref={(node) => node && vendor.readyValue.mount(node)} />;
|
|
535
|
+
}
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
Inline script objects and lifecycle callbacks are safe. The hook compares the complete non-function script configuration, reads callbacks from the latest render, adopts a compatible script already owned by the provider, and never removes a registration it did not create. Use `unmountBehavior: 'remove'` (the default) for route-local scripts, or `'keep'` for a genuine page-level singleton.
|
|
539
|
+
|
|
540
|
+
Change `retryKey` after a failed attempt to retry the same registration.
|
|
541
|
+
Consumers sharing a script id must use identical script options,
|
|
542
|
+
`readinessKey`, timeout, and unmount behavior. Conflicts throw
|
|
543
|
+
`ConsentScriptConflictError`; choose another id only when the vendor genuinely
|
|
544
|
+
supports multiple page-level loaders.
|
|
545
|
+
|
|
546
|
+
For iframe-only embeds, use the [iframe blocking](/docs/frameworks/react/iframe-blocking) pattern or `YouTubeEmbed` instead of loading a JavaScript SDK to hide an iframe. For SDK-backed widgets, treat the SDK as a singleton and each rendered component as its own instance.
|
|
547
|
+
|
|
548
|
+
## API Reference
|
|
549
|
+
|
|
550
|
+
|Property|Value|
|
|
551
|
+
|:--|:--|
|
|
552
|
+
|Type Name|\`Script\`|
|
|
553
|
+
|Source Path|\`./packages/core/src/libs/script-loader/types.ts\`|
|
|
554
|
+
|
|
555
|
+
\*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.\*
|