Skip to content
All projects
In progress

RWAP

A small, self-hosted, privacy-conscious analytics platform that answers one question well: how many people used my website?

On this page
Ended
Ongoing
Type
project

Built with

Overview

An SDK of roughly 3 KB gzipped, a private dashboard, and one container plus PostgreSQL. It reports unique visitors, sessions, page views, visitors over time, top pages, referrers, countries, devices and recent activity.

Built to be reused across every project rather than reaching for a third-party script that watches the people reading them.

Architecture

One Fastify (Node + TypeScript) container serving three things from one origin: the public collection endpoint, the private dashboard API, and the dashboard SPA itself — plus PostgreSQL.

your websites          POST /api/v1/collect           Fastify API
  SDK (pk_live_…) ───────────────────────────────▶  ├─ public:  /collect
  (write-only)                                        ├─ private: /projects/...
                                                       ├─ static:  dashboard SPA
you, dashboard          cookie session                └─ static:  /sdk.js
  browser ───────────────────────────────────────▶         │
  (read/admin)                                              ▼
                                                        PostgreSQL

Plain SQL migrations instead of an ORM — analytics queries are aggregate SQL anyway, and the dashboard SPA is served by the same API it calls, so there's no CORS and a simple SameSite cookie does the whole job. The SDK itself has zero dependencies and ships at roughly 3KB gzipped.

Seven tables, every tracking table carrying project_id and every analytics query filtering on it first: users, auth_sessions, projects, visitors, sessions, page_views, events.

Testing

API tests run against a real PostgreSQL, not a mock: visitor identification, session creation, duplicate handling, invalid keys, malformed and oversized payloads, origin checks, bot filtering, per-IP and per-project rate limiting, forged and expired sessions, CSRF content-type checks, and stats correctness across date ranges.

One property is tested on every single endpoint, not just the obvious ones: project A can never read, modify, rotate or delete project B — through the session, the secret key, or the public key. SDK tests cover visitor-id persistence, session timeout, SPA route-change tracking, and failure behaviour when the server is down or storage is blocked.

Decisions

Two credential types, never confused. The public tracking key (pk_live_…) lives in website JS anyone can read and is treated purely as an identifier — it can submit events and nothing else. The private key (sk_live_…) is shown once, stored as a SHA-256 hash, and can only read its own project. Every private route goes through one function that combines the URL's project id with the caller's identity in the same WHERE clause, so another project's id returns a 404, not a 403 — ids can't even be probed.

Privacy by never collecting the sensitive thing, not by anonymizing it after collection. No IP addresses are ever written to the database (used only in memory, as an HMAC, for rate limiting). No cookies on visitor browsers. No fingerprinting — the same browser on two different sites counts as two unrelated visitors, by construction, because there is no cross-site identifier to link them. Full URLs are discarded down to the path; query strings and referrer hostnames are kept only because they can't reasonably contain personal data at that granularity.

The abuse-prevention list is long because a public write endpoint has to assume hostility: 16KB payload cap, strict Zod schemas rejecting unknown fields, batch size capped at 25 events, bot/headless-browser filtering server-side from the User-Agent, origin-vs-allowed-domain checks, per-IP and per-project rate limits, and idempotent event ids so a retried batch can't double-count. None of it claims to stop a determined attacker from sending some fake traffic — that's true of every client-side analytics tool — it just keeps that expensive and bounded.