First-party proxy
Content blockers block well-known analytics addresses. Serving the tracker and its data through your own domain keeps your numbers complete. Proxy two paths to our collector: /s/ (the script) and /e, /r, /snap, /sv, /m/ (data and modules). Then use your own address in the script tag.
Next.js (next.config.js):
async rewrites() {
return [
{ source: '/stats/:path*', destination: 'https://COLLECTOR/:path*' },
]
}<script defer src="/stats/s/ans_YOUR_KEY.js" data-origin="/stats"></script>nginx:
location /stats/ {
proxy_pass https://COLLECTOR/;
proxy_set_header Host COLLECTOR;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}Cloudflare Worker:
export default {
async fetch(req) {
const url = new URL(req.url)
if (!url.pathname.startsWith('/stats/')) return fetch(req)
const to = new URL(url.pathname.slice(6) + url.search, 'https://COLLECTOR')
const fwd = new Request(to, req)
fwd.headers.set('X-Forwarded-For', req.headers.get('CF-Connecting-IP') ?? '')
return fetch(fwd)
},
}Always pass the visitor's address in X-Forwarded-For, and switch on Settings → Site → Behind my own proxy so the collector reads it: without it every visit looks like your server. data-origin on the script tag sends the data back through the same path.