# Cookies, Analytics & Custom Scripts

## Cookies, Analytics & Custom Scripts

**Files:** `src/config/config.toml`, `src/content/sections/<language>/cookie-consent.md` · **Skill level:** Beginner (you paste your analytics snippet)

Covena itself adds no analytics or tracking. The only thing it keeps in the visitor's browser is their cookie choice, under `cookie-consent` in local storage. The banner matters once **you** add analytics or ad tags, and it holds those tags back until the visitor agrees.

### The Cookie Banner
The banner is a card at the bottom of the page with **Accept all**, **Reject all**, and **Customise**. Customise opens a list of categories with a **Save choices** button.

```toml
# src/config/config.toml
[settings.cookieConsent]
  enable   = true   # false turns the banner off
  revision = 1      # raise it when your cookie use changes, to ask every visitor again
```

The text and categories are in `src/content/sections/<language>/cookie-consent.md`, one file per language:

```yaml
title: "We use cookies"
description: "Necessary cookies keep this site working. If you agree, we also use analytics cookies ..."
link:
  enable: true
  label: "Privacy policy"
  url: "/privacy-policy/"
acceptLabel: "Accept all"
rejectLabel: "Reject all"
customizeLabel: "Customise"
saveLabel: "Save choices"
categories:
  - id: "necessary"
    title: "Necessary"
    description: "Security, form protection, and your language choice."
  - id: "analytics"
    title: "Analytics"
    description: "Aggregated visit counts and page performance, used to improve the site."
  - id: "marketing"
    title: "Marketing"
    description: "Measure enquiries that come from ads and campaigns."
```

A category `id` is one of `necessary`, `analytics`, or `marketing`. The necessary category is always on and shows `requiredLabel` ("Always on"). Rewrite the description so it names the tools you really use.

<Aside type="tip" title="Do you need the banner?">
  If you add no analytics or marketing tags, you may not need a banner at all.
  Then set `enable = false`, and also set `enable: false` on the "Cookie
  settings" item in `footerLegal` in each `src/config/menu.<lang>.json`, so the
  footer doesn't show a link to preferences that no longer exist. Check the
  rules where your firm practises.
</Aside>

### Add Analytics Or Any Script
Paste tags into `[head]` in `config.toml`. They go into the `<head>` of every page:

```toml
[head]
  content = """
  <script defer src="https://analytics.example.com/script.js" data-site="yourfirm.com"></script>
  """
```

Use this for analytics, a verification meta tag, or a chat widget.

### Make Scripts Wait For Consent
With the banner on, mark each tracking tag with the category it belongs to:

<Steps>

1. Change `<script>` to `<script type="text/plain" data-consent="analytics">` (or `"marketing"`).
2. Keep `src`, `async`, `defer`, and the other attributes as they are. Inline code works the same way.
3. To need two categories, list both: `data-consent="analytics marketing"`.
4. For a module script, add `data-type="module"`.

</Steps>

```toml
[head]
  content = """
  <script type="text/plain" data-consent="analytics" defer src="https://analytics.example.com/script.js" data-site="yourfirm.com"></script>
  """
```

The browser ignores a `text/plain` script, so it doesn't run until the visitor accepts its category. Then the banner turns it into a real script.

<Aside type="caution">
  A gated script never runs while the banner is off. If you turn the banner
  off, use plain `<script>` tags.
</Aside>

### Reopen The Preferences
Any link with `data-cookie-settings` or `href="#cookie-settings"` reopens the preferences. The footer's "Cookie settings" link (in `footerLegal`) does this already. Mention it in your privacy policy so visitors know how to change their mind.

### For Developers
- A `consentchange` event fires on `document` on every page load and after each choice. `event.detail` has `necessary`, `analytics`, and `marketing`:

  ```js
  document.addEventListener("consentchange", (e) => {
    if (e.detail.analytics) {
      // load analytics
    }
  });
  ```

- `window.cookieConsent.get()` returns the saved choice, or `null` before the visitor chooses.
- `window.cookieConsent.open()` opens the preferences.
- The banner is `src/layouts/components/widgets/CookieConsent.astro`.

### Do It With AI
```text
Add Google Analytics 4 to this Covena site with the ID [G-XXXXXXX]. Put the tags in
[head] content in src/config/config.toml, and gate both script tags on the analytics
category: type="text/plain" data-consent="analytics". Then update the analytics
description in every src/content/sections/<language>/cookie-consent.md to say we use
Google Analytics. Don't change anything else.
```