---
name: tawa-analytics
description: "Matomo analytics on Tawa (on by default for nextjs/static): MATOMO_* runtime env, @insureco/analytics MatomoAnalytics component, cookie consent, opting out. Use when adding or debugging page analytics."
---

# Analytics — Matomo, On by Default

The platform runs Matomo at `engage.insureco.io`. **Every site is tracked by default**: services
that serve pages and every HypeSite. Nobody has to turn it on. You opt out, not in.

| What | Tracked by default? | What you do |
|------|---------------------|-------------|
| `static` service | yes | nothing. nginx inserts the tracker into every HTML page |
| `nextjs` service | yes, Matomo site provisioned | render `<MatomoAnalytics />` once (the scaffold already does) |
| `express` / `hono` / `fastify` | no (mostly APIs) | `spec.analytics.enabled: true`, then render the snippet |
| `worker`, `insureco.io/internal: "true"` | never | — |
| HypeSite | yes | nothing. Switch it off per site in the HypeSites tile |

Opt a service out:

```yaml
spec:
  analytics:
    enabled: false
```

## What the builder does

On every deploy of a tracked service (iec-builder#343):

1. Takes every **verified custom domain** for the environment, or the platform hostname
   (`{service}.tawa.pro`, `.sandbox.`, `.uat.`) when there is none.
2. Finds or creates **one Matomo site per host** (idempotent: a stored record, else a lookup
   by URL, else create). Each environment gets its own sites; traffic never mixes.
3. Injects **runtime** env into the pod:

| Var | Example | Notes |
|-----|---------|-------|
| `MATOMO_URL` | `https://engage.insureco.io` | Matomo base URL |
| `MATOMO_SITE_ID` | `218` | The first host's site; the fallback for an unknown host |
| `MATOMO_SITE_IDS` | `raterspot.com=218,www.raterspot.com=219` | Only when there are several hosts; picked by request host |

4. Writes one build-log line saying whether page views will actually be recorded:

```
Matomo analytics: site 218 (raterspot.com), site 219 (www.raterspot.com) → https://engage.insureco.io
Matomo tracker: present (<MatomoAnalytics /> found)
```

or, for a Next.js app that never renders the tracker:

```
[warn] Matomo site provisioned but this app does not use @insureco/analytics, so no page views are recorded. …
```

Fail-open: Matomo down or not configured is a `[warn]` line, never a failed deploy.

## Next.js — `@insureco/analytics`

```bash
npm install @insureco/analytics@^0.2.0
```

```
# .npmrc
@insureco:registry=https://git.insureco.io/api/packages/insureco/npm/
```

```tsx
// app/layout.tsx — a Server Component
import { MatomoAnalytics } from '@insureco/analytics'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html>
      <body>
        {children}
        <MatomoAnalytics />
      </body>
    </html>
  )
}
```

New apps from `insureco/tawa-scaffold-eco-app` already have this.

### Cookies and consent

The default is privacy-safe: Matomo's `requireCookieConsent`, which tracks **without cookies**
until the visitor consents. Wire your consent banner to the helpers:

```tsx
'use client'
import { giveCookieConsent, revokeCookieConsent } from '@insureco/analytics'

onAccept={() => giveCookieConsent()}
onDecline={() => revokeCookieConsent()}
```

| `<MatomoAnalytics cookies=…>` | Behaviour |
|-------------------------------|-----------|
| `"consent"` (default) | cookieless until `giveCookieConsent()` |
| `"off"` | never sets cookies (`disableCookies`) |
| `"on"` | cookies from the first page view; only with your own lawful basis |

### Custom events, goals, search

```tsx
'use client'
import { trackEvent, trackGoal, trackSiteSearch, setCustomDimension } from '@insureco/analytics'

trackEvent('Quote', 'Started', 'auto')
trackGoal(3, 49.99)
trackSiteSearch('flood coverage', 'policies', 12)
```

## Static sites — no code

The generated static image carries a start-up script (iec-builder#344). When the pod starts with
`MATOMO_URL` and `MATOMO_SITE_ID`, nginx inserts the tracker before `</head>` on every HTML
response, picking the site by host from `MATOMO_SITE_IDS`. It is cookieless (`disableCookies`).
Nothing is baked into the image, because the site id only exists after the build. A static site
with **its own Dockerfile** gets none of this: render the snippet yourself with
`buildMatomoSnippet` from `@insureco/analytics`.

## HypeSites

Every published HypeSite is tracked, one Matomo site per host (the `hypesites.tawa.pro/<slug>/`
URL and each custom domain), provisioned by the portal at publish through the builder, so the
Matomo admin token stays on the builder. The hypesites service inserts the tracker into every
published page: cookieless on platform hosts (sandboxed, no storage), cookieless-until-consent on
custom domains. Switch it off per site in the HypeSites tile. See [HypeSites](hypesites.md).

## Why runtime injection (not `NEXT_PUBLIC_*`)

`NEXT_PUBLIC_*` values are inlined into the client bundle at **Docker build time**, but the site
id is only known at deploy. The component reads `MATOMO_*` in a **Server Component at request
time**. Same rule as [nextjs.md](nextjs.md).

## Security

- `MATOMO_ADMIN_TOKEN` is a superuser credential. It lives **only** on the builder and is never
  injected into a pod or a page. The portal provisions HypeSite sites through the builder's
  `POST /analytics/sites` with its own Bio-ID service login.
- Site ids are public by design.

## Common mistakes

- Expecting data from a Next.js app that never renders `<MatomoAnalytics />`. The deploy log
  warns; add the component.
- `<MatomoAnalytics />` in a `'use client'` file. It must be a Server Component.
- Adding `NEXT_PUBLIC_MATOMO_SITE_ID` by hand. Unnecessary and wrong.
- Expecting data locally. The env vars are only injected on deploy; local dev renders nothing.
- Writing `spec.analytics.enabled: true` on a Next.js or static service. Harmless, and no
  longer needed.
