# SvelteKit

URL: https://privacyboard.io/docs/cookie-banner/sveltekit

Last updated 2026-11-22.

Add the Privacyboard cookie banner, privacy policy and cookie policy to a SvelteKit app, by hand or with a coding agent, and start analytics after consent.

In a SvelteKit app, the banner is one `<script>` tag, first in the `<head>` of `src/app.html`, the page template every route uses. The privacy policy and cookie policy are pages of your privacy center, linked from the footer. You can let a coding agent do it with one command, or add it by hand.

## The fastest way: one command

From the project's folder:

```bash
npx privacyboard init --site acme.com
```

It reads the package names in `package.json` (never their values), recognises the tools that handle personal data and prints the script, where it goes in a SvelteKit project, the footer links and a link to claim the setup. Coding agents can run it for you: see [Set up with an AI coding agent](/docs/getting-started/set-up-with-an-ai-agent). Nothing goes live until you claim the setup, check the tools found, validate your documents and publish.

If you already have a workspace, take the script from **Cookie banner → Install** instead, and follow the steps below.

## Put the script first in src/app.html

Add the tag before `%sveltekit.head%`, so it runs before anything SvelteKit or your pages add to the head:

```html
<!doctype html>
<html lang="en">
  <head>
    <script src="https://privacyboard.io/cmp/your-banner-id.js"></script>
    <meta charset="utf-8" />
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>
```

Not in `<svelte:head>` inside a page or layout: that content is added later, so the banner would run after other scripts.

## Footer links

Link your privacy policy and cookie policy from the footer of every page, and add a link that reopens the banner:

```html
<footer>
  <a href="https://acme.privacyboard.io/privacy-policy">Privacy policy</a>
  <a href="https://acme.privacyboard.io/cookie-policy">Cookie policy</a>
  <a href="#" data-pb-open>Cookie settings</a>
</footer>
```

Use the addresses of your own privacy center, shown in **Privacy center**, or your own domain if you set one up.

## Analytics installed from npm

Tools bundled into your app, such as `posthog-js`, do not load a script tag the banner could hold. Start them once their category is allowed, with the `privacyboard` package (`npm i privacyboard`), in your root layout:

```svelte
<script>
  import { onMount } from 'svelte';
  import posthog from 'posthog-js';
  import { whenAllowed } from 'privacyboard';

  onMount(() => whenAllowed('analytics', () => posthog.init('your-project-key', { api_host: 'https://eu.i.posthog.com' })));
</script>

<slot />
```

`whenAllowed` runs your code right away if analytics is already allowed, otherwise when the visitor accepts it. It returns a function that cancels the wait, which `onMount` calls when the layout goes away.

## Check it

Run the site, open it in a private window and check that the banner appears. Once it is online, run the [site scan](/docs/cookie-banner/site-scan): it shows what your pages contact before a visitor chooses. If your site sends a Content Security Policy, add `https://privacyboard.io` to `script-src` and `connect-src`.

## FAQ

### Does it work with prerendered or static SvelteKit sites?
Yes. `src/app.html` is used for every page, prerendered or not, and the banner runs in the browser.

### Why not svelte:head?
Content in `<svelte:head>` is added after the template head, so a tracker placed earlier could run first. `src/app.html` puts the banner before everything.

### Does it run again on client-side navigation?
No. The banner runs once per page load; navigating between routes keeps it.
