Skip to content

fix(traffic): store an anonymous visitor key instead of the visitor's IP address - #1410

Merged
gkhngyk merged 1 commit into
mainfrom
fix/traffic-anonymous-visitors
Oct 23, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/traffic-anonymous-visitors

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The tracking snippet's route stored each AI-referred visitor's IP address in ai_traffic_logs.ip_address. The address was only needed for two things, both short-lived:

  • grouping a visit's pageviews within 30 minutes (00172);
  • finding the pageview a not-found title belongs to (00200).

What changes:

  • A visitor key instead of the IP address. The route stores visitor_key: a one-way SHA-256 of the IP address, the brand and a salt that changes every UTC day (server/src/lib/visitor-key.js). The IP address itself is no longer written anywhere.
  • The salt. traffic_salt(day) (00250) makes each day's salt on first use and deletes salts older than the day before. Once a salt is gone, nothing links its keys to an address. A visitor gets a new key every day, so their days can't be joined.
  • Who can read the salts. traffic_daily_salts has RLS on and no access for anon or authenticated. The function is service_role only.
  • Matching. Visits and the not-found mark match on the key and the user agent, as they did on the IP address. A visit that crosses midnight UTC counts as two.
  • When no key can be made. If the salt can't be read, the pageview is still stored, without a key, as a visit of its own.
  • Data already stored. 00250 erases the IP addresses already stored. Their visits stay as they were grouped, since visit_of was decided when each pageview was stored.
  • Not touched. The rate limiter's in-memory keys aren't affected; they never reach the database.

Docs. The AI Traffic guide's privacy section says this in #1408.

Related issue

None. Found while updating the AI Traffic guide (#1380): it said "anonymized IP", which wasn't so.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Deploy order

  1. Apply 00250. The current server keeps working: the new column and function are only added.
  2. Deploy the server.
  3. Run the erase once more, for the rows the old server wrote in between: update ai_traffic_logs set ip_address = null where ip_address is not null.

How to test

  1. Visit a tracked site from an AI assistant, then reload.
  2. The AI Traffic page counts one visit with two pageviews.
  3. The rows have a visitor_key and no ip_address.

Tests:

  • visitor-key.test.js: the same key all day, a new key the next day, for another brand or another address; no address inside the key; the salt read once a day.
  • traffic-track.test.js: the row holds no IP address, and a failed key still stores the pageview.
  • traffic-visits.test.js: matches on the key.

Server: npm run format && npm run lint && npm test (2,132 tests) pass.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/): no web change
  • yarn typecheck passes (run from web/): no web change
  • yarn format:check passes (run from web/): no web change
  • Changes are focused — one concern per PR

🤖 Generated with Claude Code

… IP address

The tracking snippet's route stored each AI-referred visitor's IP address
in ai_traffic_logs. The address was only needed to group a visit's
pageviews for half an hour and to find the pageview a not-found title
belongs to.

The route now stores a visitor key instead: a one-way hash of the IP
address, the brand and a salt that changes every UTC day. The database
makes each day's salt on first use and deletes it after two days
(traffic_salt, 00250), so nothing links a key to an address once its salt
is gone, and a visitor's days can't be joined. Visits and the not-found
mark match on the key and the user agent, as they did on the address. If
no key can be made, the pageview is stored as a visit of its own.

00250 also erases the IP addresses already stored. Their visits stay as
they were grouped.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
@gkhngyk
gkhngyk merged commit a527f9f into main Oct 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant