# stats Built-in statistics in the admin panel: **daily visits for the last 30 days**, most viewed pages, entry pages, traffic sources, devices — and **visitor history**, but only for visitors who consented to analytics cookies. The data stays in the project's database; nothing goes to Google or other companies. ## Two levels — and why the first needs no consent | | Without consent (every visitor) | With **analytics** consent (the same one as GA4) | |---|---|---| | What is in the browser | **nothing** — no cookie, localStorage, fingerprint | cookie `ipal_vid` (random id, 180 days) | | How page views are joined into a visit | hash of a random salt + IP + user agent; the salt lives only in server memory and is **rotated daily** — after midnight nobody (us included) can link a visit to an IP or to a visit from another day | as on the left + `visitorId` from the cookie | | What the panel shows | daily chart, pages, entries, sources, devices, path within one visit | additionally **this person's history across many days** | | Stored IP / user agent | **no** | **no** | - **Art. 399 of the Polish Electronic Communications Law (ePrivacy 5(3))** requires consent to store or read information on the user's device. Level 1 stores and reads nothing on the device, so the banner does not apply to it. - **GDPR still applies** — the IP is processed for a moment while computing the hash. Legal basis: legitimate interest (Art. 6(1)(f)). **Describe it in the privacy policy** (text below). - **Level 2 without consent is illegal** — `` creates `ipal_vid` only after analytics consent, and the server still checks the consent cookie (with the current *Policy version*) before it stores `visitorId`. Do not work around this. This is not legal advice — for an unusual project (sensitive data, children) consult the client's lawyer. ## Config ```ts ipalKit({ // … stats: true, // or: stats: { retentionDays: 90, // how many days visits are kept (default 90; the chart shows 30) timeZone: 'Europe/Warsaw', // where a "day" starts (default Europe/Warsaw) dashboard: true, // chart on the panel dashboard (default true) }, }) ``` | Option (`StatsOptions`) | Default | Description | |---|---|---| | `retentionDays` | `90` | older visits (by `lastAt`) are deleted | | `timeZone` | `'Europe/Warsaw'` | day boundary (`day` field, salt rotation, charts) | | `dashboard` | `true` | `false` removes only `StatsWidget` from the dashboard — the view and the nav link stay | | `collectionOverrides` | none | changes `stats-visits`: an object merges `access` / `admin` / `hooks` key by key (your keys win), a function `(collection) => collection` takes over (keep the slug). Its `access.read` also decides who sees the stats view and the dashboard chart | Adds: - the `stats-visits` collection (group *Statistics*, read and delete: admin; `create`/`update` via API blocked — written only by the endpoint with `overrideAccess`), - `POST /api/ipal/hit`, - the **`/stats`** view (default `/admin/stats`; it uses `ipalKit({ adminRoute })`, not `routes.admin` set by hand in the config) + a nav link *Statistics overview* (`StatsNavLink`, hidden for users who cannot read `stats-visits`), its browser tab reads *Statistics* / *Statystyki* in the admin language (set after load through `DocumentTitle`, because Payload's view `meta.title` is one fixed string — i18n.md), - a 30-day chart on the dashboard (`StatsWidget`), - `STATS_SLUG` (`'stats-visits'`) from `@intecion/ipal-kit` — for your own queries. After enabling: `pnpm payload generate:importmap` (components `@intecion/ipal-kit/admin#StatsView`, `#StatsWidget`, `#StatsNavLink`) and a database migration (Postgres: `pnpm payload migrate:create`). ## Front With `SiteProviders`: ```tsx ``` Or by hand — **inside `ConsentProvider`**: ```tsx import { PageStats } from '@intecion/ipal-kit/client' // endpoint defaults to '/api/ipal/hit' ``` `` sends one beacon per page view (also on Next client-side navigation; `sendBeacon`, fallback `fetch` with `keepalive`). It renders nothing. Props (`PageStatsProps`): `endpoint` (default `'/api/ipal/hit'`), `locale`. ### `POST /api/ipal/hit` Body: `{ p: path, r?: referrer, l?: locale, v?: visitorId }`. Always answers `204` (also on an error, a rejection and when the limit of 60 requests/min per IP is exceeded — scope `stats`). `v` is stored only when the request's consent cookie has `analytics: true` under the current *Policy version* (version read from Cookie banner, cached 1 min). Path: must start with `/`, max 300 characters; visit: max 200 page views. `locale` is stored when it matches `xx` / `xx-XX`. `VISITOR_COOKIE` (`'ipal_vid'`) from `/client` — the cookie name, e.g. for your own cookie table. CSP: the endpoint is on the same domain — `connect-src 'self'` is enough. ## What we do not count - logged-in panel users (an editor viewing the site does not inflate the stats), - bots (Googlebot, link previews, Lighthouse, curl, headless), - Next prefetch (`sec-purpose: prefetch`), - a reload of the same page within 10 s. We store only the path: the query string and `#hash` are cut off (they can contain an e-mail, a token, a search phrase). Referrer — only the domain name. Country — only from the Cloudflare `cf-ipcountry` header, if present. ## Visit A sequence of page views with gaps under 30 minutes, within one day. A server restart or several instances (each has its own salt) can split an ongoing visit in two — irrelevant for statistics. Do **not** store the salt in the database to "fix" this: then yesterday's hashes could be reversed to IPs. Visits older than `retentionDays` are deleted automatically — at most once a day per server process, on a page view, with one `db.deleteMany` query **in the background** (the page view does not wait for the cleanup; an error goes to the log and the cleanup is retried on the next page view). The salt changes when `day` changes in the `timeZone` zone. ## Visitor rights (GDPR) - **Withdrawing consent** — `useConsent` deletes `ipal_vid` (it is in `DEFAULT_COOKIE_MAP.analytics`); further page views are anonymous. - **Deleting data** — the visitor gives the id from the `ipal_vid` cookie (DevTools → Application → Cookies). In the panel: *Statistics → Visits*, filter `visitorId` = id → select all → Delete. The `/admin/stats?visitor=` view shows the same as a history. - Anonymous visits cannot be linked to a person — there is nothing to hand over or delete on request. ## For the privacy policy and the cookie table Add a row in **Cookies → Cookie list** (panel): | Name | Provider | Category | Purpose | Duration | |---|---|---|---|---| | `ipal_vid` | this site | analytics | Recognising a returning visitor in the site statistics | 180 days | Privacy policy text — adjust the controller name and the retention period, paste the version for each language of the site: ```text Visit statistics. We count visits to this site with our own tool, without third parties. Without your consent we store nothing on your device: page views are joined into a visit with a one-day, irreversible hash of the IP address and the browser type; the IP address itself is not stored, and the hash becomes meaningless after a day. We record the pages visited, the time, the referring domain, the device type (desktop / mobile / tablet) and the language. The legal basis is our legitimate interest (Art. 6(1)(f) GDPR) — knowing how the site is used. If you accept analytics cookies, we set the `ipal_vid` cookie with a random identifier so that your later visits appear as one history (legal basis: consent, Art. 6(1)(a) GDPR; you can withdraw it in the cookie settings). We keep the data for 90 days. ``` Polish version (for a `pl` site): ```text Statystyki odwiedzin. Liczymy odwiedziny strony własnym narzędziem, bez udziału podmiotów trzecich. Bez Twojej zgody nie zapisujemy niczego na Twoim urządzeniu: kolejne odsłony łączymy w wizytę za pomocą jednodniowego, nieodwracalnego skrótu adresu IP i typu przeglądarki; samego adresu IP nie przechowujemy, a skrót traci znaczenie po upływie doby. Zapisujemy odwiedzone podstrony, czas, domenę strony odsyłającej, rodzaj urządzenia (komputer / telefon / tablet) i język. Podstawą jest nasz prawnie uzasadniony interes (art. 6 ust. 1 lit. f RODO) — wiedza, jak korzystacie ze strony. Jeśli zgodzisz się na cookies analityczne, zapiszemy cookie `ipal_vid` z losowym identyfikatorem, dzięki któremu zobaczymy Twoje kolejne wizyty jako jedną historię (podstawa: zgoda, art. 6 ust. 1 lit. a RODO; możesz ją wycofać w ustawieniach cookies). Dane przechowujemy przez 90 dni. ``` ## Turn off, override, extend - **Turn off:** without the `stats` option nothing is added; `stats: { dashboard: false }` hides only the dashboard chart; without `` (or with `SiteProviders stats` = `false`) nothing is sent. - **Override:** `retentionDays`, `timeZone`, `collectionOverrides` (e.g. `{ access: { read: ({ req }) => req.user?.email === 'owner@acme.pl' } }` in a project without roles); `` for your own endpoint path. The counting rules (bots, prefetch, 30 min, 10 s) currently cannot be changed with an option. - **Extend:** `STATS_SLUG` + the types `StatsOptions` / `StatsVisit` / `StatsPage` (from `@intecion/ipal-kit`) for your own queries and reports (Local API); `StatsView` / `StatsWidget` from `/admin` can be wired by hand. The collection and endpoint builders are currently not publicly exported.