Cookieless, GDPR/LSSI-Compliant Telemetry in 100 Lines of Code (Including Timezone-to-Country Attribution)
Privacy & Observability · 6 min
How to measure page views, traffic sources, Core Web Vitals, and visitor countries using navigator.sendBeacon, Cloud Logging log-based metrics, and IANA timezone mapping—with zero cookies, zero IP storage, and no cookie banner.
Nothing ruins the first impression of a clean technical site faster than a giant GDPR cookie-consent banner asking permission to load Google Analytics. Under Spain's LSSI Art. 22.2 and the EU ePrivacy Directive, any non-essential storage on the visitor's device (document.cookie, localStorage client IDs) requires prior informed consent. Under the GDPR, storing raw client IP addresses or browser fingerprints in analytics tables requires a lawful basis and retention controls.
When I built the observability pipeline for this site (ADR-011, ADR-031, ADR-037), I wanted real production telemetry—page views by tab, traffic source (direct, search, linkedin, github), device form factor, country distribution, and client-side SLO latencies—without setting a single cookie or storing a single personal identifier.
1. A 100-Line `navigator.sendBeacon` Ingestion Sink (`server/routes/telemetry.ts`)
On route transitions, src/telemetry.ts sends a fire-and-forget navigator.sendBeacon('/api/telemetry', payload) with Content-Type: text/plain (a CORS simple request that never triggers a preflight OPTIONS round-trip).
On the server, server/routes/telemetry.ts validates the payload against a strict allowlist—never echoing arbitrary client JSON into Cloud Logging—and emits a single structured JSON line to stdout:
logStructured('INFO', 'Client telemetry: page_view', {
component: 'client',
event: 'page_view',
path, // '/' | '/experience' | '/demos' | '/architecture' | '/blog' | '/contact' | '/status' | '(not found)'
lang, // 'en' | 'es'
source, // 'direct' | 'search' | 'linkedin' | 'github' | 'ai-assistant' | 'internal' | 'other'
referrerHost, // e.g. 'google.es' or ''
device, // 'desktop' | 'mobile' | 'tablet'
country, // ISO 3166-1 alpha-2 (e.g. 'ES', 'US', 'GB') or 'unknown'
});
Terraform provisions a Cloud Logging log-based counter metric (page_views_<env>) that extracts path, lang, source, device, and country as metric labels. Cloud Monitoring aggregates those counters into time series that power my public How This Site Runs (/status) dashboard (ADR-037).
2. Why IP-Based Geo Headers Fail Privacy & CDN Topologies
Relying on client IP addresses or CDN GeoIP headers (X-AppEngine-Country) has two drawbacks: first, under GDPR, deriving analytics from client IP addresses brings IP processing into the analytics scope; second, behind CDN edge rewrites, upstream proxy hops can obscure or overwrite origin country headers.
3. Ephemeral IANA Timezone-to-Country Attribution (`ADR-031`, `ADR-037`)
How do you get accurate country attribution without a GeoIP database or ever inspecting client IPs? Every modern browser knows its IANA timezone via Intl.DateTimeFormat().resolvedOptions().timeZone (e.g., Europe/Madrid, America/New_York, Europe/London).
The IANA zone1970.tab database maps each canonical timezone to its ISO 3166-1 alpha-2 country code. In src/telemetry.ts and server/routes/telemetry.ts, the beacon includes the browser's IANA timezone, and countryForTimeZone() resolves it in memory to a 2-letter country code (or 'unknown' for UTC / unmapped zones, never falling back to the IP address) and discards the timezone string immediately:
- Zero cookies or
localStorageIDs on the device (LSSI Art. 22.2 exempt). - Zero IP addresses or user-agent strings stored in the metric (HTTP request logs mask IPs to
/24and/48viaanonymizeIp()). - Accurate, privacy-first country attribution, verified by
tests/backend/privacy.test.ts.