lib-visits

Cookieless web analytics: a 3 KB script and a 1 MB static collector that counts visits and the path each one took, written as JSON lines.

lib-visits Download
Buy me tokens

Visits and journeys for your site. No cookies, no storage unless you opt out, no fingerprinting. One script of 3,068 bytes minified on the page and a 1.1 MB static binary on the server, which as a FROM scratch image is 0.6 MB compressed, idling at 2.6 MiB of memory, that writes what it sees as JSON lines.

  • Counts visits, not pageviews. A visit is a keyed hash of the day, the address and the browser: it cannot survive the day and the address is never written.
  • Knows the path a visit took. Same-site referrers chain pageviews together; a leave beacon carries engaged seconds, scroll depth and whether the end was read.
  • Carries real-user Web Vitals, optionally, in a second script of 995 bytes.
  • Yours. The collector is a static binary that runs anywhere; events come out of stdout as JSON lines and go wherever you keep logs. GPL-3.0.

Setup.

FreeOpen source

  1. Download visits for your platform:
  2. Run it: VISITS_SECRET=$(openssl rand -hex 32) ./visits serve --origin https://example.com
  3. Add the tag: <script src="/visits.min.js" data-visits="https://stats.example.com/v"></script>

Or build it: cargo build --release -p visits, or the Dockerfile for the same binary in an empty image.

Each event is one JSON line on stdout. Pages on localhost or file: are not counted unless the tag also carries data-visits-local.

Behind your site

Proxy the path so the beacon is same-origin (no CORS, fewer blockers), use data-visits="/v", and tell the collector how many proxies sit in front: --trusted-proxies 1 behind this nginx block. Too low and every visitor shares the proxy's address, too high and none has a visit; confirm it once with --log-ip.

location = /v {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

Running it in the cloud.

The site's host rewrites /v to the collector, the collector writes to stdout, the cloud keeps the lines. The address stops at the collector; the logs hold only the JSON lines.

GCP: scales to zero. Behind a Firebase Hosting rewrite the depth is 2 (Hosting and Cloud Run's frontend).

AWS: not zero at idle; the smallest Fargate task is a few dollars a month, App Runner paused bills memory only.

Azure: scales to zero on the Container Apps consumption plan.

Your own box: nginx or Caddy in front, --sink file:/var/log/visits.jsonl, and logrotate with copytruncate (the file is opened once, in append mode).

Reports from installed programs (below): the collector writes no address for them, but the platform's request log does; Cloud Run's carries the remote address of every request. Exclude the service's request-log entries for the report routes, including the 302 of a check, from the default bucket, or say on the program's disclosure page that the platform keeps the address for its retention.

Installed programs.

A program on someone's computer can report to the same collector. A report is a typed JSON object (a session summary, a crash or the daily update check) with no identifier in it, and the collector computes no visit for it and writes no address. Tell the collector which programs may post: --app arkos=https://example.com/arkos/latest.json (repeatable; VISITS_APPS, comma-separated). The redirect is where a check is sent; leave it off and a check is answered 204.

  • Two routes: POST /a takes a report and GET /a/check takes the day's update check as a query string; --app-path (or VISITS_APP_PATH) moves them. The check answers 302 to the redirect, so the update itself never passes through the collector.
  • The User-Agent must be <app>/<version> and the report's app must be an allowed one, or the answer is 403. A report from a source build (ch of source) is 403 and is not written.
  • A report may carry enumerated values, versions, counts and hashes. It may not carry free text, an identifier or any key the schema does not know: that report is refused whole with 400, not trimmed.
  • Uploads are off until the program's own setting turns them on; DO_NOT_TRACK, <APP>_TELEMETRY=off, CI and a source build keep everything local, and <APP>_TELEMETRY=log prints what would be sent instead. Sessions recorded under Local in the last seven days upload once the setting turns on; a program that wants a clean start deletes queue/session-* at that moment (Queue::clear_sessions).

The client half is a crate, visits-app, with the report types, the journal, the queue and the consent rules; the program supplies the network and the window.

visits-app = { git = "https://github.com/csarkosh/lib-visits", tag = "v0.3.0" }

The program lists the events it records, what each may carry and the surfaces it counts time in, in a file it ships:

[events.pane_open]
kind = ["terminal", "viewer", "browser"]
[events.window_open]
[focus]
surfaces = ["terminal", "viewer", "browser"]

The program's loop (seal, queue, upload, check) is one no_run example in the crate docs, crates/visits-app/src/lib.rs; the wire format is section 6 of spec/SPEC.md.

The details.

More

  • Sinks: --sink stdout (default) or --sink file:<path>.
  • Single-page apps: the script counts a view when the page loads; after a route change, call visits.page(). It sends the page before's leave and a view of the new path with the page before as its referrer (nt: "spa"), and does nothing while the path is unchanged.
  • Returning visitors: --visitor (or VISITS_VISITOR=true) adds s.vid beside the daily visit, a hash keyed on the secret alone, so a browser that comes back another day is the same visitor for as long as its address and User-Agent hold. Off by default, since it links a browser's days, which the visit never does; rotating the secret ends every visitor too.
  • Your own visits: --ignore-ips 203.0.113.9,2001:db8::/32 (or VISITS_IGNORE_IPS) answers beacons from those addresses and writes nothing. Reports and checks from them are written with "s": {"own": true}, so your own use of your program is kept apart, not lost.
  • Event time: each line's time is the browser's clock at the event (when within five minutes of receipt), so beacons that arrive together after a cold start keep their order.
  • Places: build with --features geoip and pass --geoip <file.mmdb> (or VISITS_GEOIP). A country database writes s.cc; a city one (DB-IP's IP to City Lite, CC BY 4.0, or MaxMind's GeoLite2 City) writes the region s.rg, the city s.ct and the city's coordinates s.la s.lo (to a tenth of a degree) too. The address is looked up and dropped, never written. The Docker image bundles DB-IP's City Lite of the month it is built in, about 130 MB, so the collector needs about 256 MB of memory. DB-IP's licence asks for "IP Geolocation by DB-IP" with a link to https://db-ip.com wherever its places are shown.
  • Networks: with --asn <file.mmdb> (or VISITS_ASN; DB-IP's IP to ASN Lite, CC BY 4.0, or MaxMind's GeoLite2 ASN, with the geoip feature) each event carries the network the address is on: its number s.an, its owner s.ao (an ISP, a company, a cloud) and, for clouds, VPN carriers and relays such as iCloud Private Relay, its kind s.nk. --tor <file> (or VISITS_TOR), the Tor Project's bulk exit list, makes a beacon from an exit nk tor. The address is looked up and dropped. The Docker image bundles the month's ASN Lite and the exit list as of its build.
  • Connections: a page that sends Accept-CH: ECT, RTT, Downlink, Save-Data gets Chromium's network hints on its beacons, written as s.ect, s.rtt, s.dl and s.sd. Other browsers send none.
  • Placing addresses for your other services: set VISITS_PLACE_TOKEN and POST /place with Authorization: Bearer <token> and one address as the body answers its place as JSON ({"cc":"US","rg":"Illinois","ct":"Chicago"}), writing nothing (spec/SPEC.md section 7).
  • Web Vitals: add <script src="/vitals.min.js"></script> after the core.
  • Behind a proxy: --trusted-proxies N (the client is the Nth address from the right of X-Forwarded-For; only the first X-Forwarded-For header line is read); confirm once with --log-ip, then turn it off.
  • Opt out: load any page with #visits-off; #visits-on undoes it.
  • The secret: keep it out of wherever the events go, and rotate it when you like; whoever holds both the secret and the events can test whether a given address and browser visited on a given day, and a rotation ends every visit at that moment.
  • Another language: spec/SPEC.md is the whole contract.
  • Not collected, on purpose: canvas, WebGL, audio, fonts, high-entropy client hints, the query string beyond campaign parameters, anything across days.