Updated
View as MarkdownProxy the script through your domain
Some ad blockers block analytics scripts from other domains. When you serve the script and the event endpoint from your own domain, more visits are counted.
Ask your coding agent
Paste this into Claude Code, Codex or Cursor in your project folder. It holds no key: the agent gets one with the CLI, and you approve the sign-in in the browser. To do it yourself, follow the steps after it.
Do it yourself
You need two routes on your domain:
| Your path | Goes to |
|---|---|
/tb/script.js | https://twelvebit.app/js/script.js |
/tb/e | https://twelvebit.app/api/e |
Then change the script tag. data-api is required: without it, the script sends events to /api/e on the script's origin, which is now your site.
<script defer data-website-id="tb_YOUR_SITE_ID" data-domain="example.com" data-api="/tb/e" src="/tb/script.js"></script>Pick neutral paths. Paths with "analytics" or "tracking" in them are more likely to be blocked.
Both routes must proxy the request, not redirect it. A redirect sends the browser to https://twelvebit.app again, and the ad blocker can block it there.
/tb/e must pass on the POST body as it is (JSON as text, at most 16 KB) and the headers in Visitor location behind a proxy. The page and the route are on the same origin, so the browser sends no CORS preflight.
Next.js
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
async rewrites() {
return [
{ source: "/tb/script.js", destination: "https://twelvebit.app/js/script.js" },
{ source: "/tb/e", destination: "https://twelvebit.app/api/e" },
];
},
};
export default nextConfig;With next/script, use src="/tb/script.js" and add data-api="/tb/e".
Vercel (any framework)
{
"rewrites": [
{ "source": "/tb/script.js", "destination": "https://twelvebit.app/js/script.js" },
{ "source": "/tb/e", "destination": "https://twelvebit.app/api/e" }
]
}Put this in vercel.json in the project root.
Netlify
# public/_redirects (or the publish folder)
/tb/script.js https://twelvebit.app/js/script.js 200
/tb/e https://twelvebit.app/api/e 200Status 200 makes Netlify proxy the request instead of redirecting it.
Cloudflare Workers
Add a route for example.com/tb/* to this Worker:
// worker.js
const UPSTREAM = { "/tb/script.js": "https://twelvebit.app/js/script.js", "/tb/e": "https://twelvebit.app/api/e" };
export default {
async fetch(request) {
const target = UPSTREAM[new URL(request.url).pathname];
if (!target) return fetch(request);
const headers = new Headers();
headers.set("User-Agent", request.headers.get("User-Agent") ?? "");
headers.set("Content-Type", request.headers.get("Content-Type") ?? "text/plain");
const lang = request.headers.get("Accept-Language");
if (lang) headers.set("Accept-Language", lang);
const ip = request.headers.get("CF-Connecting-IP");
if (ip) headers.set("X-Forwarded-For", ip);
return fetch(target, {
method: request.method,
headers,
body: request.method === "POST" ? await request.text() : undefined,
});
},
};nginx
location = /tb/script.js {
proxy_pass https://twelvebit.app/js/script.js;
proxy_ssl_server_name on;
}
location = /tb/e {
proxy_pass https://twelvebit.app/api/e;
proxy_ssl_server_name on;
proxy_set_header X-Forwarded-For $remote_addr;
}proxy_ssl_server_name on sends the host name in the TLS handshake. Without it, the HTTPS connection to https://twelvebit.app can fail. nginx passes on the other request headers (such as User-Agent and Accept-Language) by default.
Web SDK
With @twelvebit/web, there is no script to proxy. Set apiUrl to the base of your proxy. The SDK posts to {apiUrl}/api/e:
import { initTwelvebit } from "@twelvebit/web";
initTwelvebit({ siteId: "tb_YOUR_SITE_ID", domain: "example.com", apiUrl: "/tb" });Then proxy /tb/api/e to https://twelvebit.app/api/e.
Visitor location behind a proxy
Forward these headers when your proxy lets you:
- User-Agent: always. twelvebit uses it for the browser, the device and bot filtering. A request with no user agent is dropped as a bot.
- X-Forwarded-For (or X-Real-IP) with the visitor's IP address. twelvebit uses the first address for IP exclusions and for the cookieless daily hash. When both arrive,
X-Real-IPwins. - Accept-Language: while attack mode is on, twelvebit drops hits that have neither
Accept-LanguagenorSec-Fetch-Mode, because every current browser sends one of them.
Country, region and city come from the location headers that the network edge in front of twelvebit adds, based on the address that connects to it. Behind a proxy, that address can be your proxy server, and some edges replace forwarded IP headers with the connecting address. Then every visitor shows the location of your proxy, IP exclusions do not match, and in cookieless mode many visitors can merge into one.
Check after you set up the proxy: open the Live view and look at your own visit. If it shows your city, the proxy forwards enough. If every visit shows the same city, load the script directly from https://twelvebit.app (location works there) or accept less precise location data.
What else changes behind a proxy
- Cookies do not change. The script sets
_tb_vidand_tb_sidon your domain in both setups, so revenue attribution works the same. - The cloud-network check is off. With
data-api, each event carries a mark that says the address is the proxy's. twelvebit then does not check the address against the cloud network ranges, so your proxy's server does not make every visit a bot. Bots with a known bot user agent are still dropped. Setdata-apionly when you proxy. - The SDKs do not send this mark. When SDK events go through a proxy, forward the visitor's IP address. If the visits show only on the bot card under Cloud networks, turn on Count cloud-network visits in Settings → Bot tracking.
- Attack mode limits by address. While attack mode is on, one address can send at most 30 hits a minute. If your proxy does not forward the visitor's address, all visitors share the proxy's address and reach that limit together.
Check that it works
- Open
https://example.com/tb/script.jsin the browser. It must return JavaScript that starts with/*! twelvebit analytics tracker. - Open your site, then the Network tab. A page view sends
POST /tb/e, and the answer is202with an empty body. - A
202with text means that the server dropped the hit, and the text says why. See Troubleshooting. - Run the CLI
statuscommand and look for your production host in the hosts. - Open the Live view and check that your visit shows your city.
Common mistakes
| Mistake | What you see | Fix |
|---|---|---|
No data-api on the script tag | POST /api/e on your own site answers 404, or your own route answers | Add data-api="/tb/e". |
| A redirect instead of a rewrite | /tb/script.js answers 301 or 302, and the ad blocker still blocks the request | Use a rewrite or proxy rule (Netlify: status 200). |
| The proxy does not pass on the body | POST /tb/e answers 400 bad json or bad payload | Pass the request body as it is. |
No User-Agent passed on | 202 with the text bot | Forward the visitor's User-Agent. |
No Accept-Language passed on | 202 with attack mode: no_browser while attack mode is on | Forward Accept-Language. |
| No visitor IP passed on | Every visitor in one city; IP exclusions do not match | Forward X-Forwarded-For, or load the script directly. |
Content Security Policy
With a proxy, the script and the requests are same-origin, so script-src 'self' and connect-src 'self' are enough. Without a proxy, add https://twelvebit.app to both.