Serve the tracker from your own domain
Some ad blockers drop requests to analytics hosts. Serving the script and the event endpoint from your own domain, on paths that do not say “analytics” or “track”, avoids that:
<script defer src="/a/s.js" data-key="wk_…" data-api="/a/e"></script>The forwarding contract
Section titled “The forwarding contract”A proxy hides your visitors from Millimetry: every event would seem to come from your server, and all your visitors would merge into one. So your proxy passes on who the visitor is, and proves it is yours with your site’s forwarding secret (fs_…). Find it under the site’s settings → Forwarding secret; it is shown once.
| Header | Value |
|---|---|
X-Forwarding-Secret |
Your site’s fs_… secret |
X-Client-IP |
The visitor’s IP address |
X-Client-Country |
Optional: the visitor’s ISO country code, if your proxy knows it |
X-Client-Region |
Optional: the visitor’s region code |
Pass the visitor’s Origin, User-Agent and Content-Type headers through unchanged. Without a valid secret Millimetry ignores the other headers, so nobody else can claim to speak for your visitors. Millimetry never stores the IP address. It hashes it with a value that changes every day, and uses it to look up the country when your proxy does not send one.
Keep the secret out of your repository: in a Worker secret, an environment variable, or a file only the server can read. If it leaks, rotate it in the site’s settings. The old one stops working at once.
Cloudflare Worker
Section titled “Cloudflare Worker”If your site is on Cloudflare, a Worker on the route example.com/a/* does all of this, including the visitor’s country and region. Route it on the whole site (example.com/*) and set MILLIMETRY_SITE_KEY, and it also reports the AI agents that request your pages, including the ones that never run JavaScript.
// A Cloudflare Worker that serves the Millimetry tracker from a site's own domain (spec §12.2).// Route it on the site's zone as `example.com/a/*`; the page then loads// <script defer src="/a/s.js" data-key="wk_…" data-api="/a/e"></script>// and ad blockers see a first-party script posting to a first-party path.//// Because the request now reaches Millimetry from Cloudflare rather than from the visitor, the// Worker forwards who the visitor is: their address (CF-Connecting-IP) and location// (request.cf.country / regionCode), plus the site's forwarding secret, which proves the headers// come from the site's own proxy. Without the secret Millimetry ignores them.//// AI agents (spec §9.1): route the Worker on the whole site (`example.com/*`) and set// MILLIMETRY_SITE_KEY, and it also reports every request an AI agent makes (ChatGPT fetching a page// for someone, GPTBot crawling, a signed agent, a headless browser), including agents that never run// JavaScript and so never load the tracker. The page is served exactly as before; the report goes// out in the background, with the page's host and path only: no query string, no address, no// cookie. Millimetry decides what is an agent; ordinary visitors and search engines are never sent.//// Set the secret with `wrangler secret put MILLIMETRY_FORWARDING_SECRET` (never in source or// wrangler.jsonc), and run `wrangler types` in your project for the real Env type.
export interface Env { /** The site's forwarding secret (`fs_…`), from the Millimetry dashboard. */ MILLIMETRY_FORWARDING_SECRET: string; /** The ingest host; defaults to Millimetry's. */ MILLIMETRY_HOST?: string; /** The site's key (`wk_…`, public). Set it to report AI agents' requests. */ MILLIMETRY_SITE_KEY?: string; /** * `all` (the default) reports every AI agent; `people` only those acting for a person (crawlers * are many, and every stored request counts toward the plan); `off` reports none. */ MILLIMETRY_AGENTS?: string;}
/** What the Worker needs from its execution context. */export interface WaitUntil { waitUntil(promise: Promise<unknown>): void;}
/** The fields Cloudflare attaches to every request. */interface RequestCf { country?: string; regionCode?: string; /** Set when Cloudflare verified the bot (on zones whose plan exposes it). */ verifiedBotCategory?: string;}
/** * A cheap first look at whether a request may be an AI agent: the tokens of the agents Millimetry * knows (`server/crates/core/src/agents.rs`; a test keeps the two in step), a headless browser or a * Web Bot Auth signature. Millimetry makes the real call. Only page loads are reported: an agent * in a browser also fetches every script, stylesheet and image, and each would be a stored event. */const AGENT_TOKENS = /chatgpt-user|claude-user|claude-code|perplexity-user|mistralai-user|meta-externalfetcher|google-agent|google-notebooklm|firecrawlagent|oai-searchbot|claude-searchbot|perplexitybot|duckassistbot|google-cloudvertexbot|gptbot|claudebot|anthropic-ai|meta-externalagent|ccbot|bytespider|amazonbot|cohere-ai|cohere-training-data-crawler|diffbot|ai2bot|headlesschrome/i;
const ASSET = /\.(?:js|mjs|css|map|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|mp4|webm|mp3|wasm)$/i;
/** A page load: a GET or POST for a document, not a script, a stylesheet, an image or a font. */export function isPageLoad(request: Request): boolean { if (request.method !== "GET" && request.method !== "POST") return false; // Browsers say what they fetch; crawlers usually do not, and then the path decides. const dest = request.headers.get("sec-fetch-dest"); if (dest !== null && dest !== "document" && dest !== "iframe") return false; return !ASSET.test(new URL(request.url).pathname);}
export function mayBeAgent(request: Request): boolean { return ( isPageLoad(request) && (AGENT_TOKENS.test(request.headers.get("user-agent") ?? "") || request.headers.has("signature-agent")) );}
/** Tells Millimetry about one agent request; never throws, never retries. */async function reportAgent( request: Request, status: number, ms: number, env: Env, host: string,): Promise<void> { const url = new URL(request.url); const cf = (request as Request & { cf?: RequestCf }).cf; const body = JSON.stringify({ k: env.MILLIMETRY_SITE_KEY, skip: env.MILLIMETRY_AGENTS === "people" ? ["crawler"] : [], events: [ { via: "edge", ua: (request.headers.get("user-agent") ?? "").slice(0, 512), signature_agent: request.headers.get("signature-agent")?.slice(0, 512) ?? undefined, verified: Boolean(cf?.verifiedBotCategory), url: `${url.origin}${url.pathname}`, status, ms, }, ], }); try { const answer = await fetch(`${host}/v1/agent/events`, { method: "POST", headers: { "content-type": "application/json", "x-forwarding-secret": env.MILLIMETRY_FORWARDING_SECRET, }, body, }); await answer.body?.cancel(); } catch { // Analytics must never break the site: a lost report is acceptable. }}
/** A tracker event is a few hundred bytes; anything far larger is refused here, not buffered. */const MAX_EVENT_BYTES = 32 * 1024;const PASSED_THROUGH = ["content-type", "origin", "user-agent"];
async function forwardEvent(request: Request, env: Env, host: string): Promise<Response> { const length = Number(request.headers.get("content-length") ?? Number.NaN); if (!Number.isFinite(length) || length > MAX_EVENT_BYTES) { return new Response(null, { status: 413 }); } const body = await request.arrayBuffer(); if (body.byteLength > MAX_EVENT_BYTES) { return new Response(null, { status: 413 }); }
const headers = new Headers(); for (const name of PASSED_THROUGH) { const value = request.headers.get(name); if (value) headers.set(name, value); } const cf = (request as Request & { cf?: RequestCf }).cf; headers.set("x-client-ip", request.headers.get("cf-connecting-ip") ?? ""); if (cf?.country) headers.set("x-client-country", cf.country); if (cf?.regionCode) headers.set("x-client-region", cf.regionCode); headers.set("x-forwarding-secret", env.MILLIMETRY_FORWARDING_SECRET);
const upstream = await fetch(`${host}/v1/web/event`, { method: "POST", headers, body }); // Hand back only what the browser needs: the status and the CORS answer. const reply = new Headers({ Vary: "Origin" }); const allowOrigin = upstream.headers.get("access-control-allow-origin"); if (allowOrigin) reply.set("access-control-allow-origin", allowOrigin); return new Response(null, { status: upstream.status, headers: reply });}
export async function handle(request: Request, env: Env, ctx?: WaitUntil): Promise<Response> { const host = (env.MILLIMETRY_HOST ?? "https://e.millimetry.app").replace(/\/$/, ""); const { pathname } = new URL(request.url); if (pathname === "/a/s.js" && request.method === "GET") { // The script is public and cached for an hour at Millimetry's edge and here. return fetch(`${host}/s.js`); } if (pathname === "/a/e" && request.method === "POST") { try { return await forwardEvent(request, env, host); } catch { // The tracker never retries; answer plainly and keep the page's console quiet. return new Response(null, { status: 502 }); } } // Anything else on the route belongs to the site, streamed through untouched. const reporting = ctx !== undefined && Boolean(env.MILLIMETRY_SITE_KEY) && env.MILLIMETRY_AGENTS !== "off" && mayBeAgent(request); if (!reporting) return fetch(request); const started = Date.now(); const response = await fetch(request); ctx.waitUntil(reportAgent(request, response.status, Date.now() - started, env, host)); return response;}
export default { fetch: handle,};// An example wrangler.jsonc for the proxy Worker in cloudflare-worker.ts. Copy both into a Worker// project on the site's Cloudflare account, adjust the route, then:// wrangler secret put MILLIMETRY_FORWARDING_SECRET # the fs_… secret from the dashboard// wrangler types # generates the real Env type// wrangler deploy{ "$schema": "node_modules/wrangler/config-schema.json", "name": "millimetry-proxy", "main": "cloudflare-worker.ts", "compatibility_date": "2026-10-08", "compatibility_flags": ["nodejs_compat"], // The tracker's paths only. To also report AI agents, route the whole site instead // ("example.com/*") and set MILLIMETRY_SITE_KEY below; every request then runs the Worker, which // counts toward Workers' request pricing, and pages are passed through untouched. "routes": [{ "pattern": "example.com/a/*", "zone_name": "example.com" }], "observability": { "enabled": true, "head_sampling_rate": 1, "traces": { "enabled": true } }, "vars": { "MILLIMETRY_HOST": "https://e.millimetry.app", // The site's public key (wk_…). Leave it empty to report no agents. "MILLIMETRY_SITE_KEY": "", // all: every AI agent; people: only agents acting for a person (skip crawlers); off: none. "MILLIMETRY_AGENTS": "all" }}wrangler secret put MILLIMETRY_FORWARDING_SECRET # paste the fs_… secretwrangler typeswrangler deploylocation = /a/e { proxy_pass https://e.millimetry.app/v1/web/event; proxy_ssl_server_name on; proxy_set_header Host e.millimetry.app; proxy_set_header X-Client-IP $remote_addr; include /etc/nginx/millimetry-secret.conf; # proxy_set_header X-Forwarding-Secret "fs_…";}location = /a/s.js { proxy_pass https://e.millimetry.app/s.js; proxy_ssl_server_name on; proxy_set_header Host e.millimetry.app;}If nginx itself sits behind a load balancer or CDN, make sure $remote_addr is the visitor, not the load balancer. The realip module does this.
handle /a/e { rewrite * /v1/web/event reverse_proxy https://e.millimetry.app { header_up Host e.millimetry.app header_up X-Client-IP {remote_host} header_up X-Forwarding-Secret {env.MILLIMETRY_FORWARDING_SECRET} }}handle /a/s.js { rewrite * /s.js reverse_proxy https://e.millimetry.app { header_up Host e.millimetry.app }}Checking it works
Section titled “Checking it works”Open your site in a private window, then the dashboard’s live view: the visit should appear within seconds with your country. If every visit shows the same country, or visits merge into one, the proxy is not sending X-Client-IP or the secret is wrong.