Skip to content

Via your domain

Serve our script from your own domain, and let your server forward page views and events to us. Content blockers that stop requests to analytics hosts leave your own domain alone, so those visitors are counted too. It’s for sites that run their own server code: a Next.js or Express app, a Cloudflare Worker, or Nginx.

How it works

Without it, the browser loads the script from app.lyli.io and sends each page view there. With Via your domain, it asks two paths on your domain instead, and your server passes the requests on:

Path on your domainWhat your server does
/k3m9x2p7qa/script.jsFetches our script and serves it.
/k3m9x2p7qa/eventForwards each page view, custom event, and Web Vitals measurement to us, with the visitor’s IP address and your site’s proxy key in two headers.

The prefix is your site’s ID without s_, so it’s unique to your site, and a blocklist can’t catch it by a common name, the way lists catch paths like /analytics/event. Nothing else on your domain is forwarded. With the right key, we count a page view exactly as one sent to us directly; without it, we store nothing.

Where to find it

Open your site, choose Settings in the sidebar, then Install. In the second step, Add the script, choose the Via your domain tab, then pick your server: Next.js, Express, Cloudflare Worker, or Nginx. A note under the HTML and Next.js tabs links there too.

Everyone in the workspace sees the setup. Only admins see the proxy key in it; members are asked to have an admin add it. Sample sites don’t have the tab.

Set it up

Install fills in your site’s ID, our address, and, for admins, the key. The code here uses the example ID s_k3m9x2p7qa; copy yours from Install.

1. Forward two paths on your domain

/k3m9x2p7qa/script.js and /k3m9x2p7qa/event, unique to your site. Pick your server:

app/k3m9x2p7qa/[file]/route.ts
// app/k3m9x2p7qa/[file]/route.ts
// Serves Lyli's script and forwards events through your own domain.
// Set LYLI_PROXY_KEY in your environment (see below).
const LYLI = "https://app.lyli.io";

// Where the visitor's address comes from. Only headers your own platform sets can be trusted:
// - "x-forwarded-for" (most hosts and proxies): TRUSTED_PROXIES is how many proxies in front of this
//   app add to it, like Express's "trust proxy". 1 behind one proxy; 2 behind e.g. Cloudflare and a
//   load balancer. Entries further left can be set by the browser and are never used.
// - "x-real-ip": only if your proxy always overwrites that header.
const VISITOR_IP_FROM: "x-forwarded-for" | "x-real-ip" = "x-forwarded-for";
const TRUSTED_PROXIES = 1;

function visitorIp(headers: Headers): string {
  if (VISITOR_IP_FROM === "x-real-ip") return headers.get("x-real-ip") ?? "";
  const chain = (headers.get("x-forwarded-for") ?? "")
    .split(",")
    .map((entry) => entry.trim())
    .filter(Boolean);
  return chain.at(-TRUSTED_PROXIES) ?? "";
}

type Context = { params: Promise<{ file: string }> };

export async function GET(_request: Request, { params }: Context) {
  if ((await params).file !== "script.js") return new Response(null, { status: 404 });
  const res = await fetch(`${LYLI}/t.js`);
  return new Response(res.body, {
    status: res.status,
    headers: {
      "Content-Type": "text/javascript; charset=utf-8",
      "Cache-Control": "public, max-age=3600",
    },
  });
}

export async function POST(request: Request, { params }: Context) {
  if ((await params).file !== "event") return new Response(null, { status: 404 });
  try {
    const res = await fetch(`${LYLI}/api/proxy/event`, {
      method: "POST",
      body: await request.text(),
      headers: {
        "Content-Type": "text/plain",
        "User-Agent": request.headers.get("user-agent") ?? "",
        "X-Lyli-Visitor-IP": visitorIp(request.headers),
        "X-Lyli-Proxy-Key": process.env.LYLI_PROXY_KEY ?? "",
      },
    });
    return new Response(null, { status: res.status });
  } catch {
    return new Response(null, { status: 502 });
  }
}

2. Add the proxy key to your server’s environment

The key lets your server tell us who the visitor is. Keep it on the server, out of the page and out of your repository. Here your-proxy-key stands for it. For a Worker, Wrangler asks for the value: paste the key. For Nginx, the step is Save the proxy key in its own file: one file per site, so the main config can be committed; make the file readable only by Nginx.

Environment
LYLI_PROXY_KEY=your-proxy-key

3. Load the script from your domain

Put this tag on every page, before </head>. It replaces the one from the HTML or Next.js tab.

Every page, before </head>
<script defer src="/k3m9x2p7qa/script.js"
  data-site="s_k3m9x2p7qa"
  data-api="/k3m9x2p7qa/event"></script>

If your site recognizes returning visitors, add data-remember to this tag only for visitors who agree, for example through your consent tool: it keeps a visitor ID in their browser, which in the EU and UK needs their consent. Without it, they get a daily code.

Check that it works

Open a page of your site with the new tag. In your browser’s developer tools, the request to /k3m9x2p7qa/event on your domain answers 202 once we’ve taken it. Every snippet passes on the status we answer with, so another one tells you what’s wrong (see Troubleshooting).

A 202 also answers requests we take but leave out, such as one from curl or a server tool, which we count as a bot. So test from an ordinary browser, and look for the page view in Realtime. A browser where you excluded your own visits, or one run by automation, sends no request at all.

If the site’s Individual visits is on, Settings › Install also describes the latest page view, and its Via row reads Your domain. With Individual visits off, the default, Install says only that data is arriving.

The visitor’s address

We tell visitors apart and place them by the address your server sends in X-Lyli-Visitor-IP. It must be the visitor’s, not your server’s or a load balancer’s, and it must come from what your own platform sets, since a browser can write anything into the headers it sends.

ServerWhere the snippet reads it
Next.jsThe x-forwarded-for header, from the right: set TRUSTED_PROXIES to how many proxies in front of your app add to it. Use x-real-ip only if your proxy always overwrites it.
Expressreq.ip. Behind a load balancer or Cloudflare, set Express’s trust proxy to the number of proxies.
Cloudflare WorkerThe CF-Connecting-IP header.
Nginx$remote_addr. Behind a load balancer or CDN, set up the realip module (set_real_ip_from and real_ip_header).

If every request carries the same address, visits all show one place or none, visitors with the same browser look like one, and a busy site runs into the per-visitor limit. A header that’s missing or isn’t an IP address is refused.

The proxy key

Each site has its own. Admins see it in Install’s Via your domain tab, and its last four characters in Settings › General › Proxy key. A request with a wrong key is refused and nothing of it is stored. Repeated wrong keys from one address are held back for a short while.

If the key leaked, an admin can choose Rotate key… in Proxy key. The old key stops working at once: page views through your own domain are refused until your server has the new one, which shows in its place, ready to copy.

Errors, replays, and bots

Errors. With Errors on for the site, Install’s snippets forward a third path, /k3m9x2p7qa/errors.js, which serves e.js, and its error reports go through the same event path. Step 3 then shows e.js’s tag too, with your Errors settings in it; the two tags replace the ones from the HTML or Next.js tab and Settings › Errors. With the default settings, e.js’s tag reads:

e.js, next to it
<script defer src="/k3m9x2p7qa/errors.js" data-site="s_k3m9x2p7qa" data-api="/k3m9x2p7qa/event"
  data-consent="wait"></script>
  • Replays and heatmaps. Their recorder isn’t forwarded: the tag in Settings › Recording loads it from our domain, and it sends there.
  • Bots. Still left out of every number. The Bots panel, opened from the Overview’s status line, says how many came through your domain. It warns when every one it left out came through your domain without a browser’s user agent: the sign that your proxy isn’t forwarding the User-Agent header.
  • Hostnames. Whether a page view counts still depends on the page’s own address, not your server’s: Choose which hostnames count.
  • Content-Security-Policy. With the script and its events on your own domain, 'self' covers them in script-src and connect-src. The recorder still needs our address.

Troubleshooting

You seeWhat to do
403The proxy key is missing or wrong, for example after a rotation, or not set where the code runs. Or the page’s hostname isn’t one the site counts: Settings › General › Hostnames shows the latest one turned away.
400X-Lyli-Visitor-IP is missing or isn’t an IP address: check where your server reads the visitor’s address. Or the body didn’t reach us as the browser sent it: forward it unchanged. A custom event we can’t read, such as one with more than 10 properties, gets it too.
402The workspace isn’t recording: its trial or plan ended more than 14 days ago. See Plans and usage.
404The path isn’t forwarded yet, or data-site isn’t your site’s ID.
429Too many requests: repeated wrong keys, which clear within a minute once the key is right, or the same address sent for every visitor.
502Your server couldn’t reach us. The Next.js and Express snippets answer 502 then, and Nginx does on its own.
Bots panel warning“Your proxy may not be forwarding the User-Agent header”: requests through your domain named a server tool or no browser, so we left them out as bots. Forward each visitor’s User-Agent header, as Install’s snippets do.

Privacy

Via your domain changes where requests go. What we collect stays the same, but for one thing: we note that each page view and event came through your domain, and the bots we leave out are counted by the way they came. Your server sends us the page view, the visitor’s browser identification (user agent), their IP address, and your key. The snippets set their own headers, so your visitors’ cookies and sign-in never reach us.

We use the address for the daily visitor code, the per-visitor limit, and the location, looked up on the servers that run Lyli and rounded to about 11 km. We never store IP addresses in our database; our hosting provider’s request logs keep them for about 7 days.

Consent doesn’t change. Whether you need your visitors’ consent depends on the country; in Sweden and Germany, assume you do (consent, by place). Exclude your own visits works the same through your domain.