Your own domain
Serve the script and send its requests through your own site, so a blocker that drops third-party trackers does not drop yours.
Some browsers and extensions block requests to addresses they know as analytics. A visit is then never counted, however little the script collects. Requests to your own domain are left alone, so the fix is to pass two paths of your site through to Snekot:
| On your site | Goes to |
|---|---|
/stats/snekot.js |
https://api.snekot.co/snekot.js |
/stats/v1/* |
https://api.snekot.co/v1/* |
/stats is an example; use any path a blocker would not guess.
Then load the script from your own path and tell it to send there too:
<script async src="/stats/snekot.js"></script>
<script>
window.snekotq = window.snekotq || [];
window.snekot = window.snekot || ((...args) => window.snekotq.push(args));
window.snekot('setConfig', 'YOUR_PUBLIC_KEY', 'your-project', { endpoint: '/stats' });
</script>
endpoint is where every request goes. Without it the script is served from your domain but still posts to Snekot‘s own, which is the part a blocker stops.
What your proxy must pass on
The request now reaches Snekot from your server, not from the visitor. Three things have to come with it:
- The visitor’s address. Visitors are told apart by a scramble of their address and browser, and the address is never stored. If every request carries your server’s address, everyone on the same browser counts as one visitor. Send the visitor’s address in
X-Snekot-IP.X-Forwarded-Foris read too, with the visitor first, but a network between your server and Snekot may rewrite that header, and it does not rewrite this one. - The visitor’s country, if you want countries: a two-letter code in
X-Country-Code. The country headers of Cloudflare and Vercel are read as they are. - The request as the browser sent it: the
Authorization,Content-Type,User-Agent,OriginandRefererheaders, and the body. A proxy passes these on unless told not to.Originmatters if the project limits its public keys to some sites.
Vercel and Netlify add the visitor’s address to what they pass on by themselves, in headers that are read here, so their snippets below need nothing more.
Next.js
In next.config.js, on any host:
module.exports = {
async rewrites() {
return [
{ source: '/stats/snekot.js', destination: 'https://api.snekot.co/snekot.js' },
{ source: '/stats/v1/:path*', destination: 'https://api.snekot.co/v1/:path*' },
];
},
};
Next sends the visitor’s address on in X-Forwarded-For. If Next itself runs behind a proxy of yours, that proxy has to hand Next the visitor’s address in the first place.
Vercel
For a site that is not Next.js, in vercel.json:
{
"rewrites": [
{ "source": "/stats/snekot.js", "destination": "https://api.snekot.co/snekot.js" },
{ "source": "/stats/v1/:path*", "destination": "https://api.snekot.co/v1/:path*" }
]
}
Netlify
In netlify.toml. The 200 makes each a proxy and not a redirect:
[[redirects]]
from = "/stats/snekot.js"
to = "https://api.snekot.co/snekot.js"
status = 200
force = true
[[redirects]]
from = "/stats/v1/*"
to = "https://api.snekot.co/v1/:splat"
status = 200
force = true
Cloudflare Workers
A Worker on a route such as example.com/stats/*:
const API = 'https://api.snekot.co';
export default {
async fetch(request) {
const url = new URL(request.url);
const path = url.pathname.replace(/^\/stats/, '');
if (path !== '/snekot.js' && !path.startsWith('/v1/')) return new Response('Not found', { status: 404 });
const headers = new Headers(request.headers);
headers.set('X-Snekot-IP', request.headers.get('CF-Connecting-IP') ?? '');
if (request.cf?.country) headers.set('X-Country-Code', request.cf.country);
headers.delete('Cookie');
return fetch(API + path + url.search, {
method: request.method,
headers,
body: request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body,
});
},
};
Caddy
example.com {
handle_path /stats/* {
reverse_proxy https://api.snekot.co {
header_up Host {upstream_hostport}
header_up X-Snekot-IP {remote_host}
header_up -Cookie
}
}
}
handle_path takes /stats off the front, so /stats/v1/log arrives as /v1/log.
Check that it works
Open your site, then look at the request to /stats/v1/page in the browser’s network panel. It should be answered 200 by your own domain.
Then open the site from two different networks, such as a laptop and a phone on mobile data. Views should count two visitors. If it counts one, the visitor’s address is not getting through; if countries are missing, neither is the country.
https://api.snekot.co/v1/me, opened through your proxy as /stats/v1/me, shows the country, browser and device a page view from you would be recorded with, and stores nothing.
What does not change
Your public key still names the project, and the same limits apply to it. Cookies are not used, so there is nothing for your proxy to pass on there; the snippets above remove them so none of your site’s own cookies leave it.
The React and Vue helpers take the same address: <EventsProvider endpoint="/stats" …> loads the script from there and sends there.