The visitor's IP address

Docs·Proxy guide

Proxy with Astro

One endpoint serves Spreelytics from /sp/. It runs on the server, so your site needs an adapter, like @astrojs/node, @astrojs/vercel or @astrojs/netlify. 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 endpoint

Create src/pages/sp/[...path].ts:

// src/pages/sp/[...path].ts
// Serves Spreelytics from /sp/ on your site: /sp/js/ is the script, everything else is tracking.
import type { APIRoute } from "astro";

export const prerender = false;

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"];

export const ALL: APIRoute = async ({ request, params, url, clientAddress }) => {
  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", clientAddress);

  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 });
};

Point the script at your proxy

In your Spreelytics snippet (see Astro), 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

clientAddress comes from your adapter. On Vercel and Netlify it is the visitor. With @astrojs/node behind a load balancer, check that your adapter reads 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.