@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,258 +1,397 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: PostHog
|
|
3
|
-
description: PostHog
|
|
4
|
-
|
|
5
|
-
cookieless tracking, allowing analytics to continue even without cookie
|
|
6
|
-
consent.
|
|
7
|
-
icon: posthog
|
|
3
|
+
description: Choose PostHog loading and cookieless behavior, configure the
|
|
4
|
+
region, and synchronize v3 permissions.
|
|
8
5
|
group: integrations
|
|
9
6
|
---
|
|
10
|
-
PostHog is an open-source product analytics platform that helps you understand user behavior, track events, and analyze product usage. Unlike traditional analytics tools, PostHog supports both cookieless and cookie-based tracking. This means you can sync c15t with PostHog and, when your PostHog project is configured for cookieless tracking, continue collecting privacy-preserving analytics after a user rejects measurement consent.
|
|
11
7
|
|
|
12
|
-
|
|
8
|
+
## Configure PostHog
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
Use the browser project token beginning with `phc_`. Select the region matching
|
|
11
|
+
your PostHog project. This example waits for measurement permission before
|
|
12
|
+
requesting the SDK.
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
| Package manager | Command |
|
|
15
|
+
| :-------------- | :-------------------------- |
|
|
16
|
+
| npm | `npm install @c15t/scripts` |
|
|
17
|
+
| pnpm | `pnpm add @c15t/scripts` |
|
|
18
|
+
| yarn | `yarn add @c15t/scripts` |
|
|
19
|
+
| bun | `bun add @c15t/scripts` |
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
```ts title="src/consent-scripts.ts"
|
|
22
|
+
import { posthog } from '@c15t/scripts/posthog';
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
export const scripts = [
|
|
25
|
+
posthog({
|
|
26
|
+
id: 'phc_YOUR_PROJECT_TOKEN',
|
|
27
|
+
region: 'eu',
|
|
28
|
+
loadMode: 'after-consent',
|
|
29
|
+
initOptions: { cookieless_mode: 'never' },
|
|
30
|
+
}),
|
|
31
|
+
];
|
|
32
|
+
```
|
|
22
33
|
|
|
23
|
-
|
|
34
|
+
The helper initializes PostHog. Remove a separate `posthog-js` initializer,
|
|
35
|
+
framework plugin or array snippet before using this setup.
|
|
24
36
|
|
|
25
|
-
|
|
37
|
+
## Register the scripts
|
|
26
38
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
defaults: "2026-01-30",
|
|
31
|
-
cookieless_mode: 'on_reject'
|
|
32
|
-
})
|
|
39
|
+
Complete your [framework quickstart](https://c15t.com/docs/frameworks) first. Keep its Inth
|
|
40
|
+
endpoint, policy, styles and consent UI. Remove the vendor's original script,
|
|
41
|
+
SDK initializer or tag-manager entry so c15t owns loading once.
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
The `scripts` export in `src/consent-scripts.ts` is a configuration, not an
|
|
44
|
+
initializer. Add it to your existing consent owner using the registration point
|
|
45
|
+
below. These are partial edits to that owner, not additional providers.
|
|
36
46
|
|
|
37
|
-
|
|
47
|
+
**Next.js**
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
Import the configuration into the client boundary from your router guide:
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
> To wrap this in your framework, pass the callbacks option to your ConsentManagerProvider the same way you would pass scripts — see the JavaScript, React, or Next.js script loader guide.
|
|
51
|
+
```ts
|
|
52
|
+
import { ConsentRoot } from 'c15t/next';
|
|
53
|
+
import { scripts } from './consent-scripts';
|
|
54
|
+
```
|
|
46
55
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function syncPostHogMeasurementConsent(hasMeasurementConsent: boolean) {
|
|
52
|
-
if (hasMeasurementConsent) {
|
|
53
|
-
posthog.opt_in_capturing();
|
|
54
|
-
} else {
|
|
55
|
-
posthog.opt_out_capturing();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const runtime = getOrCreateConsentRuntime({
|
|
60
|
-
mode: 'hosted',
|
|
61
|
-
callbacks: {
|
|
62
|
-
onBannerFetched() {
|
|
63
|
-
syncPostHogMeasurementConsent(
|
|
64
|
-
runtime.consentStore.getState().has('measurement')
|
|
65
|
-
);
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
runtime.consentStore
|
|
71
|
-
.getState()
|
|
72
|
-
.subscribeToConsentChanges(({ allowedCategories }) => {
|
|
73
|
-
syncPostHogMeasurementConsent(
|
|
74
|
-
allowedCategories.includes('measurement')
|
|
75
|
-
);
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
> ℹ️ Info:
|
|
80
|
-
> Avoid using onConsentSet plus manual deduplication for PostHog. subscribeToConsentChanges() already gives you the exact change-only semantics most analytics SDKs need.
|
|
81
|
-
|
|
82
|
-
### Script helper pattern
|
|
83
|
-
|
|
84
|
-
If you want to load PostHog via a script tag, it's recommended to use this approach.
|
|
85
|
-
|
|
86
|
-
1. **Choose a region and loading mode** The c15t helper seeds PostHog's initialization queue, loads the bootstrap script, and synchronizes consent through `posthog.opt_in_capturing()` / `posthog.opt_out_capturing()`. You do not need to call `posthog.init()` separately.
|
|
87
|
-
|
|
88
|
-
Use region to keep PostHog's API, UI, and bootstrap script hosts aligned. c15t defaults to region: 'eu'; set region: 'us' for PostHog Cloud US. You can still pass apiHost, uiHost, or scriptUrl for self-hosted or proxied setups.
|
|
89
|
-
|
|
90
|
-
The helper supports three loading modes:
|
|
91
|
-
|
|
92
|
-
* `loadMode: 'always'` — the backwards-compatible default. PostHog loads immediately and c15t synchronizes measurement consent through PostHog's APIs. Use this when you intentionally want PostHog cookieless behavior after rejection.
|
|
93
|
-
* `loadMode: 'after-consent'` — PostHog is not requested until measurement consent is granted. This is the recommended GDPR/EU cookie-banner default when your policy requires no PostHog network activity before consent.
|
|
94
|
-
* `loadMode: 'disabled'` — returns an inert callback-only script with no PostHog network request. Use this for environment flags or temporary rollouts.
|
|
95
|
-
|
|
96
|
-
> ℹ️ Info:
|
|
97
|
-
> For a privacy-first GDPR/EU cookie-banner setup, use region: 'eu' with loadMode: 'after-consent'. This keeps PostHog Cloud in the EU region and prevents any PostHog script request until measurement consent is granted.
|
|
98
|
-
|
|
99
|
-
The helper includes these PostHog init defaults:
|
|
100
|
-
|
|
101
|
-
```ts
|
|
102
|
-
const initOptions = {
|
|
103
|
-
api_host: 'https://eu.i.posthog.com',
|
|
104
|
-
ui_host: 'https://eu.posthog.com',
|
|
105
|
-
defaults: '2026-01-30',
|
|
106
|
-
cookieless_mode: 'on_reject',
|
|
107
|
-
};
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
You can still override any of these through initOptions. Keep cookieless\_mode: 'on\_reject' when you want PostHog to use cookieless capture after a consent rejection.
|
|
111
|
-
|
|
112
|
-
2. **Enable cookieless tracking in PostHog** Enable **Cookieless server hash mode** in your PostHog project under **Project Settings** > **Web analytics** when you use `loadMode: 'always'` and want rejected-consent traffic to be recorded cookielessly. This is required by PostHog before cookieless events are accepted.
|
|
113
|
-
|
|
114
|
-
3. **Add the script helper**
|
|
115
|
-
|
|
116
|
-
import \{ type ReactNode } from 'react';
|
|
117
|
-
import \{ ConsentManagerProvider } from '@c15t/react';
|
|
118
|
-
import \{ posthog } from '@c15t/scripts/posthog';
|
|
119
|
-
|
|
120
|
-
const scripts = \[
|
|
121
|
-
 posthog(\{
|
|
122
|
-
 id: 'phc\_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
123
|
-
 region: 'eu',
|
|
124
|
-
 loadMode: 'after-consent',
|
|
125
|
-
 }),
|
|
126
|
-
];
|
|
127
|
-
|
|
128
|
-
export function ConsentProvider(\{ children }: \{ children: ReactNode }) \{
|
|
129
|
-
 return (
|
|
130
|
-
 \<ConsentManagerProvider
|
|
131
|
-
 options=\{\{
|
|
132
|
-
 mode: 'hosted',
|
|
133
|
-
 backendURL: 'https\://your-instance.c15t.dev',
|
|
134
|
-
 scripts,
|
|
135
|
-
 }}
|
|
136
|
-
 \>
|
|
137
|
-
 \{children}
|
|
138
|
-
 \</ConsentManagerProvider>
|
|
139
|
-
 );
|
|
140
|
-
}'use client';
|
|
141
|
-
|
|
142
|
-
import \{ type ReactNode } from 'react';
|
|
143
|
-
import \{ ConsentManagerProvider } from '@c15t/nextjs';
|
|
144
|
-
import \{ posthog } from '@c15t/scripts/posthog';
|
|
145
|
-
|
|
146
|
-
const scripts = \[
|
|
147
|
-
 posthog(\{
|
|
148
|
-
 id: 'phc\_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
149
|
-
 region: 'eu',
|
|
150
|
-
 loadMode: 'after-consent',
|
|
151
|
-
 }),
|
|
152
|
-
];
|
|
153
|
-
|
|
154
|
-
export function ConsentProvider(\{ children }: \{ children: ReactNode }) \{
|
|
155
|
-
 return (
|
|
156
|
-
 \<ConsentManagerProvider
|
|
157
|
-
 options=\{\{
|
|
158
|
-
 mode: 'hosted',
|
|
159
|
-
 backendURL: '/api/c15t',
|
|
160
|
-
 scripts,
|
|
161
|
-
 }}
|
|
162
|
-
 \>
|
|
163
|
-
 \{children}
|
|
164
|
-
 \</ConsentManagerProvider>
|
|
165
|
-
 );
|
|
166
|
-
}import \{ getOrCreateConsentRuntime } from 'c15t';
|
|
167
|
-
import \{ posthog } from '@c15t/scripts/posthog';
|
|
168
|
-
|
|
169
|
-
getOrCreateConsentRuntime(\{
|
|
170
|
-
 mode: 'hosted',
|
|
171
|
-
 backendURL: 'https\://your-instance.c15t.dev',
|
|
172
|
-
 scripts: \[
|
|
173
|
-
 posthog(\{
|
|
174
|
-
 id: 'phc\_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
175
|
-
 region: 'eu',
|
|
176
|
-
 loadMode: 'after-consent',
|
|
177
|
-
 }),
|
|
178
|
-
 ],
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
### No PostHog request before consent
|
|
182
|
-
|
|
183
|
-
If your policy requires PostHog to be completely absent until the user grants measurement consent, use `loadMode: 'after-consent'`:
|
|
56
|
+
Keep the server-resolved `state` and shared `consentConfig` from your
|
|
57
|
+
router guide. Its manifest, init and save URLs stay in effect. Add
|
|
58
|
+
`scripts` as a top-level prop on the existing root:
|
|
184
59
|
|
|
185
|
-
```
|
|
186
|
-
|
|
60
|
+
```tsx
|
|
61
|
+
<ConsentRoot state={state} config={consentConfig} scripts={scripts}>
|
|
62
|
+
{children}
|
|
63
|
+
</ConsentRoot>
|
|
64
|
+
```
|
|
187
65
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
66
|
+
For a Pages Router or static-export setup using `ConsentProvider`, add
|
|
67
|
+
`scripts` to its existing `options` instead. Keep the router-specific setup
|
|
68
|
+
from [Next.js script loading](../frameworks/next/script-loader.md).
|
|
69
|
+
|
|
70
|
+
**TanStack Start**
|
|
71
|
+
|
|
72
|
+
In your existing root route component, import the scripts alongside
|
|
73
|
+
`ConsentRoot`. Keep the server loader from the [TanStack Start quickstart](https://c15t.com/docs/frameworks/tanstack-start/quickstart).
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { Outlet } from '@tanstack/react-router';
|
|
77
|
+
import { ConsentRoot } from 'c15t/tanstack-start';
|
|
78
|
+
import { scripts } from '../consent-scripts';
|
|
79
|
+
|
|
80
|
+
function Root() {
|
|
81
|
+
const state = Route.useLoaderData();
|
|
82
|
+
return (
|
|
83
|
+
<ConsentRoot state={state} backendURL={backendURL} initRoute={false} scripts={scripts}>
|
|
84
|
+
<Outlet />
|
|
85
|
+
{/* Keep your consent banner, dialog and preferences link here. */}
|
|
86
|
+
</ConsentRoot>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
195
89
|
```
|
|
196
90
|
|
|
197
|
-
|
|
91
|
+
This edits the existing route. `Route` and `backendURL` come from its setup;
|
|
92
|
+
keep the document shell and head components if they are part of your root.
|
|
93
|
+
`initRoute={false}` keeps the quickstart's direct-backend initialization.
|
|
94
|
+
If your app mounts a consent server route, retain its existing `initRoute`
|
|
95
|
+
instead. Do not return script callbacks from a server function or route loader.
|
|
198
96
|
|
|
199
|
-
|
|
97
|
+
**React**
|
|
98
|
+
|
|
99
|
+
Import the scripts into your existing provider component:
|
|
200
100
|
|
|
201
101
|
```ts
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
102
|
+
import { ConsentProvider } from 'c15t/react';
|
|
103
|
+
import { scripts } from './consent-scripts';
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Keep the existing options and add `scripts`:
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
<ConsentProvider options={{ ...consentOptions, scripts }}>
|
|
110
|
+
{children}
|
|
111
|
+
</ConsentProvider>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Here `consentOptions` is your existing configuration, including
|
|
115
|
+
`mode: hosted({ url: backendURL })`. Keep the banner, dialog and preferences
|
|
116
|
+
link inside the provider. See [React script loading](../frameworks/react/script-loader.md).
|
|
117
|
+
|
|
118
|
+
**Nuxt**
|
|
119
|
+
|
|
120
|
+
Attach one loader from the root `app.vue`, after the Nuxt module has
|
|
121
|
+
started its browser runtime. This keeps vendor callbacks in application code rather
|
|
122
|
+
than serialized `nuxt.config.ts` runtime configuration.
|
|
123
|
+
|
|
124
|
+
```vue title="app/app.vue"
|
|
125
|
+
<script setup lang="ts">
|
|
126
|
+
import { onUnmounted } from 'vue';
|
|
127
|
+
import { createScriptLoader } from 'c15t/modules/script-loader';
|
|
128
|
+
import { scripts } from '../src/consent-scripts';
|
|
129
|
+
|
|
130
|
+
const nuxtApp = useNuxtApp();
|
|
131
|
+
const kernel = useConsentKernel();
|
|
132
|
+
let loader: ReturnType<typeof createScriptLoader> | undefined;
|
|
133
|
+
|
|
134
|
+
const removeMountedHook = nuxtApp.hook('app:mounted', () => {
|
|
135
|
+
loader = createScriptLoader({ kernel, scripts });
|
|
136
|
+
});
|
|
137
|
+
onUnmounted(() => {
|
|
138
|
+
removeMountedHook();
|
|
139
|
+
loader?.dispose();
|
|
140
|
+
});
|
|
141
|
+
</script>
|
|
142
|
+
|
|
143
|
+
<template>
|
|
144
|
+
<ConsentRoot />
|
|
145
|
+
<NuxtPage />
|
|
146
|
+
</template>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Merge the setup code into your root and retain its footer and preferences
|
|
150
|
+
link. `useConsentKernel` is auto-imported by the c15t Nuxt module. Adjust the
|
|
151
|
+
relative script import if your `app.vue` is at the project root. This loader
|
|
152
|
+
waits until the module has applied browser persistence and privacy signals,
|
|
153
|
+
then reads the current snapshot and observes future changes. Do not also register these scripts
|
|
154
|
+
in another loader. See the [Nuxt quickstart](https://c15t.com/docs/frameworks/nuxt/quickstart).
|
|
155
|
+
|
|
156
|
+
**Vue**
|
|
157
|
+
|
|
158
|
+
Use the kernel already provided by the Vue plugin. Merge this setup into
|
|
159
|
+
`App.vue`, whose lifetime covers the application:
|
|
160
|
+
|
|
161
|
+
```vue title="src/App.vue"
|
|
162
|
+
<script setup lang="ts">
|
|
163
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
164
|
+
import { createScriptLoader } from 'c15t/modules/script-loader';
|
|
165
|
+
import { useConsentKernel } from 'c15t/vue/vue-plugin';
|
|
166
|
+
import ConsentRoot from 'c15t/vue/consent-root';
|
|
167
|
+
import { scripts } from './consent-scripts';
|
|
168
|
+
|
|
169
|
+
const kernel = useConsentKernel();
|
|
170
|
+
let loader: ReturnType<typeof createScriptLoader> | undefined;
|
|
171
|
+
|
|
172
|
+
onMounted(() => {
|
|
173
|
+
loader = createScriptLoader({ kernel, scripts });
|
|
205
174
|
});
|
|
175
|
+
onUnmounted(() => loader?.dispose());
|
|
176
|
+
</script>
|
|
177
|
+
|
|
178
|
+
<template>
|
|
179
|
+
<ConsentRoot />
|
|
180
|
+
<main>Your application</main>
|
|
181
|
+
</template>
|
|
206
182
|
```
|
|
207
183
|
|
|
208
|
-
|
|
184
|
+
Keep your existing page content and preferences link. The plugin still owns
|
|
185
|
+
the kernel and persistence; this component owns only the vendor loader.
|
|
186
|
+
Do not register the same scripts in plugin configuration as well. See the
|
|
187
|
+
[Vue quickstart](https://c15t.com/docs/frameworks/vue/quickstart).
|
|
209
188
|
|
|
210
|
-
|
|
211
|
-
* **SDK pattern:** your app loads `posthog-js`; c15t synchronizes
|
|
212
|
-
measurement consent with PostHog opt-in and opt-out APIs.
|
|
213
|
-
* **Script helper pattern:** by default, c15t loads PostHog on page start with
|
|
214
|
-
[`alwaysLoad`](/docs/frameworks/react/script-loader#always-load), then
|
|
215
|
-
switches PostHog between cookie-based and cookieless capture as consent
|
|
216
|
-
changes. Set `loadMode: 'after-consent'` to block the script request until
|
|
217
|
-
measurement consent is granted.
|
|
189
|
+
**Astro**
|
|
218
190
|
|
|
219
|
-
|
|
191
|
+
Point the existing Astro integration at a client module. Keep its `mode`,
|
|
192
|
+
`ui` and framework integration from the [Astro quickstart](https://c15t.com/docs/frameworks/astro/quickstart).
|
|
193
|
+
Import `fileURLToPath` in your Astro configuration:
|
|
220
194
|
|
|
221
|
-
|
|
195
|
+
```js title="astro.config.mjs"
|
|
196
|
+
import { fileURLToPath } from 'node:url';
|
|
197
|
+
```
|
|
222
198
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
* **Script Implementation with `loadMode: 'after-consent'`** — PostHog is unavailable until measurement consent is granted. Guard `posthog.capture(...)` calls or call them only after consent.
|
|
199
|
+
Add this option to the existing `c15t({ ... })` call. Resolve the path from
|
|
200
|
+
the configuration file because Astro injects the import into a virtual module:
|
|
226
201
|
|
|
227
|
-
|
|
228
|
-
|
|
202
|
+
```js
|
|
203
|
+
clientEntrypoint: fileURLToPath(new URL('./src/c15t.client.ts', import.meta.url)),
|
|
204
|
+
```
|
|
229
205
|
|
|
230
|
-
|
|
231
|
-
|
|
206
|
+
Export the scripts from that module:
|
|
207
|
+
|
|
208
|
+
```ts title="src/c15t.client.ts"
|
|
209
|
+
import type { C15tClientOptionsExtension } from '@c15t/astro';
|
|
210
|
+
import { scripts } from './consent-scripts';
|
|
211
|
+
|
|
212
|
+
export default { scripts } satisfies C15tClientOptionsExtension;
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The integration passes this extension to its shared browser runtime. Vendor
|
|
216
|
+
helpers contain callbacks, so do not put them in the serialized `scripts`
|
|
217
|
+
option in `astro.config.mjs`. Keep one runtime across consent islands and
|
|
218
|
+
`ClientRouter` navigation.
|
|
219
|
+
|
|
220
|
+
**Svelte**
|
|
221
|
+
|
|
222
|
+
Import the scripts in the component that owns your existing provider and
|
|
223
|
+
pass them as a top-level prop:
|
|
224
|
+
|
|
225
|
+
```svelte title="src/App.svelte"
|
|
226
|
+
<script lang="ts">
|
|
227
|
+
import { ConsentManagerProvider, hosted } from '@c15t/svelte';
|
|
228
|
+
import { scripts } from './consent-scripts';
|
|
229
|
+
|
|
230
|
+
const backendURL = import.meta.env.VITE_C15T_BACKEND_URL;
|
|
231
|
+
if (!backendURL) throw new Error('Set VITE_C15T_BACKEND_URL');
|
|
232
|
+
const mode = hosted({ url: backendURL });
|
|
233
|
+
</script>
|
|
234
|
+
|
|
235
|
+
<ConsentManagerProvider {mode} {scripts}>
|
|
236
|
+
<!-- Keep your application, consent UI and preferences link here. -->
|
|
237
|
+
</ConsentManagerProvider>
|
|
232
238
|
```
|
|
233
239
|
|
|
234
|
-
|
|
240
|
+
Retain the styles and consent UI from the [Svelte quickstart](https://c15t.com/docs/frameworks/svelte/quickstart).
|
|
241
|
+
The provider owns the loader and disposes it on unmount.
|
|
235
242
|
|
|
236
|
-
|
|
243
|
+
**SvelteKit**
|
|
237
244
|
|
|
238
|
-
|
|
245
|
+
Add the scripts to the existing root layout provider. Keep the server load
|
|
246
|
+
and its serializable prefetch data from the [SvelteKit quickstart](https://c15t.com/docs/frameworks/sveltekit/quickstart).
|
|
239
247
|
|
|
240
|
-
|
|
248
|
+
```svelte title="src/routes/+layout.svelte"
|
|
249
|
+
<script lang="ts">
|
|
250
|
+
import { ConsentManagerProvider, hosted } from '@c15t/svelte';
|
|
251
|
+
import { scripts } from '../consent-scripts';
|
|
241
252
|
|
|
242
|
-
|
|
253
|
+
let { children, data } = $props();
|
|
254
|
+
const mode = hosted({ url: data.backendURL });
|
|
255
|
+
</script>
|
|
256
|
+
|
|
257
|
+
<ConsentManagerProvider {mode} {scripts} prefetch={data.prefetch}>
|
|
258
|
+
{@render children()}
|
|
259
|
+
<!-- Keep your consent UI and preferences link here. -->
|
|
260
|
+
</ConsentManagerProvider>
|
|
261
|
+
```
|
|
243
262
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
263
|
+
Import vendor helpers in the layout component, not in `+layout.server.ts`.
|
|
264
|
+
For static hosting, keep your browser-only `mode` setup and omit request
|
|
265
|
+
prefetch; the `scripts` prop stays the same. If you pass an externally owned
|
|
266
|
+
`runtime` to the provider, register scripts when creating that runtime instead.
|
|
248
267
|
|
|
249
|
-
|
|
268
|
+
**JavaScript**
|
|
250
269
|
|
|
251
|
-
|
|
270
|
+
Attach the loader to your existing kernel before calling
|
|
271
|
+
`kernel.commands.init()`:
|
|
252
272
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
273
|
+
```ts
|
|
274
|
+
import { createScriptLoader } from 'c15t/modules/script-loader';
|
|
275
|
+
import { scripts } from './consent-scripts';
|
|
276
|
+
|
|
277
|
+
const loader = createScriptLoader({ kernel, scripts });
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Call `loader.dispose()` when that application instance is destroyed.
|
|
281
|
+
`kernel` is the hosted kernel from your quickstart. A provider-owned kernel
|
|
282
|
+
already has a loader; do not attach a second one. See
|
|
283
|
+
[JavaScript script loading](../frameworks/javascript/script-loader.md).
|
|
284
|
+
|
|
285
|
+
## Choose loading and capture behavior separately
|
|
286
|
+
|
|
287
|
+
| `loadMode` | SDK loading | Consent handling |
|
|
288
|
+
| ------------------------------ | ------------------------------------- | --------------------------------------------------------------- |
|
|
289
|
+
| `'after-consent'` | Waits for measurement permission | Calls PostHog opt-in and opt-out APIs after load and on changes |
|
|
290
|
+
| `'always'`, the helper default | Loads even while permission is denied | Calls the same consent APIs |
|
|
291
|
+
| `'disabled'` | Does not load PostHog | Does not synchronize consent with an SDK loaded elsewhere |
|
|
292
|
+
|
|
293
|
+
The helper defaults `initOptions.cookieless_mode` to `'on_reject'`. In that
|
|
294
|
+
mode, PostHog can continue cookieless capture after denial. It requires the
|
|
295
|
+
project's cookieless server hash setting. See
|
|
296
|
+
[PostHog's cookieless guide](https://posthog.com/tutorials/cookieless-tracking).
|
|
297
|
+
|
|
298
|
+
The example overrides this with `'never'` so opting out is not a switch to
|
|
299
|
+
cookieless capture. Loading after consent only controls the initial request;
|
|
300
|
+
for later revocation, verify PostHog's opt-out behavior and stop application
|
|
301
|
+
capture calls too. SDK code that already executed cannot be undone by removing
|
|
302
|
+
its script element.
|
|
303
|
+
|
|
304
|
+
## Configure hosts and initialization
|
|
305
|
+
|
|
306
|
+
| Option | Behavior |
|
|
307
|
+
| ------------------------------ | ---------------------------------------------------------------------------------------- |
|
|
308
|
+
| `id` | Browser project token. |
|
|
309
|
+
| `region` | `'eu'` by default; use `'us'` for a US project. |
|
|
310
|
+
| `apiHost / uiHost / scriptUrl` | Explicit overrides for proxy or self-hosted PostHog deployments. Keep all hosts aligned. |
|
|
311
|
+
| `loadMode` | Defaults to `'always'`; choose deliberately. |
|
|
312
|
+
| `initOptions` | Additional serializable SDK options, including the cookieless override. |
|
|
313
|
+
|
|
314
|
+
The helper applies the resolved API and UI hosts after `initOptions`; use the
|
|
315
|
+
top-level host fields to change them. A global `posthog` queue may exist before
|
|
316
|
+
the SDK is ready. Neither API presence nor SDK readiness is a consent grant.
|
|
317
|
+
|
|
318
|
+
## Keep application events behind permission
|
|
319
|
+
|
|
320
|
+
With the script helper registered, a React client component can guard an event
|
|
321
|
+
using the current effective permission:
|
|
322
|
+
|
|
323
|
+
```tsx title="src/signup-event.tsx"
|
|
324
|
+
'use client';
|
|
325
|
+
|
|
326
|
+
import { useConsent } from 'c15t/react';
|
|
327
|
+
|
|
328
|
+
export function SignupEvent() {
|
|
329
|
+
const allowed = useConsent('measurement');
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<button
|
|
333
|
+
type="button"
|
|
334
|
+
onClick={() => {
|
|
335
|
+
if (allowed) window.posthog?.capture('signup_started');
|
|
336
|
+
}}
|
|
337
|
+
>
|
|
338
|
+
Start signup
|
|
339
|
+
</button>
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
This is an event-call example, not a signup implementation. Next.js client
|
|
345
|
+
components can import `useConsent` from `c15t/next`.
|
|
346
|
+
|
|
347
|
+
## Use an existing PostHog SDK
|
|
348
|
+
|
|
349
|
+
If your app already initializes `posthog-js`, keep that SDK owner and attach a
|
|
350
|
+
callback-only script instead of the loading helper. Initialize your SDK with
|
|
351
|
+
`opt_out_capturing_by_default: true` and `cookieless_mode: 'never'` before
|
|
352
|
+
mounting c15t when denied permission must stop capture.
|
|
353
|
+
|
|
354
|
+
```ts title="src/posthog-consent.ts"
|
|
355
|
+
import type { Script, ScriptCallbackInfo } from 'c15t/modules/script-loader';
|
|
356
|
+
|
|
357
|
+
type PostHogConsentApi = {
|
|
358
|
+
opt_in_capturing: () => unknown;
|
|
359
|
+
opt_out_capturing: () => unknown;
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
export function posthogConsent(instance: PostHogConsentApi): Script {
|
|
363
|
+
const sync = ({ hasConsent }: ScriptCallbackInfo) => {
|
|
364
|
+
if (hasConsent) instance.opt_in_capturing();
|
|
365
|
+
else instance.opt_out_capturing();
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
return {
|
|
369
|
+
id: 'posthog-sdk-consent',
|
|
370
|
+
category: 'measurement',
|
|
371
|
+
callbackOnly: true,
|
|
372
|
+
alwaysLoad: true,
|
|
373
|
+
onBeforeLoad: sync,
|
|
374
|
+
onConsentChange: sync,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
```
|
|
257
378
|
|
|
258
|
-
|
|
379
|
+
Pass `posthogConsent(posthog)` in the existing provider's `scripts` array, using
|
|
380
|
+
your initialized SDK instance. The callback-only entry synchronizes initial
|
|
381
|
+
and changed effective permissions without loading a second SDK. It does not
|
|
382
|
+
block the SDK import or its initial request. `loadMode: 'disabled'` on the
|
|
383
|
+
loading helper is not a substitute, because it performs no synchronization.
|
|
384
|
+
|
|
385
|
+
Keep c15t as the source of truth for the banner and saved choices. Effective
|
|
386
|
+
permission can come from policy defaults; a synchronization callback is not
|
|
387
|
+
proof that the visitor explicitly accepted. Do not reuse v2
|
|
388
|
+
`getOrCreateConsentRuntime` or `onBannerFetched` examples with v3.
|
|
389
|
+
|
|
390
|
+
## Verify the integration
|
|
391
|
+
|
|
392
|
+
With the example above, a fresh opt-in session should make no PostHog SDK
|
|
393
|
+
request until measurement is allowed. Grant, capture one test event, revoke,
|
|
394
|
+
and confirm later application calls stop. Repeat with persisted consent and
|
|
395
|
+
client navigation. If you choose cookieless capture, verify that behavior
|
|
396
|
+
explicitly in the project rather than expecting silence after denial. See
|
|
397
|
+
[consent verification](../guides/verify-consent.md).
|