The visitor's IP address

Docs·Proxy guide

Proxy with SvelteKit

One +server.ts route serves Spreelytics from /sp/. This guide uses /sp/. Any path works if nothing else on your site uses it and it doesn't contain words like "analytics" or "tracking", which blockers look for.

Add the server route

Create src/routes/sp/[...path]/+server.ts:

// src/routes/sp/[...path]/+server.ts
// Serves Spreelytics from /sp/ on your site: /sp/js/ is the script, everything else is tracking.
import type { RequestHandler } from "./$types";

const SCRIPTS = "https://cdn.spreelytics.com/cdn/sdk/";
const API = "https://sdk.spreelytics.com/1.0/";
const FORWARD = ["content-type", "user-agent", "origin", "referer", "accept", "accept-language"];

const proxy: RequestHandler = async ({ request, params, url, getClientAddress }) => {
  const path = params.path;
  const target = (path.startsWith("js/") ? SCRIPTS + path.slice(3) : API + path) + url.search;

  const headers = new Headers();
  for (const name of FORWARD) {
    const value = request.headers.get(name);
    if (value) headers.set(name, value);
  }
  // The visitor's IP address.
  headers.set("x-forwarded-for", getClientAddress());

  const upstream = await fetch(target, {
    method: request.method,
    headers,
    body: request.method === "GET" || request.method === "HEAD" ? undefined : await request.arrayBuffer(),
  });

  // fetch has already unzipped the body, so these headers no longer apply.
  const responseHeaders = new Headers(upstream.headers);
  responseHeaders.delete("content-encoding");
  responseHeaders.delete("content-length");
  return new Response(upstream.body, { status: upstream.status, headers: responseHeaders });
};

export { proxy as GET, proxy as POST };

Point the script at your proxy

In your Spreelytics snippet (see SvelteKit), load the script through /sp/js/ and add api_host:

<script src="/sp/js/spreelytics-loader.js"></script>
<script>
  Spreelytics.init({
    client_key: "YOUR_CLIENT_KEY",
    api_host: "https://www.example.com/sp",
  });
</script>

Check it works

Open your site with the browser's Network tab open and filtered by /sp/. The script files and requests like track/page should come from your own domain, with status 200. In Spreelytics, Settings → Tracking script says "Last seen just now" and Realtime shows you in your own country.

  • Wrong country, or 200 and nothing arrives: the proxy isn't passing the visitor's IP address in X-Forwarded-For. See The visitor's IP address below.
  • 401: the Origin and Referer headers didn't arrive, or your domain isn't in Allowed domains.

The visitor's IP address

getClientAddress() comes from your adapter. On Vercel, Netlify and Cloudflare it is the visitor. With adapter-node behind a load balancer, set ADDRESS_HEADER=X-Forwarded-For, or every visitor arrives from the load balancer's address.

See your own numbers today.

Free up to 1M events a month · no credit card

Contact

Tell us what you need.

A real person reads these. We usually reply within one working day.

Or email us directly at contact@spreelytics.com.