# Guide: cookie banner

> From one script tag to a fully custom integration — three paths, pick the one that fits.

Source: https://croombit.com/en/docs/guides-banner

## Option 1 — One script tag (30 seconds)

Add one tag before `</body>`. The banner appears automatically, records every choice, and populates the cookie declaration table from your last scan.

```html
<script src="https://croombit.com/cmp.js"
        data-tc-api-key="pk_live_…"></script>
```

- `data-tc-locale="es"` — force a language instead of detecting the browser’s
- `data-tc-auto-show="false"` — load silently and call `window.croombit.show()` yourself
- Let visitors review or change their choice later: add `data-tc-open-preferences` to any link or button (see below)

### Let visitors change their choice

Once a visitor has decided, the banner no longer shows. Put a link in your footer or account page and the same panel reopens with **their current choices already set** — they toggle what they want and save. Each save is a new, timestamped record in the vault.

```html
<a href="#" data-tc-open-preferences>Cookie settings</a>
<!-- or, from your own code: -->
<button onclick="window.croombit.showPreferences()">Cookie settings</button>
```

Closing the panel without saving leaves the existing choice untouched. Scripts that are already running keep running until the next page load; Google tags react immediately through Consent Mode.

> **TIP:** Don't want to add a link yourself? Turn on **Floating button** in the banner designer's Behaviour tab and the script paints its own icon (bottom-left or bottom-right) once a decision exists — no page changes needed.

> **TIP:** You need at least one published purpose and a `pk_` key with your domain on its allowlist before the banner shows — see the [Quickstart](https://croombit.com/en/docs/quickstart).

## Option 2 — Google Tag Manager (no code)

1. Download the [Croombit CMP tag template](https://croombit.com/gtm/croombit-cmp.tpl). In GTM: **Templates → Tag Templates → New**, then **⋮ → Import**, pick the file and **Save**.
2. In **Tags → New**, choose **Croombit CMP**.
3. Enter your publishable key and choose a layout (bar, box, modal, or wall).
4. Set the trigger to **Consent Initialization — All Pages**.
5. Publish the container.

> **NOTE:** GTM injects the script programmatically via `window.__tcConfig` instead of `data-*` attributes, and only once the container has loaded. The banner looks and records the same as option 1, but tags that fire before the script finishes loading don't yet see the Consent Mode defaults — when holding back tags must be guaranteed, use option 1 first in `<head>`.

## Option 3 — Custom integration (full control)

Use this when you need to match an existing design system or drive the banner from your own UI framework. Uses the SDK — about 30 lines.

- Publish the purposes you’ll ask about — e.g. `analytics`, `marketing` (see [Consent statements](https://croombit.com/en/docs/statements)).
- Create a **publishable** key (`pk_…`) and add your domain to its allowlist.

## Step 1 — Create the client

```ts
import { CroombitClient } from '@croombit/sdk';

const tc = new CroombitClient({ baseUrl: 'https://api.croombit.com', apiKey: 'pk_live_…' });

// Identify the anonymous visitor by a first-party cookie you set yourself.
const visitor = 'cookie_id:' + getOrCreateVisitorCookie();
```

## Step 2 — Render the banner from your purposes

Don’t hard-code the copy — pull the exact published wording so the record matches what was shown.

```ts
const { purposes } = await tc.getPurposes({ locale: navigator.language });

for (const p of purposes) {
  //  p.key            -> "analytics"
  //  p.text.title     -> "Usage analytics"
  //  p.text.body      -> "Helps us see which features get used…"
  //  p.defaultState   -> "denied"  (regional rules — always respect this)
  renderToggle(p);
}
```

## Step 3 — Save the choice

```ts
async function onSave(choices /* { analytics: true, marketing: false, … } */) {
  await tc.recordConsentBatch(
    Object.entries(choices).map(([purpose, ok]) => ({
      subject: visitor,
      purpose,
      state: ok ? 'GRANTED' : 'DENIED',
      locale: navigator.language,
      channel: 'cookie_banner',
    })),
    { idempotencyKey: crypto.randomUUID() },
  );
  tc.notifyConsentChanged();
}
```

## Step 4 — Gate your tags

```ts
tc.gate(visitor, ['analytics'], () => {
  loadGoogleAnalytics();
});
```

> **TIP:** Using Google tags? `tc.applyConsentMode(visitor, gtag)` translates the state into a Consent Mode v2 `gtag("consent","update",…)` call for you.

> **NOTE:** When the visitor signs in, carry their choices over with a merge — see [anonymous → identified](https://croombit.com/en/docs/guides-merge).