# Analytics and cookie consent

> Load the marketplace's Google Tag Manager and Plausible, ask visitors' consent with your own cookie banner, and keep a record of every answer.

The marketplace's team connects Google Tag Manager and Plausible in the console (**Settings → Integrations**) and lists the shop's cookies under **Settings → Cookies**. Your storefront loads what's connected, shows the cookie banner in its own design, and sends each answer to mongrl, which keeps them as proof of consent.

## What to load

`GET /v1/marketplace` has a `tracking` section:

```json
{
  "gtm_container_id": "GTM-5FBXPDFN",
  "gtm_script_host": "",
  "plausible_domain": "usedicons.com",
  "consent": {
    "required": true,
    "version": 2,
    "policy_url": "/privacy",
    "cookies": [
      { "name": "_ga, _ga_*", "provider": "Google Analytics", "category": "statistics",
        "purpose": "Tells visits apart", "duration": "2 years" }
    ]
  }
}
```

- **Plausible** sets no cookies, so load it on every page without asking: `<script defer data-domain="usedicons.com" src="https://plausible.io/js/script.revenue.js">`.
- **Google Tag Manager** loads on every page too, but only after Google's consent defaults (below), so its tags get nothing the visitor hasn't allowed. With `gtm_script_host`, load `https://<host>/gtm.js` instead of Google's (a server-side container on the shop's domain).
- **The banner** shows when `consent.required` is true and the visitor hasn't answered `consent.version` (or a later one).

## The consent categories

Visitors choose two things; necessary cookies need no consent.

| Category | Google consent mode |
|---|---|
| `necessary` | `functionality_storage`, `security_storage`: always granted |
| `statistics` | `analytics_storage` |
| `marketing` | `ad_storage`, `ad_user_data`, `ad_personalization`, `personalization_storage` |

The banner offers **Accept all**, **Reject all** (as prominent as accepting) and a way to choose, with nothing ticked in advance. Link the privacy policy, and list `consent.cookies` by category in the choices. Put a **Cookie settings** link in the footer, so visitors can change their mind as easily as they answered.

## The order on every page

In `<head>`, before anything else that tracks:

```html
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('consent', 'default', {
    ad_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied',
    analytics_storage: 'denied', personalization_storage: 'denied',
    functionality_storage: 'granted', security_storage: 'granted',
    wait_for_update: 500
  });
  gtag('set', 'ads_data_redaction', true);
  // The visitor answered earlier: their choice, before GTM starts.
  gtag('consent', 'update', { analytics_storage: 'granted' /* … from the cookie */ });
</script>
<!-- then the GTM snippet -->
```

When the visitor answers, call `gtag('consent', 'update', …)` with their choice and push `{ event: 'consent_update' }` so tags waiting on consent fire straight away.

## Keep the answer

Remember the answer in a first-party cookie for a year at most: a random visitor ID (a UUID), the version, and the two choices. Then send it from your server with the shop's secret key, and the visitor's session when they're signed in:

```http
POST /v1/consents
{ "visitor_id": "0b7e6a52-55c1-4f0e-9d0e-6f7a2c1d4e5f", "version": 2,
  "statistics": true, "marketing": false, "source": "banner", "locale": "en" }
```

It answers `204`. `source` is `banner` for the first answer and `settings` when they change it. Show the visitor ID in the cookie settings: the team looks it up in the console to show what that visitor agreed to, and when.

When the team asks everyone again, `consent.version` goes up and the banner shows to everyone whose answer is older.

## Events

Push GA4's recommended ecommerce events to the `dataLayer` (`view_item_list`, `select_item`, `view_item`, `add_to_wishlist`, `begin_checkout`, `add_shipping_info`, `add_payment_info`, `purchase`, plus `sign_up`, `login`, `search` and `generate_lead`), with `ecommerce: null` pushed first to clear the previous one. The team sets up Google Analytics, Google Ads, Meta and other tags on them in the container.
