Files
ipal-kit/src/modules/stats/visitKey.ts
T
Radosław SmolińskiandClaude Opus 5.5 4ec27d9ef0 Built-in statistics (cookieless, consent-gated visitor history); per-page meta descriptions, NoSnippet
Stats: ipalKit({ stats: true }) adds the stats-visits collection, POST /api/ipal/hit, the
/admin/stats view (30-day visits chart, top/entry pages, referrers, devices), a dashboard card
and a nav link. <PageStats> (or <SiteProviders stats>) sends one beacon per page view. Without
consent nothing is stored on the device and no IP is kept: page views are joined by a hash with
an in-memory salt replaced daily. With analytics consent an ipal_vid cookie links visits across
days (server re-checks the consent cookie and policy version; withdrawal deletes the cookie).
Bots, logged-in panel users and prefetches aren't counted. Retention 90 days.
@payloadcms/next added to peerDependencies (DefaultTemplate). Docs: stats.md with privacy text.

Also in this release: meta.description auto-filled from page blocks, <NoSnippet>,
MetaDescriptionCheck in the SEO tab, data-nosnippet on banner/language switcher, GA4 CSP
wildcards, consent version passed to Analytics' default consent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-21 15:10:55 +02:00

20 lines
967 B
TypeScript

import { createHash, randomBytes } from 'node:crypto'
/**
* Cookieless visit key: SHA-256 of a random salt + IP + user agent + host. The
* salt lives only in this process's memory and is replaced when the day
* changes — the old one is gone, so nobody (us included) can link a stored key
* back to an IP or to the same visitor on another day. Nothing is stored on
* the visitor's device, which is why this needs no consent.
*
* Trade-off: a restart or a second instance starts its own salt, so a visit in
* progress can split in two. Fine for statistics; never fixed by persisting the
* salt — that would make yesterday's keys reversible.
*/
let current: { day: string; salt: Buffer } | null = null
export function visitKey(day: string, parts: string[]): string {
if (!current || current.day !== day) {current = { day, salt: randomBytes(32) }}
return createHash('sha256').update(current.salt).update(parts.join('\n')).digest('hex').slice(0, 32)
}