Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

How I keep bots out of my own analytics, with no cookies and no third-party scripts

I run a small file converter, imageconvert.art. Like most side projects, I wanted to know one thing: are real people using it? I didn't want Google Analytics (cookie banner, a third-party script on every page, data I do

I run a small file converter, imageconvert.art. Like most side projects, I wanted to know one thing: are real people using it?

I didn't want Google Analytics (cookie banner, a third-party script on every page, data I don't need). So I wrote a tiny page-view counter of my own. On the first day it said I had visitors. Lots of them. Most of them, it turned out, weren't people.

Here's how I fixed it with five small checks. No cookies, no fingerprinting, nothing stored that identifies anyone.

The naive version

Count a view every time the server renders a page. That counts everything: Googlebot, Bingbot, uptime monitors, SEO crawlers, link previews from chat apps, and the random scanners that hit every new domain looking for /wp-login.php.

Moving the count to a JavaScript beacon fixes the simplest crawlers, because they never run JavaScript:

navigator.sendBeacon("/ui/v", JSON.stringify({ p: location.pathname, r: document.referrer }));

But plenty of bots do run JavaScript now: headless Chrome, preview renderers, and Google's own renderer. So I added more layers.

1. Only count a view after a real interaction

A person scrolls, moves the mouse, taps or presses a key. A headless browser that loads the page and leaves does none of that. And events a script fires itself have isTrusted === false, so they don't count either.

if (navigator.webdriver) return; // automated browsers announce themselves

const kinds = ["pointermove", "pointerdown", "touchstart", "wheel", "scroll", "keydown"];
let sent = false;
const onAct = (e) => {
  if (sent || !e.isTrusted || document.visibilityState !== "visible") return;
  sent = true;
  kinds.forEach((k) => removeEventListener(k, onAct, true));
  send();
};
kinds.forEach((k) => addEventListener(k, onAct, { capture: true, passive: true }));

This was the single biggest improvement. The trade-off: someone who opens a page and reads it without touching anything isn't counted. On a tool site where every visit involves clicking something, that's rare.

2. A signed token proves the page was really served

Anyone can POST to /ui/v. To make sure a ping came from a page my server actually rendered, every page carries a small signed timestamp:

<body data-vt="1763290000.3f9a1c...">

The beacon sends it back, and the server checks the signature (HMAC-SHA256 with a key that only exists in memory) and its age:

var age = now - DateTimeOffset.FromUnixTimeSeconds(secs);
return age >= TimeSpan.FromSeconds(1) && age <= TimeSpan.FromHours(12);

At least one second old means a script that fetches the page and fires the beacon instantly doesn't count. At most 12 hours means a token can't be harvested once and replayed forever. Because the key lives in memory, a restart invalidates old tokens; the cost is losing a few views, which I'm fine with.

3. Check Sec-Fetch-Site

Real browsers add a Sec-Fetch-Site header to every request. A beacon from my own page says same-origin. A request forged from another site says cross-site, so it's dropped:

var site = ctx.Request.Headers["Sec-Fetch-Site"].ToString();
if (site.Length > 0 && site != "same-origin") return;

(If the header is missing entirely, I let it through, since some older browsers don't send it.)

4. Cap views per visitor per day

No human views 60 pages of a file converter in one day. Past that, it's a script:

public const int MaxViewsPerVisitorPerDay = 60;
return _viewCounts.AddOrUpdate(hash, 1, (_, n) => n + 1) <= MaxViewsPerVisitorPerDay;

5. The usual user-agent list, and skip myself

Known bot user agents are dropped, and so are views from my own admin account, so testing doesn't inflate the numbers.

What about privacy?

To count unique visitors without cookies, I hash the IP address and user agent together with a random salt that changes every day and is never stored. The same person on the same day gets the same hash; tomorrow it's a different one, and nobody, me included, can turn a hash back into an IP. The raw IP is never saved.

Was it worth it?

The whole thing is about 80 lines across one C# class and one small script. What I got:

  • Numbers I actually believe, so I can tell whether a Reddit post or a new guide brought anyone.
  • No cookie banner, because nothing is stored in the visitor's browser.
  • No third-party script slowing down the page.

It won't stop a determined attacker who drives a real browser and fakes mouse movement. But nobody is doing that to inflate the stats of a side project. The goal is to filter out the everyday automated noise, and these five checks do that well.

If you want to see it running, the converter is at imageconvert.art. I'm happy to answer questions in the comments.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.