---
url: /saas-starter/v0.10.0/guide/analytics.md
---
# Analytics and cookie consent

::: tip What this page covers
Every choice of `--Persistence`, `--Auth`, `--IncludeTests` and `--Aspire`: a label says
which options a part is for. The copy in your app's `docs/` folder has only the parts for yours.
`ApiHost` stands for your app's name.
:::

The app has no analytics code. It has one switch: a Google Tag Manager container. Once that's set,
the app asks each visitor which cookies they accept, and loads Tag Manager only after they accept
some. Google Analytics, Datadog, Rollbar and other tools are then added as tags in Tag Manager,
without changing the app.

Without a container, the app shows no banner, because it sets no cookies that need consent
([what it does set](./security.md#cookies-and-consent)).

## Turning it on

1. Create a container at [tagmanager.google.com](https://tagmanager.google.com) (type: Web), and
   note its ID, such as `GTM-ABC1234`.
2. Set `Analytics__GoogleTagManagerId` to that ID.
3. For each tool you add in Tag Manager, allow its addresses in the
   [Content-Security-Policy](#the-content-security-policy).

| Setting | What it does |
|---|---|
| `Analytics__GoogleTagManagerId` | The container's ID. Empty (the default): no Tag Manager and no banner |
| `Analytics__ConsentVersion` | The cookie policy's version (default `1`). Raise it when the cookies you use change, and everyone is asked again |
| `Analytics__ConnectOrigins__0`, `__1`, ... | Origins your tags send data to or load tracking pixels from (`connect-src` and `img-src`) |
| `Analytics__ScriptOrigins__0`, `__1`, ... | Origins your tags load scripts from (`script-src`), besides Tag Manager's own |

The app checks these at startup and refuses to start if one is malformed, such as an ID that
isn't `GTM-...` or an origin that isn't a plain `https://` address.

## What visitors see

When there's a container, a banner shows at the bottom of each page until the visitor chooses. It
has **Accept all**, **Reject all** and **Choose**, which lets them pick:

* **Necessary:** always on. These are the cookies the app needs to work, so they need no consent.
* **Analytics:** for counting visits and seeing which pages are used.
* **Marketing:** for measuring and personalising advertising.

Refusing takes one click, the same as accepting, as the GDPR expects. **Cookie settings** in the
footer brings the choices back, so people can change their mind. Change the banner's wording in
`src/Client/src/features/consent/CookieConsent.tsx` so it describes your own tags.

The choice is kept in a `cookie_consent` cookie for six months, after which people are asked
again. Its value is `<version>.<analytics>.<marketing>`, such as `1.1.0`. A choice made under an
older `ConsentVersion` doesn't count, so the banner shows again.

::: info With accounts
**With an account, the choice is saved in the person's
[preferences](./accounts.md#preferences) too** (`PUT /api/account/consent`), with the policy version
and the time they chose. That records what each person agreed to and when, and their other
devices don't ask again:

* **When someone signs in,** a choice saved in their preferences is applied to that browser. If
  there's none yet, the browser's choice is saved to their preferences.
* **A choice made while signed in** is saved straight away. A new choice replaces the last one.
* **The saved choice** is in `GET /api/account/preferences` (`consent`) and in the person's data
  export. It's deleted with the account.
  :::

## How it reaches Tag Manager

Tag Manager isn't loaded until the visitor accepts analytics or marketing cookies, so nothing
reaches Google before they choose. Nothing is loaded at all if they reject both.

The app passes each choice on with Google's
[Consent Mode](https://developers.google.com/tag-platform/security/guides/consent):

| Choice | Consent Mode |
|---|---|
| Analytics | `analytics_storage` |
| Marketing | `ad_storage`, `ad_user_data`, `ad_personalization` |
| Necessary | `functionality_storage` and `security_storage`, always granted |

Google's own tags, such as Google Analytics and Google Ads, follow Consent Mode by themselves.
Other tags need a **consent check** in Tag Manager: in the tag's *Advanced settings → Consent
settings*, require `analytics_storage` (or `ad_storage` for marketing tags).

If someone withdraws consent later, the app tells Tag Manager straight away. Tags stop using the
refused cookies, but Tag Manager stays loaded until the next page load. Cookies the tags set
earlier stay until they expire, and Google's tags don't read them while consent is refused.

## The Content-Security-Policy

In production, the app's Content-Security-Policy blocks scripts, connections and images from
other sites ([security](./security.md)). With a container set, Tag Manager's own origin
(`https://www.googletagmanager.com`) is allowed. Add the origins your tags use:

* **Data and pixels (`ConnectOrigins`):** these can be broad.
* **Scripts (`ScriptOrigins`):** keep these as narrow as you can. Allowing scripts from a broad
  origin such as `https://*.google.com` would let an injected script load through it.

The app writes no inline scripts for Tag Manager, so the policy still doesn't allow
`'unsafe-inline'` or `'unsafe-eval'`. That means:

* **Custom HTML tags** with inline `<script>` code are blocked. Use tag templates from Tag
  Manager's Community Template Gallery instead, or add the tool to the app's code (below).
* **Custom JavaScript variables** evaluate to `undefined`, because they need `'unsafe-eval'`.
  Google recommends custom templates instead.
* **Preview mode (Tag Assistant)** needs more origins than the policy allows. Preview against the
  development server (`npm run dev`), which sends no Content-Security-Policy.

Check a published page in your browser's developer tools: blocked requests are reported in the
console.

## Adding tools

### Google Analytics 4

1. In Tag Manager, add a **Google tag** with your measurement ID (`G-...`) and trigger it on
   **Initialization – All Pages**. Google's guide:
   [set up Google Analytics in Tag Manager](https://support.google.com/tagmanager/answer/9442095).
2. Allow its origins, as listed in Google's
   [Content Security Policy guide](https://developers.google.com/tag-platform/security/guides/csp):

   ```
   Analytics__ConnectOrigins__0=https://*.google-analytics.com
   Analytics__ConnectOrigins__1=https://*.google.com
   ```

   With Google Ads features on, that guide lists more origins (`https://*.g.doubleclick.net`,
   and each Google country domain you serve).

It respects Consent Mode by itself, so it needs no consent check.

### Datadog Real User Monitoring

Datadog's browser SDK sets a session cookie (`_dd_s`), so it goes under **Analytics**.

* [Datadog's browser RUM setup](https://docs.datadoghq.com/real_user_monitoring/browser/setup/client/):
  pick the CDN option to load it from a tag. Datadog's docs list the origins for your site
  (region), which go in `ScriptOrigins` (where the SDK loads from) and `ConnectOrigins` (where it
  sends data).
* [User tracking consent](https://docs.datadoghq.com/real_user_monitoring/browser/data_collected/#user-tracking-consent)
  explains how to hold back data until consent.

The CDN snippet Datadog gives is inline code, which the policy blocks in a Custom HTML tag. Use a
Datadog tag template from the Community Template Gallery if one suits you, or add the npm package
to the client (below).

### Rollbar and other error tracking

* [Rollbar.js](https://docs.rollbar.com/docs/browser-js) sends error reports from the page.
  Whether it needs consent depends on what you send with them, such as the user's ID or email.
  If unsure, treat it as **Analytics**.
* Rollbar's setup snippet is inline code too, so the same applies as for Datadog: use a template,
  or add it to the client's code.

### Adding a tool to the client's code instead

When a tool can't run from Tag Manager, install its npm package in `src/Client`, start it from
the app, and check consent first:

```ts
import { readConsent } from './features/consent/consent'

// version: from useConsentOptions() (the API's Analytics:ConsentVersion)
if (readConsent(version)?.analytics) startTheTool()
```

Add its origins to `ScriptOrigins` (if it loads more scripts) and `ConnectOrigins`.

## What doesn't need consent

Server-side telemetry sets nothing in the browser: traces, metrics and logs sent with
OpenTelemetry (`OTEL_EXPORTER_OTLP_ENDPOINT`, see [observability](./observability.md)), or an APM
agent such as Datadog's on the server. Consent rules cover storing and reading data on the
visitor's device, so these need no banner. They're still personal data if they hold IP addresses
or user IDs, so list them in your privacy policy.

The banner covers cookies. You still need a privacy policy that names the tools you use and what
they collect. This page isn't legal advice: check what your own tags do.
