# Cookies, Analytics & Custom Scripts

## Cookies, Analytics & Custom Scripts

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

Lotline itself sets no tracking cookies. It keeps only two small things in the visitor's browser: their cookie choice, and whether they closed the announcement bar. A consent banner matters once **you** add analytics or ad tags, and Lotline's banner holds those tags back until the visitor agrees.

### The Cookie Banner
The banner is a card in the bottom-right corner with **Accept all**, **Reject all** and **Choose**. Accept and Reject have the same size and style, so saying no is as easy as saying yes. **Choose** lists three categories: **Necessary** (always on), **Analytics** and **Marketing**. The choice is saved in the visitor's browser (`localStorage`, key `cookie-consent`).

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

The banner's text, button labels, categories and link are in `src/content/sections/english/cookie-consent.md` (and the same file in each other language folder):

```yaml
title: "Cookies on this site"
description: "We store only what the site needs to work. With your OK, we also count visits and check which ads bring in inquiries."
link:
  enable: true
  label: "Cookie policy"
  url: "/cookie-policy/"
acceptLabel: "Accept all"
rejectLabel: "Reject all"
customizeLabel: "Choose"
saveLabel: "Save choices"
categories:
  - id: "necessary"   # necessary | analytics | marketing
    title: "Necessary"
    description: "Your cookie choice, and whether you closed the announcement bar. Nothing that identifies you."
```

Each category's `id` must stay `necessary`, `analytics` or `marketing`. Change the titles and descriptions as you like, and describe what your own tools store. The link goes to `/cookie-policy/` (`src/content/pages/english/cookie-policy.md`), a placeholder text you have to adapt to the tools you use.

<Aside type="tip" title="Do you need the banner?">
  Without analytics or ad tags, Lotline stores only necessary data, and you
  can set `enable = false`. Some privacy-friendly analytics tools don't use
  cookies either. Check the rules for your region.
</Aside>

### Add Analytics Or Any Script
Everything in `[head] content` goes into the `<head>` of every page. Paste the snippet your tool gives you (Google Analytics, Plausible, Fathom, a Meta pixel, a site verification tag and so on):

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

### Make Scripts Wait For Consent
With the banner on, mark each tracking script with its category. It then runs only after the visitor accepts that category, on that page and every page after:

<Steps>

1. Change `<script` to `<script type="text/plain"`.
2. Add `data-consent="analytics"` (or `"marketing"` for ad and conversion tags).
3. Keep every other attribute (`src`, `async`, `defer`, `data-...`) as it is.

</Steps>

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

- Inline scripts work the same way: add the two attributes to the `<script>` tag.
- To require two categories, list both: `data-consent="analytics marketing"`.
- For a module script, also add `data-type="module"`.

<Aside type="caution">
  Scripts marked `type="text/plain"` **never run** while the banner is off. If
  you turn the banner off, change them back to plain `<script>` tags.
</Aside>

### Reopen The Preferences
Any link with `href="#cookie-settings"` (or a `data-cookie-settings` attribute) reopens the preferences. The **Cookie settings** link in the footer's legal row does this. If you turn the banner off, set that link's `"enable"` to `false` in each `src/config/menu.<language>.json`.

### For Developers
- `window.cookieConsent.get()` returns the saved choice (or `null`), and `window.cookieConsent.open()` reopens the preferences.
- A `consentchange` event fires on `document` on every page load and after each choice, with `{ necessary, analytics, marketing }` in `detail`:

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

The banner's code is `src/layouts/components/widgets/CookieConsent.astro`.

### Do It With AI
```text
Add my analytics to the site: [PASTE SNIPPET]. Put it in [head] content in src/config/config.toml.
The cookie banner is on, so gate it on the "analytics" category: type="text/plain" and
data-consent="analytics", keeping all other attributes. Then update cookie-policy.md, in every
language folder of src/content/pages/, to name [TOOL NAME] and what it stores.
```