Add a cookie consent banner to Svelte
c15t puts the banner in your Svelte app as code. It holds tags until the visitor chooses and keeps Google Consent Mode v2 in sync.
Install c15t in a Svelte app
c15t is the open source consent library behind Inth Consent. It renders the banner from your own bundle and records each choice in your Inth project.
npm install c15t @c15t/integrationsThe c15t package gives you the Svelte components from c15t/svelte and the Vite plugin from c15t/svelte/vite.
Add your Inth project URL to .env. Copy it from your Inth dashboard. Vite reads it at build time, so set it before you build.
.env
VITE_C15T_BACKEND_URL=https://your-project.inth.appAdd consentManifest to the plugins in vite.config.ts. It gives the backend URL to hosted() and writes no file into your project.
vite.config.ts
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { consentManifest } from 'c15t/svelte/vite';
import { defineConfig } from 'vite';
export default defineConfig({ plugins: [consentManifest(), svelte()] });Code on this page comes from the c15t Svelte quickstart, checked 28 October 2026.
Do you use SvelteKit? See the SvelteKit cookie consent banner. It puts the banner in the server HTML.
Mount the provider in App.svelte
Render ConsentProvider around your app, with the vendor scripts that c15t loads. The provider starts consent when it mounts, so keep it at the root of the app.
src/App.svelte
<script lang="ts">
import { posthog } from '@c15t/integrations/posthog';
import {
ConsentBanner,
ConsentDialog,
ConsentDialogLink,
ConsentProvider,
hosted,
} from 'c15t/svelte';
const scripts = [
posthog({
id: 'phc_your_project_key',
initOptions: { cookieless_mode: 'never' },
loadMode: 'after-consent',
}),
];
</script>
<ConsentProvider mode={hosted()} {scripts}>
<main>
<h1>c15t + Svelte</h1>
<p>Your app goes here.</p>
</main>
<footer>
<ConsentDialogLink>Privacy settings</ConsentDialogLink>
</footer>
<ConsentBanner />
<ConsentDialog />
</ConsentProvider>ConsentDialog loads in its own chunk after the page. ConsentDialogLink lets visitors open their preferences again, so put it in the footer. There is no stylesheet to import.
Hold scripts until consent
With loadMode: 'after-consent', PostHog waits for measurement permission. Remove any <script> tags or SDK imports that already load PostHog, so c15t is the only thing that loads it.
Svelte scripts and embeds covers more vendors, iframes and the network blocker. The c15t integrations list the helper for each vendor.
Google Consent Mode v2 in Svelte
The Google tag helper sends the Consent Mode v2 default before your config and an update after each choice. It ships in @c15t/integrations.
src/consent-scripts.ts
import { gtag } from '@c15t/integrations/google-tag';
export const scripts = [gtag({ id: 'G-XXXXXXXXXX', category: 'measurement' })];Add the helper to the scripts array in src/App.svelte, next to PostHog.
Measurement maps to analytics_storage. Marketing maps to ad_storage, ad_user_data and ad_personalization.
By default the helper loads gtag.js before a choice. Google then gets a default that matches the current permissions. Set loadMode: 'after-consent' to send nothing to Google until the visitor accepts.
Show the right banner in each region
Your Inth project sets a policy for each region, such as opt in for the EU and UK or opt out for California. With hosted(), the browser asks your backend for the policy, and the backend reads the visitor’s location from that request. Regional policies need no extra code.
Test each region before launch. See how regional policies work.
When the banner appears
The browser resolves consent after the page loads, so the banner appears after the first paint. consentManifest() adds a small script to index.html that sends /init while your JavaScript downloads.
For a banner in the server HTML, use SvelteKit. Measure your own pages before and after you add the banner.
Check that consent gates work
Build with vite build, run vite preview and open a private window with the Network tab open. Test under an opt in policy, such as the EU policy.
- Under an opt in policy, no PostHog request leaves the page before a choice.
- After Reject All and a reload, PostHog stays off.
- When you turn on measurement in Privacy settings, PostHog loads.
- When you turn measurement off, the page reloads without PostHog.
Choices post to your Inth project. Check that Inth has a record for each one. The c15t verify consent guide has the full release checklist.
Use Inth in production
c15t is open source. Point it at your Inth project, which hosts the backend and keeps the consent records. See Inth open source.
c15t also has a local mode with no backend. It keeps no consent records, so the docs advise against it in production.
Want to see what else your repo sends before consent? Run a free codebase scan.
Cookie consent banners for other frameworks
The same banner and the same Inth project work in other stacks. See the setup for Next.js, React, TanStack Start, Nuxt, Vue, Astro, SvelteKit, HTML and JavaScript.
Questions about Svelte consent
Install c15t and @c15t/integrations. The Svelte components come from c15t/svelte.
Svelte 5.20 and later, in a Vite app without SvelteKit.
Yes, with server helpers from the same package. See the SvelteKit cookie consent banner.
Import c15t/svelte/styles.css yourself and pass styles={false} to the provider.
Use the c15t Google tag helper. It sends the default before your config and an update after each choice.
Yes. Set a policy for each region in your Inth project. c15t applies the policy that matches the visitor’s location. See regional policies.
c15t has a local mode with no backend, but it keeps no consent records. Use your Inth project in production.