Overview
The Analytics system tracks page views using aggregated daily counters and a session ring buffer. No personal data (IP addresses, user agents, visitor IDs) is persisted to the database.Type Definition
Privacy by Design: The
knownVisitors map uses FNV-1a hashes of (IP+UserAgent) for session deduplication. These hashes exist only in memory and are never written to the database.Initialization
Initialize
PocketBase application instance
*Analytics- Initialized analytics instanceerror- Error if collection setup fails
core/analytics/analytics.go:33
Process:
- Creates
_analyticscollection (daily aggregated counters) - Creates
_analytics_sessionscollection (recent visit ring buffer) - Starts session cleanup worker (runs every 30 minutes)
Route Registration
RegisterRoutes
ServeEvent to bind middleware
core/analytics/collector.go:15
Example:
Tracking Behavior
What Gets Tracked
The system tracks:Request URL path
Device category:
"desktop", "mobile", "tablet"Browser:
"chrome", "firefox", "safari", "edge", "opera", "unknown"Operating system:
"windows", "macos", "linux", "ios", "ipados", "android", "unknown"Whether this is a new session (first visit within 30-minute window)
What Does NOT Get Tracked
- IP addresses (used only for session hashing, never stored)
- Full user agents (used only for parsing, never stored)
- Visitor IDs or cookies
- Personal information
- Query parameters
- Request headers
Excluded Paths
These paths are automatically excluded from tracking: API and System Routes:/api/*/_/*/_app/immutable/*/.well-known/*
/favicon.ico/service-worker.js/manifest.json/robots.txt
- CSS:
.css - JavaScript:
.js,.json,.map - Images:
.png,.jpg,.jpeg,.gif,.svg,.ico,.webp,.bmp,.tiff,.tif,.heic,.heif,.avif - Video:
.mp4,.webm,.ogg,.ogv,.mov,.avi,.wmv,.flv,.mkv,.m4v,.3gp - Audio:
.mp3,.wav,.flac,.aac,.m4a,.wma,.opus - Documents:
.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.rtf,.csv,.md - Archives:
.zip,.rar,.7z,.tar,.gz,.bz2 - Fonts:
.woff,.woff2,.ttf,.eot,.otf
core/analytics/collector.go:204
Bot Detection
Requests from these user agents are excluded:bot,crawler,spiderlighthouse,pagespeed,prerenderheadless,pingdomgooglebot,baiduspider,bingbotyandex,facebookexternalhitahrefsbot,semrushbotscreaming frog- Empty user agent strings
core/analytics/collector.go:255
Session Management
Session Window
Default session window: 30 minutes A visitor is considered “new” if they haven’t been seen within the last 30 minutes.Session Hash
Sessions are tracked using FNV-1a hashes:- Fast, non-cryptographic hash
- 64-bit hash value (16-character hex string)
- Deterministic (same IP+UA always produces same hash)
- Collision-resistant for practical purposes
- Never written to database
core/analytics/collector.go:142
Session Cleanup
Background worker runs every 30 minutes to remove expired sessions from memory:core/analytics/analytics.go:47
Data Storage
Daily Counters (_analytics)
Aggregated view counts stored as:core/analytics/collector.go:62
Session Ring Buffer (_analytics_sessions)
Recent visits stored as ring buffer (max 50 entries):core/analytics/collector.go:103
Complete Examples
Basic Setup
Custom Session Window
Conditional Tracking
Testing Analytics
User Agent Parsing
- Contains “mobile” or “android” →
"mobile" - Contains “tablet” or “ipad” →
"tablet" - Default →
"desktop"
- Contains “chrome” (not “edg”) →
"chrome" - Contains “firefox” →
"firefox" - Contains “safari” (not “chrome”) →
"safari" - Contains “edg” →
"edge" - Contains “opera” →
"opera" - Default →
"unknown"
- Contains “windows” →
"windows" - Contains “macintosh” or “mac os” →
"macos" - Contains “linux” (not “android”) →
"linux" - Contains “iphone” →
"ios" - Contains “ipad” →
"ipados" - Contains “android” →
"android" - Default →
"unknown"
core/analytics/collector.go:156
Constants
core/analytics/types.go:6
Best Practices
- Privacy First: Never log or store IP addresses or personal data
- GDPR Compliance: System is designed for GDPR compliance (no cookies, no personal data)
- Performance: Tracking happens after response is sent (non-blocking)
- Data Retention: Clean up old analytics data (90-day retention recommended)
- Bot Filtering: Rely on built-in bot detection, don’t reinvent
- Session Window: 30 minutes is standard, adjust based on your use case
- Testing: Test with real user agents, not synthetic ones
Related
- Analytics Storage - Data retrieval and aggregation
- Analytics Dashboard - Viewing analytics in the UI
- Privacy Guide - Privacy-focused design principles