Static Sites on Cloudflare Workers
A static site served by Cloudflare Workers (Hugo, Jekyll, Eleventy, plain HTML, ...) can be tracked without Laravel, without a script in the page and without a cookie. A small Worker in front of the static assets forwards every request to the Ingest API, so the tracking stays server-side and ad blockers cannot stop it.
If your site runs next to a Laravel app with signup or checkout, use the beacon script instead: it attributes the registrations and payments in the app to the visit, which a Worker cannot do.
Thanks
This setup is based on the Worker Joel Clermont built and shared for his own sites.
How It Works
The Worker serves the page and then, after the response has been sent, forwards the request to POST stats-hit: the URL, the method, the status and content type of the response, the IP, the user agent and a few request headers. SimpleStats decides whether it was a page view (no prefetch, no asset, no redirect, no 404, no bot) and records the visitor with its referrer and campaign parameters together with the view.
The Worker contains no tracking rules, so it never has to be updated when they change.
Setup
1. Route the pages through the Worker
run_worker_first sends every request through the Worker, except the asset folders. In wrangler.jsonc:
{
"name": "my-site",
"main": "worker/index.js",
"compatibility_date": "2026-10-01",
"assets": {
"directory": "./public",
"binding": "ASSETS",
"html_handling": "auto-trailing-slash",
"not_found_handling": "404-page",
// every request runs through the Worker, except the asset folders
"run_worker_first": ["/*", "!/css/*", "!/fonts/*", "!/images/*", "!/js/*", "!/favicon.ico"]
},
"vars": {
"SIMPLESTATS_API_URL": "https://simplestats.io/api/v1/",
"SIMPLESTATS_HOST": "example.com"
}
}Self-hosted? Set SIMPLESTATS_API_URL to the /api/v1/ URL of your installation. SIMPLESTATS_HOST is the hostname of your production site: requests for any other host (preview URLs) are not sent.
2. Store the API token as a secret
Create a project in SimpleStats and store its API token as a Worker secret:
npx wrangler secret put SIMPLESTATS_API_TOKENWithout the token the Worker sends nothing, which keeps local wrangler dev runs out of your stats.
3. Add the Worker
worker/index.js:
// Only these request headers are needed, never send cookies or credentials.
const HEADERS = ['referer', 'accept', 'sec-fetch-dest', 'sec-fetch-mode', 'sec-purpose', 'purpose', 'x-moz', 'x-requested-with'];
export default {
async fetch(request, env, ctx) {
const response = await env.ASSETS.fetch(request);
const url = new URL(request.url);
if (env.SIMPLESTATS_API_TOKEN && url.hostname === env.SIMPLESTATS_HOST) {
ctx.waitUntil(
sendHit(request, response, env).catch((error) => {
console.error('SimpleStats tracking failed', { error: String(error), path: url.pathname });
}),
);
}
return response;
},
};
async function sendHit(request, response, env) {
const headers = Object.fromEntries(
HEADERS.map((name) => [name, request.headers.get(name)]).filter(([, value]) => value !== null),
);
const answer = await fetch(new URL('stats-hit', env.SIMPLESTATS_API_URL), {
method: 'POST',
headers: {
Authorization: `Bearer ${env.SIMPLESTATS_API_TOKEN}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: request.url,
method: request.method,
status: response.status,
content_type: response.headers.get('Content-Type'),
ip: request.headers.get('CF-Connecting-IP'),
user_agent: request.headers.get('User-Agent'),
headers,
}),
signal: AbortSignal.timeout(5000),
});
if (!answer.ok) {
throw new Error(`SimpleStats answered ${answer.status}`);
}
}Deploy with npx wrangler deploy. Open a page of your site, and the visitor and the page view show up on the dashboard.
Good to Know
- Every forwarded request counts against the rate limit of your plan's page view bucket, also the ones that are no page view. Keep the asset folders out of
run_worker_firstso they never reach the Worker. - Only the first page view of a visitor per day counts as an event against your monthly limit. It also sets the referrer, the campaign and the entry page.
- A visitor is recognized by IP, user agent and the UTC day. A visitor who comes back after midnight UTC, or whose IP changes (WiFi to mobile), counts as a new visitor.
- Once the monthly event limit is reached, nothing is recorded until the next month.
- A reload is another page view, never another visitor.
- Failed calls are not retried. A call that fails or takes longer than 5 seconds is logged in the Worker logs and that view is lost. The visitor never waits for it.
- No registrations or payments. A static site has neither. If your signup and checkout run in a Laravel app, the beacon script attributes them to the visit.