Docsllms.txtSign inGo to your sites

Updated

View as Markdown

Proxy 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.

Prompt for your coding agent · holds no key
Serve the twelvebit script for <production domain> through this project's own domain, so that ad blockers block it less. 1. Detect how the site is served and add two rewrites (proxy, not redirect): /tb/script.js → https://twelvebit.app/js/script.js /tb/e → https://twelvebit.app/api/e Next.js: rewrites() in next.config. Vercel: vercel.json. Netlify: _redirects with status 200. Cloudflare: a Worker route. Other servers: a reverse proxy rule. 2. Forward the User-Agent and the visitor IP (X-Forwarded-For) when the platform lets you. 3. Change the twelvebit script tag (data-website-id="<site id>") to src="/tb/script.js" and add data-api="/tb/e". Keep the other attributes. 4. Check it: start the app, confirm that /tb/script.js returns JavaScript and that a page view sends POST /tb/e with a 202. Then run npx -y twelvebit@latest status to see it arrive. Reference: https://twelvebit.app/docs/proxy.md. At the end, show me the diff. Fill in <production domain> and <site id> from this project: the site id is data-website-id in the twelvebit script tag, or TWELVEBIT_SITE_ID in .env. Ask me if you cannot find them.

Do it yourself

You need two routes on your domain:

Your pathGoes to
/tb/script.jshttps://twelvebit.app/js/script.js
/tb/ehttps://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.

HTML
<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
// 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)

JSON
{
  "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

Text
# public/_redirects (or the publish folder)
/tb/script.js  https://twelvebit.app/js/script.js  200
/tb/e          https://twelvebit.app/api/e  200

Status 200 makes Netlify proxy the request instead of redirecting it.

Cloudflare Workers

Add a route for example.com/tb/* to this Worker:

worker.js
// 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

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:

TypeScript
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-IP wins.
  • Accept-Language: while attack mode is on, twelvebit drops hits that have neither Accept-Language nor Sec-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_vid and _tb_sid on 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. Set data-api only 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

  1. Open https://example.com/tb/script.js in the browser. It must return JavaScript that starts with /*! twelvebit analytics tracker.
  2. Open your site, then the Network tab. A page view sends POST /tb/e, and the answer is 202 with an empty body.
  3. A 202 with text means that the server dropped the hit, and the text says why. See Troubleshooting.
  4. Run the CLI status command and look for your production host in the hosts.
  5. Open the Live view and check that your visit shows your city.

Common mistakes

MistakeWhat you seeFix
No data-api on the script tagPOST /api/e on your own site answers 404, or your own route answersAdd data-api="/tb/e".
A redirect instead of a rewrite/tb/script.js answers 301 or 302, and the ad blocker still blocks the requestUse a rewrite or proxy rule (Netlify: status 200).
The proxy does not pass on the bodyPOST /tb/e answers 400 bad json or bad payloadPass the request body as it is.
No User-Agent passed on202 with the text botForward the visitor's User-Agent.
No Accept-Language passed on202 with attack mode: no_browser while attack mode is onForward Accept-Language.
No visitor IP passed onEvery visitor in one city; IP exclusions do not matchForward 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.