Docs · Guide

First-party proxy

A first-party proxy routes the Statlark script and its event beacons through your own domain, so they’re same-origin instead of cross-origin. That dodges host-based ad-blockers and keeps visitor geography working — with no change to the tracker or the collector. It’s an optional upgrade over the default install.

Using an AI coding tool?Hand it the prompt below, or point it at this page as markdown: first-party-proxy.md

The default: a zero-config hosted collector

The one-line snippet already works out of the box. The script loads from statlark.com and events post to Statlark’s hosted collector, which stamps each visit’s country, region and city from the edge — so you get geography with zero setup.

<script defer src="https://statlark.com/script.js" data-website-id="sl_xxxx"></script>

The only downside is that both requests go to a Statlark host, which browser extensions that block by hostname can recognise and drop. If that matters for your audience, proxy through your own domain instead.

How the proxy works

You expose two paths under a segment on your own domain — say /q3v/ — and point the snippet at them:

  • GET /q3v/s.js — rewrites to https://statlark.com/script.js (the unchanged tracker).
  • POST /q3v/e — forwards the event beacon to the collector. It forwards only the geo and user-agent headers, and never the Cookie or Authorization header — going same-origin means the beacon now carries your site’s first-party cookies, and those must not reach the collector.

Then the snippet becomes relative, with a data-api override:

<script defer src="/q3v/s.js" data-website-id="sl_xxxx" data-api="/q3v/e"></script>

Set it up

You don’t have to hand-write any of this. The recipe below adds the rewrite, the forwarding route and the swapped snippet. Follow its steps by hand, or paste it into your AI coding tool. In Settings → Install, the First-party upgrade panel shows the same recipe with your site ID and proxy path filled in. If your site loads the consent banner, the recipe proxies that too.

The recipe is written for the Next.js App Router on Vercel. On another stack the same paths work, but you set them up yourself, as How the proxy works describes.

If your app has auth middleware, exclude the proxy segment from its matcher so the script and beacon aren’t auth-gated or handed a Set-Cookie on every hit.

Hand this to your AI

Copy this from Settings → Install → First-party upgrade, where your site ID and proxy path are filled in. Or replace YOUR_SITE_ID and YOUR_PROXY_PATH yourself: Settings shows the path. It never needs your API key.

Add a first-party Statlark analytics proxy to this project for my site. It routes the tracker script and its event beacons through the site's own domain, so they're same-origin: that dodges host-based ad-blockers and keeps visitor geography working. The Statlark tracker and collector need NO changes.

Before you start:
- This recipe is for the Next.js App Router on Vercel. If the project is anything else, stop and ask me.
- The site ID below is YOUR_SITE_ID and the path segment is YOUR_PROXY_PATH. If either is still a placeholder, ask me for it: Statlark shows both under Settings, Install, First-party upgrade.
- The site must already load Statlark with a script.js or consent.js tag, as a plain <script> or a next/script <Script>. If it doesn't, or it starts Statlark with @statlark/web or Google Tag Manager, stop and tell me.
- If the tag has data-cookieless="true" or data-on-reject="cookieless", stop and tell me: cookieless mode can't run behind a proxy.
- If the site loads consent.js, you MUST do the consent steps below instead of the plain snippet in step 4, or the tracker would load without asking. If it loads the plain tag and the site has visitors in the EU or UK, ask me first: Statlark sets first-party analytics cookies, and the tag may need to load through the consent banner (https://statlark.com/docs/consent).

Use the path segment "YOUR_PROXY_PATH" exactly. Do NOT prefix the folder with "_": an _-prefixed App Router folder is a private folder and won't route. Put the new route files in the app folder the project already uses (app/ or src/app/). If next.config turns on cacheComponents, leave out the export const dynamic lines below: Next.js doesn't allow them there.

1) next.config — add a rewrite for the script (GET). If it already has rewrites, add this entry to them:
   async rewrites() {
     return [{ source: '/YOUR_PROXY_PATH/s.js', destination: 'https://statlark.com/script.js' }];
   }

2) app/YOUR_PROXY_PATH/e/route.ts — events (POST), NEW file. Build the upstream headers
   FROM SCRATCH and forward ONLY geo + user-agent. NEVER forward Cookie or
   Authorization: going same-origin means the beacon now carries this site's
   first-party cookies, and they must not reach the collector.

   export const dynamic = 'force-dynamic';
   export async function POST(request: Request) {
     const body = await request.text();
     const headers = new Headers({ 'content-type': 'text/plain' });
     for (const h of ['x-vercel-ip-country','x-vercel-ip-country-region','x-vercel-ip-city','user-agent']) {
       const v = request.headers.get(h); if (v) headers.set(h, v);
     }
     try {
       const upstream = await fetch('https://sxqkwvtahauliimhxwil.supabase.co/functions/v1/collect', { method: 'POST', headers, body, signal: AbortSignal.timeout(5000) });
       return new Response(await upstream.text().catch(() => ''), {
         status: upstream.status,
         headers: { 'content-type': upstream.headers.get('content-type') ?? 'application/json' },
       });
     } catch {
       return new Response('', { status: 502 });
     }
   }

3) proxy.ts or middleware.ts — Next.js 16 renamed middleware.ts to proxy.ts, and that file
   has nothing to do with this analytics proxy. If the site has auth middleware in either,
   exclude the /YOUR_PROXY_PATH/ prefix from its matcher so the beacon and the script GET
   aren't auth-gated or handed a Set-Cookie on every hit. For a negative-lookahead
   matcher, add the "YOUR_PROXY_PATH/" token, e.g.:
     matcher: ['/((?!_next/static|_next/image|favicon.ico|YOUR_PROXY_PATH/).*)']

4) Finally (last, atomic), swap the Statlark snippet to the first-party paths. Keep the form the site
   already uses (a next/script <Script> stays a <Script>): change src and add data-api, nothing else:
   <script defer src="/YOUR_PROXY_PATH/s.js" data-website-id="YOUR_SITE_ID" data-api="/YOUR_PROXY_PATH/e"></script>

If the site loads consent.js, keep the banner first-party too. Do this instead of the plain snippet in step 4:
  a) In step 1's rewrites, add a second entry:
       { source: '/YOUR_PROXY_PATH/c.js', destination: 'https://statlark.com/consent.js' }
  b) app/YOUR_PROXY_PATH/geo/route.ts — GET, NEW file. Return the VISITOR's country from
     THIS request's inbound x-vercel-ip-country header. Do NOT fetch the collector's
     /geo — that would read this function's datacenter IP, not the visitor's.
       export const dynamic = 'force-dynamic';
       export function GET(request: Request) {
         // Absent in local dev / off-Vercel → ''. The consent controller treats an
         // empty or unknown country as "ask", so it shows the banner (fails closed),
         // never silently tracks.
         const country = request.headers.get('x-vercel-ip-country') ?? '';
         return new Response(JSON.stringify({ country }), {
           headers: { 'content-type': 'application/json', 'cache-control': 'no-store' },
         });
       }
  c) At step 4, load the consent controller INSTEAD of the tracker (it injects
     /YOUR_PROXY_PATH/s.js after the visitor accepts). Edit the current consent.js tag: change
     its src to /YOUR_PROXY_PATH/c.js and add data-tracker-src, data-api and data-geo. Keep its
     other options, such as data-policy, as they are. A tag with the default policy ends up as:
       <script defer src="/YOUR_PROXY_PATH/c.js" data-website-id="YOUR_SITE_ID" data-policy="eu" data-tracker-src="/YOUR_PROXY_PATH/s.js" data-api="/YOUR_PROXY_PATH/e" data-geo="/YOUR_PROXY_PATH/geo"></script>
     Don't also add the plain tracker snippet.
  The step-3 matcher already excludes the whole "/YOUR_PROXY_PATH/" prefix, so c.js and geo
  need no middleware change.

To check it once it's deployed: on the live site, the browser's Network tab shows
/YOUR_PROXY_PATH/s.js loading the tracker and the page's beacon POST to /YOUR_PROXY_PATH/e
returning 202, and the /YOUR_PROXY_PATH/ path isn't auth-gated. If you did the consent steps,
/YOUR_PROXY_PATH/c.js returns the controller JS and /YOUR_PROXY_PATH/geo returns {"country":"…"}.
Don't send hand-made events to /YOUR_PROXY_PATH/e to test it. Roll back by reverting the snippet
to the direct https://statlark.com/script.js form (no data-api).
Don't add SRI or change anything else. Finish by telling me what you changed, and remind me to deploy, run those checks, then confirm it in Statlark under Settings, Install, Verification.

Running a consent banner on a proxied site? Proxy the controller too, so it — and its region check — stay same-origin. Alongside s.js and e, add two more paths:

  • GET /q3v/c.js — a rewrite to https://statlark.com/consent.js (the consent controller).
  • GET /q3v/geo — a small route that returns {"country":"XX"} read from the request’s own x-vercel-ip-country edge header (no-store). Read the inbound header — don’t fetch the collector’s /geo, which would resolve your function’s datacenter, not the visitor. Without this route the controller calls collect.statlark.com directly — a third-party request that defeats the proxy.

Then load the controller (not the tracker) from your domain — it injects /q3v/s.js after the visitor accepts, forwarding data-api to it:

<script defer src="/q3v/c.js" data-website-id="sl_xxxx"
        data-policy="eu" data-tracker-src="/q3v/s.js" data-api="/q3v/e"
        data-geo="/q3v/geo"></script>

data-on-reject="cookieless" can’t be used behind a proxy — like cookieless mode, it needs the hosted collector to derive its rotating id. Keep the default on a proxied install.

Verify & roll back

A working proxy means GET /q3v/s.js (or your derived segment) returns the tracker JavaScript and POST /q3v/e returns 202. The Install screen’s verification check confirms it end-to-end. To roll back, revert the snippet to the direct https://statlark.com/script.js form (drop data-api) — nothing else changes.

One caveat: cookieless mode can’t be combined with a first-party proxy — cookieless needs the hosted collector to derive its rotating visitor id. Pick one.

Last reviewed