r/webdev • u/No_Advertising2536 • Oct 11 '26
Showoff Saturday How I count unique visitors without cookies or storing IPs, and where it breaks
I've been building a small web analytics tool, and the part people ask about most is how it counts "unique visitors" without cookies, localStorage, or stored IPs. Here is the approach and its honest limitations.
The ID
For every request, the server computes:
sha256(daily_salt | site_id | ip | user_agent), truncated to 16 hex chars
- daily_salt is 16 random bytes, generated once per UTC day and stored in the DB. Old salts are deleted after 24 hours, so once that happens, nobody (including me) can recompute yesterday's IDs from an IP.
- site_id is included in the hash so the same user gets different IDs across different sites (preventing cross-site tracking).
- The IP is used solely for the hash and a country lookup, then immediately dropped. It never hits the disk.
Sessions are defined simply as "same ID, active within the last 30 minutes" (one upsert per event).
Where it breaks (by design or trade-off)
- The same person visiting on two different days = two visitors. That's intentional, but it means "monthly uniques" is effectively a sum of daily uniques.
- Same office/NAT + same browser version = counted as one visitor. Rare outside large enterprise networks, but real.
- A phone switching from Wi-Fi to cellular mid-visit = new ID, new session.
- iCloud Private Relay / VPNs rotate IPs, which slightly inflates counts.
Based on my data, the margin of error is small enough to answer "is my traffic growing?", which is all most small sites need. It's not suitable for per-user product analytics; for that, you need explicit user identification.
The Client Side
- SPAs: I patch history.pushState and listen for popstate events, tracking route changes as pageviews without requiring framework plugins.
- Transport: Uses navigator.sendBeacon, falling back to fetch with keepalive so events survive tab closure.
- Engagement/scroll depth is flushed on visibilitychange, not unload (since unload is unreliable on mobile).
Happy to dive deeper into any part! Curious if anyone has a better trick for the NAT collision case that doesn't rely on client-side storage.
8
u/fauxtoe Oct 11 '26
ok?
1
u/Any-Ask-4131 Oct 11 '26
Does a session crossing midnight UTC get split when the salt changes, even if it's still within the 30-minute window? That seems worth calling out alongside the daily-uniques limitation.
1
u/No-Molasses-2097 Oct 11 '26
The usual trick for the NAT case is mixing a coarse device signal into the hash, like screen resolution bucketed into a few classes plus the platform token from UA client hints. It does not help much with carrier NAT on identical phones, but it splits up office NAT collisions pretty well since two desktops rarely share an exact resolution.
1
u/muharremyurtsever Oct 12 '26
another case pulling the other way: private relay rotates its egress ips over time and between sessions, so one safari user can split into several ids within a day. and if the relay share of your traffic changes, that bias won't stay constant either
1
u/Latter-Mix2317 Oct 13 '26
We do almost exactly this (random daily key per site, deleted afterwards, HMAC of key + IP + UA + site).
On CGNAT: yes, it merges people, and on mobile it's not rare. But the error is one-directional (undercount), and for "is this page doing better than last month" it mostly cancels out. I'd put it in the docs plainly rather than call it an edge case.
On "useless for repeat users": that's the trade, intentionally. You get daily visitors, funnels within a day, sources. You don't get 30-day retention per person. If you need that you need consent, and that's fine, it's just a different tool.
1
u/glensum_app Oct 14 '26
Two failure modes worth naming on top of the daily-uniques limit, because they push in opposite directions. IPv6 privacy extensions (RFC 4941) rotate the interface identifier on a schedule - sometimes hourly - so one phone can hash into several 'uniques' in a single day. CGNAT is the reverse: a whole subscriber pool collapses into one visitor, as noted above. A partial fix that also handles the midnight-UTC split: keep a rolling window of hour salts (the last ~25) and resolve the ID with the salt of the hour the session started in, instead of bucketing by UTC day. You pay one extra lookup and weaken the 'old salts are gone' guarantee from 24 to 25 hours - worth stating explicitly if you advertise that property.
1
u/geoffreybyers Oct 15 '26
A monthly unique count that sums daily uniques overstates growth by a factor of repeat visitor rates. A person who visits every day for a month shows as 30 uniques instead of one. The claim that this answers "is my traffic growing?" is only true if you assume the ratio of new to returning visitors is constant. If retention improves but acquisition stays flat, the number goes up even if your actual visitor count does not.
3
u/RemoDev Oct 11 '26
I use Matomo, cookieless, self-hosted.
Problem solved.
2
u/chumbaz Oct 11 '26
This is worthless for repeat users over time If you’re trying to use this for any tangible conversion tracking anyway.
What is the point of unique visitors if your scope is daily?
2
u/TinyCash599 Oct 11 '26
Rare outside large enterprise networks? Carrier CGNAT plus a common phone model and you've got a whole pool of subscribers collapsing into one visitor per day. That's not rare, that's a chunk of your mobile traffic. I'd want to see actual numbers before calling it rare.
1
u/[deleted] Oct 12 '26
[removed] — view removed comment
1
u/[deleted] Oct 12 '26
[removed] — view removed comment
-2
u/[deleted] Oct 11 '26
[removed] — view removed comment