# Astro

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

Last updated 2026-11-22.

Add the Privacyboard cookie banner, privacy policy and cookie policy to an Astro site, and keep analytics and embeds waiting until visitors consent.

In an Astro site, the banner is one `<script is:inline>` tag, first in the `<head>` of your base layout. 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 Astro 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 your base layout

Add the tag first in the `<head>` of the layout every page uses, usually `src/layouts/Layout.astro` or `BaseLayout.astro`:

```astro
---
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <script is:inline src="https://privacyboard.io/cmp/your-banner-id.js"></script>
    <meta charset="utf-8" />
    <title>{title}</title>
  </head>
  <body>
    <slot />
  </body>
</html>
```

`is:inline` tells Astro to leave the tag as it is. Without it, Astro may bundle or move scripts, and the banner would no longer run first.

## 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 site, 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 a regular script in the layout:

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

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

This script is bundled by Astro, and `whenAllowed` runs your code right away if analytics is already allowed, otherwise when the visitor accepts it. Analytics added with a tag in the page, such as Google Analytics, is held back by the banner itself; see [Blocking before consent](/docs/cookie-banner/blocking-before-consent).

## 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 view transitions?
Yes. The banner loads once per visit: if Astro runs the script again when it swaps pages, the banner sees that it is already loaded and does nothing. The visitor’s choice is kept.

### Does it work on a fully static Astro site?
Yes. The banner runs in the visitor’s browser, so static, server-rendered and hybrid sites work the same.

### Can I use Partytown for the banner?
No. Partytown moves scripts to a web worker, where the banner could not hold back the page’s trackers. Keep it a plain inline script, first in the head.
