· 7 min read
Cookieless Web Analytics: Counting Visitors with a Daily Salt
The usual way to count a site's visitors is to write a cookie into each visitor's browser: if the same cookie comes back, it is the same person. Easy, but it has a price: a lasting identity that recognises the visitor for days and weeks. While building Nabız, my own web analytics tool, I wanted a way to count unique visitors without cookies and without recognising anyone. This post is about how cookieless analytics works, the daily salt at its core, and the price the method pays on purpose.
The first idea and why it falls short
Without a cookie, the visitor has to be recognised from the request itself. Every request already carries two pieces of information: the IP address and the User-Agent string the browser identifies itself with. Combining them and taking a hash is the first idea:
visitor = SHA-256(IP + User-Agent)
The same person on the same computer produces the same value, and the IP itself is not stored. On paper it looks anonymous. It isn't.
IPv4 has about 4.3 billion addresses, and the number of common browser strings is limited. A graphics card can compute billions of SHA-256 hashes per second. Someone holding these hashes can try every IP address in turn and quickly find which hash belongs to which address. An unsalted hash does not hide personal data; it only puts it a little further away.
The daily salt
The fix is the method tools like Plausible use too: add a random, secret value to the hash that changes every day, a salt.
visitor = SHA-256(today's salt ‖ site ‖ IP ‖ User-Agent)
The salt is generated on the server: 32 random bytes. It changes three things:
- Without the salt, the hash cannot be reversed. Trying every IP also requires knowing the salt.
- The next day the salt changes and the old one is deleted. Who a hash from yesterday belonged to can no longer be computed, not even by the server.
- The site goes into the hash too. The same person gets two different identities on two different sites; there is no cross-site tracking.
In Nabız this lives in one package:
// Visitor builds a visitor's identity for today:
// the first 8 bytes of SHA-256(salt ‖ site ‖ IP ‖ User-Agent).
// IP and User-Agent are used here and discarded; they are never written anywhere.
func (k *Keeper) Visitor(ctx context.Context, siteID, ip, userAgent string) (int64, error) {
s, err := k.current(ctx)
if err != nil {
return 0, err
}
h := sha256.New()
h.Write(s)
for _, field := range []string{siteID, ip, userAgent} {
// Each field is prefixed with its length; otherwise ("ab", "c") and ("a", "bc")
// would give the same bytes and two different visitors could share an identity.
var n [4]byte
binary.BigEndian.PutUint32(n[:], uint32(len(field)))
h.Write(n[:])
h.Write([]byte(field))
}
sum := h.Sum(nil)
// SQLite INTEGER is a signed 64-bit value; the uint64 is converted to int64 as is.
return int64(binary.BigEndian.Uint64(sum[:8])), nil
}
There is a small but important detail here: before writing the fields one after another, each one is prefixed with its length. Otherwise a site named "ab" with IP "c" and a site "a" with IP "bc" would produce the same bytes. Rare in real data, but making the hash input unambiguous is a cheap guarantee.
The first 8 bytes of the hash are used because they only need to fit in an SQLite integer column. Among one site's visitors in one day, the chance of a 64-bit collision is negligible.
How the salt rotates
There is no separate scheduler. On the first request after the day changes, a new salt is generated, written to the database, and the salts of all other days are deleted. All in one transaction: if it fails halfway, neither is the new salt written nor the old one deleted.
// rotate fetches or generates the given day's salt and deletes the salts of every
// other day, all in one transaction: if it fails halfway, nothing changes.
func (k *Keeper) rotate(ctx context.Context, day string) ([]byte, error) {
// ...
if _, err := tx.ExecContext(ctx, `DELETE FROM salts WHERE day <> ?`, day); err != nil {
return nil, err
}
return value, tx.Commit()
}
The salt is kept in memory as well, and when a new one arrives the old one's bytes are wiped with clear(k.value). The salt is also written to the database because the server may restart during the day: if it lived only in memory, an afternoon update would split that day's visitors in two.
The tests pin these behaviours one by one: same day, same identity; next day, a different one; the old salt is deleted; a restart keeps the salt; and 20 requests arriving at the moment the day changes all use the same new salt.
Less data leaves the browser in the first place
Hashing protects the server side, but what the browser sends matters too. Nabız's script does not send the full page address, only its path and campaign parameters:
// Sent address: only the path and source parameters. Everything after "#" (e.g. the
// decryption key in Zarf) and other query values (e.g. ?token=...) never leave the browser.
var keep = ["utm_source", "utm_medium", "utm_campaign", "ref", "source"];
A bug taught me this rule. The first version sent location.href, the whole address. In Zarf, my file sharing tool, the decryption key sits in the # part of the address. Had I added Nabız to Zarf, users' file keys would have gone to the analytics server. I had put the key after # precisely so it would never leave the browser; the analytics script would have broken that promise.
Pages with IDs in their address had a similar issue. In Seans, my cinema site, there is /bookings/<id>; in Casus, room codes are in the address. They would have shown up one by one in the page list of a public dashboard. With the data-mask setting, these pages are sent as a pattern like /bookings/:id; the real ID never leaves the browser, and they are grouped into one row in the dashboard.
The IP address is not written anywhere either: it is used to compute the visitor identity and the country, then discarded. The access log of the Caddy server in front is turned off as well.
The price
The method has limits accepted on purpose:
- Returning visitors cannot be measured. The same person counts as a new visitor the next day. Since the salt changes daily, that is not a bug but the point.
- The day boundary is UTC. The salt changes at midnight UTC, which is 03:00 in Turkey. Someone browsing at that hour may be counted twice.
- Identical browsers on the same network can look like one person. Two people in an office with the same IP and the same browser get the same identity. The opposite happens too: if a mobile network changes the IP during the day, one person is counted twice.
In return, nothing is written to the browser, there is no identity that recognises a person across days, and the data on the server cannot be linked back to anyone once the salt is deleted. That is why no cookie banner is needed.
What I took away
- An unsalted hash is not anonymisation. When the input space is small, as with IP addresses, the hash can be reversed by trying every value.
- Deleting the salt matters as much as deleting the data. Privacy rests on the old salt really being gone, which is why rotation is one transaction and the memory is wiped too.
- The fields going into a hash must be unambiguous. A simple rule such as a length prefix stops two different inputs from producing the same bytes.
- The best data is data never collected. Limiting what leaves the browser matters as much as protecting the server: a path, not the full address; a pattern, not the ID.
- Every method has a price, and the price should be written down. No returning visitors is not a gap but a decision made for privacy.