r/webdev • • 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.

0 Upvotes

35 comments sorted by

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/No_Advertising2536 Oct 11 '26

Yeah, that would split office NAT nicely. I've held off because every extra device signal pushes it closer to fingerprinting, which is the thing I'm trying to stay away from. Bucketed resolution is probably fine though, might test it.

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/No_Advertising2536 Oct 12 '26

True, relay splits one person into several IDs, so it pushes the other way from NAT. Apple publishes the relay IP ranges, so the share is at least measurable.

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/No_Advertising2536 Oct 13 '26

Fair, "rare" was the wrong word, especially on mobile. Added a plain trade-offs section to the docs today: shared IPs undercount, and no cross-day retention for anonymous visitors unless you identify() signed-in users. Agree it's a different tool, not a broken one.

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/No_Advertising2536 Oct 14 '26

Good point on IPv6, I'd only thought about the undercount side. Fixed the docs, they said "never high", which was wrong. Midnight is already handled (we check yesterday's salt too), and for IPv6 I'm thinking of hashing the /64 instead of the full address, since privacy extensions only rotate the interface part.

1

u/glensum_app Oct 15 '26

Glad it helped. /64 hashing is the right call for IPv6 - with privacy extensions (RFC 8981) the interface ID rotates but the /64 stays stable, so you end up counting ISP prefixes instead of rotating devices, which matches the intent. One honest caveat: IPv4 users behind CGNAT share one IP across households, so there it's IP+UA hashing or nothing - and mobile carrier traffic will always undercount no matter what. Sounds like your docs now say that out loud, which is more than most analytics tools do.

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.

1

u/No_Advertising2536 Oct 15 '26

You're right, over a month it's really visitor-days, not people, and better retention inflates it. I've added that to the docs. For growth I'd look at the daily trend and at new referral sources, not the monthly total.

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?

0

u/[deleted] Oct 11 '26

[removed] — view removed comment

1

u/webdev-ModTeam Oct 11 '26

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.

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 11 '26

[removed] — view removed comment

1

u/webdev-ModTeam Oct 11 '26

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.

1

u/[deleted] Oct 12 '26

[removed] — view removed comment

1

u/No_Advertising2536 Oct 12 '26

Thanks! In-memory is tempting, but on a normal multi-page site it resets on every navigation, so it only helps SPAs. sessionStorage fixes that but then I'm storing something on the device, and "nothing stored in the browser" is the whole promise (ePrivacy doesn't care that it's session-scoped). Might still be worth it as an opt-in for people who want accuracy over the no-banner claim.

1

u/[deleted] Oct 12 '26

[removed] — view removed comment

1

u/No_Advertising2536 Oct 12 '26

Good point on constant vs moving bias, that's a better way to frame it than "accuracy". The events-per-ID idea is nice, flagging an ID that behaves like 200 people and showing a range instead of a fake exact number. Did you pick a threshold by eye, or something like a percentile cutoff?

-2

u/[deleted] Oct 11 '26

[removed] — view removed comment

1

u/webdev-ModTeam Oct 11 '26

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.

-1

u/[deleted] Oct 11 '26

[removed] — view removed comment

1

u/webdev-ModTeam Oct 11 '26

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.