Proxying the script
Serve the tracker from your own domain for complete, first-party data.
Some ad blockers block third-party analytics domains. Serving the script and the ingest endpoint from your own domain makes Stancelens first-party, so you get complete numbers.
What to forward
| Your path | Stancelens | Needed for |
|---|---|---|
/stats/js/e.js | https://stancelens.com/e.js | Always |
/stats/js/e-*.js | https://stancelens.com/e-*.js | Modules and flags. They load from the same folder as e.js. |
/stats/api/event | https://stancelens.com/api/event | Always |
/stats/api/flags | https://stancelens.com/api/flags | stancelens.flag() |
/stats/api/replay | https://stancelens.com/api/replay | data-replay |
Then point the script at your paths. Because data-api ends in /event, the flags and replay modules use /stats/api/flags and /stats/api/replay next to it:
html
<script defer data-site="SITE_ID" data-api="/stats/api/event" src="/stats/js/e.js"></script>Next.js
next.config.ts
const nextConfig = {
async rewrites() {
return [
{ source: "/stats/js/:file", destination: "https://stancelens.com/:file" },
{ source: "/stats/api/:endpoint(event|flags|replay)", destination: "https://stancelens.com/api/:endpoint" },
];
},
};
export default nextConfig;Vercel (any framework)
vercel.json
{
"rewrites": [
{ "source": "/stats/js/:file", "destination": "https://stancelens.com/:file" },
{ "source": "/stats/api/:endpoint(event|flags|replay)", "destination": "https://stancelens.com/api/:endpoint" }
]
}Netlify
_redirects
/stats/js/* https://stancelens.com/:splat 200
/stats/api/event https://stancelens.com/api/event 200
/stats/api/flags https://stancelens.com/api/flags 200
/stats/api/replay https://stancelens.com/api/replay 200Cloudflare Workers
js
export default {
async fetch(request) {
const url = new URL(request.url);
const js = url.pathname.match(/^\/stats\/js\/(e[\w.-]*\.js)$/);
if (js) return fetch(`https://stancelens.com/${js[1]}`, { cf: { cacheTtl: 3600 } });
const api = url.pathname.match(/^\/stats\/api\/(event|flags|replay)$/);
if (api) {
const upstream = new Request(`https://stancelens.com/api/${api[1]}${url.search}`, request);
upstream.headers.set("X-Forwarded-For", request.headers.get("CF-Connecting-IP") ?? "");
return fetch(upstream);
}
return fetch(request);
},
};Nginx
nginx
location ~ ^/stats/js/(e[\w.-]*\.js)$ {
proxy_pass https://stancelens.com/$1;
proxy_set_header Host stancelens.com;
proxy_ssl_server_name on;
}
location ~ ^/stats/api/(event|flags|replay)$ {
proxy_pass https://stancelens.com/api/$1$is_args$args;
proxy_set_header Host stancelens.com;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}Forward the visitor's IP in X-Forwarded-For. Without it every visitor appears to come from your server, so the daily visitor hash collapses many people into one. The IP is never stored.