Cookie-free website analytics without a consent banner
How this website counts page views and visitors with no cookies, nothing stored in the browser and no third-party script: a small beacon, a daily-changing hash in place of a visitor ID, and a short list of what is never saved.
- Topic
- Field Notes
- Published
- Updated
- Reading time
- 5 min read
Most analytics tools set a cookie or keep an ID in the browser so they can recognise a visitor next time. That is usually what brings in the consent banner. I wanted to know which pages people read and where they came from, without asking anyone to click "accept".
So this website counts its own visits. The browser script is 38 lines long, stores nothing on the visitor's device and sends data only to this site's own server. Here is how it works, and what it gives up.
What the browser sends
On each page load, the script sends one small message to /api/hit on the same domain. It uses navigator.sendBeacon, so the page does not wait for a reply. The message holds:
- the page path, such as
/blog.html - the page the visitor came from, which the server reduces to a site name
- a campaign label, if the link had
utm_sourceorrefin it
The script also listens for clicks on links. For a link to another website, it sends the link text and the destination's domain. Email and phone links are recorded simply as "Email" and "Phone". Links to other pages on this site are ignored, unless I have marked them for tracking or they are PDF or ZIP downloads.
It sends nothing when the browser reports that it is being driven by automation, and nothing on admin pages. It uses no cookies, no local storage and no session storage.
Counting visitors without an ID
Counting page views is easy. Counting people without an ID takes one more step. The server does it like this:
- Once a day, it creates a random secret, called a salt.
- For each hit, it runs the salt, the visitor's IP address and their browser's user agent through SHA-256, and keeps the first 16 characters as that visitor's code for the day.
- When a new day starts, the old salt is deleted.
visitor = hashlib.sha256(f"{daily_salt}|{ip}|{user_agent}".encode()).hexdigest()[:16]The same person on the same day gets the same code, so they count once. The next day the salt is new, so the code is new. Once a salt is gone, nobody can work back from yesterday's codes to an IP address, or link a visitor's Monday code to their Tuesday code. The IP address itself is never written to the database.
What is saved, and what never is
Each saved event holds the day, the page path, the 16-character daily code, a source name such as "Google", "LinkedIn" or "Direct", a device type (phone, tablet or desktop) and a browser name. Clicks also keep a short label and the destination.
Never saved: IP addresses, cookies, full referring URLs, query strings, form contents, names or email addresses.
The server also rejects any path that does not match a real public page, so nobody can fill the reports with made-up addresses. Events older than 400 days are deleted.
Keeping the numbers honest
- Requests whose user agent looks like a bot, crawler, link preview or uptime checker are ignored.
- One visitor code can record at most 300 events a day, so a script stuck in a loop cannot flood the reports.
- Requests sent from other websites' pages are ignored, and anything over 2 KB is dropped.
- My own visits are not counted while I am signed in to the admin.
- The endpoint always sends back the same empty reply, so a bot cannot tell what was kept.
What the report shows
The Visitors page in the admin shows page views, visitors, link clicks and real enquiries for the last 7, 30 or 90 days, each compared with the period before. Below that are the top pages, sources, devices, browsers and the outside links people clicked.
The enquiry count comes straight from the contact form's inbox, not from a click event, and suspected spam is left out. As I wrote in what to track first on a small business website, a click on "Send" and a saved enquiry are not the same thing. The spam filtering itself is described in stopping contact-form spam without a CAPTCHA.
What this approach gives up
- Someone who visits on Monday and again on Tuesday counts as two visitors. There is no way to see returning visitors, and that is the point.
- Two people on the same network, using the same browser version on the same day, may count as one.
- There are no funnels, heatmaps or session recordings.
- Ad and tracker blockers may still block the script.
- You build it, and you keep it working.
Do you still need a banner?
Cookie banners exist because many privacy rules ask for consent before a website stores or reads information on a visitor's device. This setup stores nothing on the device and reads nothing back from it, so there is no analytics cookie to ask about.
Whether your site needs a banner also depends on everything else it loads. Embedded videos, maps, chat widgets, advertising pixels and fonts served by other companies can all set cookies or share visitor data. Check the whole page, not just the analytics, and ask someone qualified about the rules that apply to your business.
If you would rather not build it
A ready-made tool is often the better choice. Umami is open source, its standard tracker works without cookies, and you can self-host it or use its Cloud plans. Umami Cloud vs self-hosted Umami compares the two options.
Want analytics like this built into your own website? See the sites I have built, or send me a few lines about what you need.
Written by Lay Shah, an independent developer building websites and tools for small businesses. Discuss a brief More articles