Menu

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

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

DEV Community: dotnet·Hanzala Rasool·1 day ago
#Kpvwhk6P
#devtodotnet#devto#dev#every#script
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.