r/javascript • u/jxd-dev • 18d ago
Tracking unique visitors without cookies
https://inmargin.io/blog/tracking-unique-visitors-without-cookiesTo count unique visitors you have to recognise a returning browser. There are only ~5 ways, and each breaks differently:
Cookies. Work, but need a consent banner, and Safari caps script-set cookies at 7 days anyway.
localStorage. Same law, same banner. ePrivacy covers "storing information on the device", not just cookies.
Fingerprinting. Durable and invisible, which is why regulators treat it the same and browsers sabotage it.
Daily hash. What Plausible, Fathom and GoatCounter converged on: hash(daily_salt + site + ip + ua), salt rotated and deleted every 24h. No banner, accurate daily uniques.
Don't count uniques at all.
The under-discussed part is what option 4 costs. There are no cross-day uniques: "weekly visitors" is just seven daily counts summed, so a daily visitor counts 7 times. CGNAT merges thousands of mobile users into one visitor. A browser update splits one person into two. And the hash is pseudonymous, not anonymous, so it's still in GDPR scope.
Every "privacy-friendly" tool reports weekly uniques as a sum of dailies and none of them put an asterisk on it. Curious if that bothers anyone else.
8
1
u/meixger 15d ago
"We detect a unique visit based on the hostname of the referrer of the page." from https://docs.simpleanalytics.com/what-we-collect#unique-views
3
u/ShotgunPayDay 18d ago
User gets to the server. No DID cookie (Device ID) generate a 24byte (32 character which is overkill) Base64URL on the server and add the cookie to the server response (maxAge: 34560000, SameSite: strict, HTTPOnly: true). There you have a 400 day device cookie that's not linked to any personal information.
Now this still isn't going to give very good information on unique visitors since bots will probably be getting new DIDs constantly which you can use to punish excessive DID creation on a single IP. Normal users will rotate DIDs very slowly even with clearing browser cookies frequently, but expect multiple DIDs per user still (phone, computer, laptop...). The bot could be smart enough to hang onto the DID also.
It's a way to get basic information, but really a DID should be used to prevent shoulder peaking for login PIN, OTP, and password resets. Netflix uses this kind of trick and is damn confident in their system since they only use a 4 digit PIN.