In July I wrote about visitor stats polluted by bots: a scanner posing as a browser had slipped in a fake visitor from Turkey. The fix back then was to report a visit only after the browser actually executes JavaScript. Most scanners do not run JS, so by design they are never counted.
That post ended by saying the stats were clean. They were not; I just could not see the dirty part.
I found it through a different dataset
On the evening of 11/04 I added real-visitor Web Vitals to the site: the browser measures LCP, INP and CLS, sends them back, and I store them to compute p75, which I use to decide whether to change the home page animation.
A few minutes after launch, I opened the data and there were already 40-odd entries. This site does not have that many visitors. Even stranger, the project detail pages had an FCP of 9.6 seconds, which even a very slow phone should not produce.
The 9.6 seconds looked familiar: it is how long the first compile of a page takes in Next.js dev mode.
The culprit: my own E2E tests
On every push to main, GitHub Actions runs the Playwright E2E tests on this very desktop (a self-hosted runner). The tests start a next dev and click through the home page, project pages and article pages in a browser.
To the stats code, these tests look like perfect "real people":
- They really execute JavaScript. The July defense blocked "programs that do not run JS", but E2E uses a real Chromium.
- Their User-Agent is an ordinary Chrome. Playwright's
devices["Desktop Chrome"]emulates desktop Chrome, the string has noheadless, so the UA blacklist could not catch it either. - They write into the same folder. E2E runs inside the production directory, and the stats file sits next to it in
data/.
Each push put dozens of fake Web Vitals in.
What about the visitor stats?
If Web Vitals got hit, the visitor stats must have too: E2E opens the home page every time, and the home page footer reports one visit.
Opening data/visitor-stats.json, there was a country code I had never seen:
1,037 entries of XX, more than ten times the real visits.
The old code looked like this:
The country is read from cf-ipcountry, a header Cloudflare adds at its edge. E2E hits the desktop's localhost directly, never passes through Cloudflare, so there was no such header and the visit was recorded as XX.
And the display code happened to filter out XX (originally meant for cases where Cloudflare cannot identify the country):
So the number in the footer was always right, and all the fake data hid in a field that was never displayed. The E2E tests and the visitor stats were added on the same day (7/17), so it had been accumulating from day one, quietly, for three and a half months.
The fix: check whether it went through Cloudflare
A real visitor always comes in through the Cloudflare Tunnel and always carries cf-ipcountry. A request without that header is not a visitor, however much it looks like a browser:
Web Vitals use the same rule. When Cloudflare itself cannot identify the country it also sends XX; that request did pass through Cloudflare, so it is still recorded.
This is far more reliable than a UA blacklist: the UA is whatever the client claims, while cf-ipcountry is added by Cloudflare, cannot be changed by someone connecting from outside, and someone hitting localhost cannot fake "went through Cloudflare".
What I learned
- Your own tests are the most human-looking bots. They run JS, use a real browser, and have a normal UA. Stats have to exclude not only outsiders but yourself.
- "Not displayed" does not mean "not there". Filtering out
XXkept the screen clean, and also hid the problem for months. This time a different dataset went wrong, and looking back revealed it. - Keep test data and production data in different places. The header check is a patch; the root problem is that E2E and the live site share
data/. - Filter on facts, not on self-declarations. The UA is a self-declaration; "did it go through Cloudflare" is a fact.