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:
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
// 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 });
}
}// Serves Lyli's script and forwards events through your own domain.
// Needs Node 18+ (built-in fetch). Set LYLI_PROXY_KEY in your environment (see below).
// Add before your other routes.
const LYLI = "https://app.lyli.io";
app.get("/k3m9x2p7qa/script.js", async (req, res) => {
try {
const r = await fetch(`${LYLI}/t.js`);
res
.status(r.status)
.type("text/javascript")
.set("Cache-Control", "public, max-age=3600")
.send(await r.text());
} catch {
res.sendStatus(502);
}
});
app.post("/k3m9x2p7qa/event", express.text({ type: "*/*", limit: "8kb" }), async (req, res) => {
try {
// Forward the body however earlier middleware left it: text, a Buffer or parsed JSON.
const body =
typeof req.body === "string"
? req.body
: Buffer.isBuffer(req.body)
? req.body.toString("utf8")
: JSON.stringify(req.body ?? {});
const r = await fetch(`${LYLI}/api/proxy/event`, {
method: "POST",
body,
headers: {
"Content-Type": "text/plain",
"User-Agent": req.get("user-agent") || "",
// req.ip is the address that connected to your server, which the browser can't forge.
// Behind a load balancer or Cloudflare, set app.set("trust proxy", <number of proxies>)
// so req.ip is the visitor's (see Express's "behind proxies" guide). Use
// req.get("cf-connecting-ip") only if your server accepts traffic from Cloudflare alone.
"X-Lyli-Visitor-IP": req.ip,
"X-Lyli-Proxy-Key": process.env.LYLI_PROXY_KEY || "",
},
});
res.sendStatus(r.status);
} catch {
res.sendStatus(502);
}
});// Cloudflare Worker, on the route your-domain.com/k3m9x2p7qa/*
// Add the key as a secret: npx wrangler secret put LYLI_PROXY_KEY
const LYLI = "https://app.lyli.io";
export default {
async fetch(request, env) {
const { pathname } = new URL(request.url);
if (pathname === "/k3m9x2p7qa/script.js") {
return fetch(`${LYLI}/t.js`, { cf: { cacheTtl: 3600, cacheEverything: true } });
}
if (pathname === "/k3m9x2p7qa/event" && request.method === "POST") {
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": request.headers.get("CF-Connecting-IP") || "",
"X-Lyli-Proxy-Key": env.LYLI_PROXY_KEY,
},
});
return new Response(null, { status: res.status });
}
return new Response("Not found", { status: 404 });
},
};# Inside your server { } block. Serves Lyli's script and forwards events.
# The key lives in its own file (next step), so this config can be committed safely.
# Only the headers set here go to Lyli: never your visitors' cookies or sign-in.
location = /k3m9x2p7qa/script.js {
proxy_pass https://app.lyli.io/t.js;
proxy_pass_request_headers off;
proxy_set_header Host app.lyli.io;
proxy_set_header Accept-Encoding $http_accept_encoding;
proxy_ssl_server_name on;
}
location = /k3m9x2p7qa/event {
proxy_pass https://app.lyli.io/api/proxy/event;
proxy_pass_request_headers off;
proxy_set_header Host app.lyli.io;
proxy_set_header Content-Type text/plain;
proxy_set_header User-Agent $http_user_agent;
proxy_ssl_server_name on;
# $remote_addr is the address that connected to Nginx. Behind a load balancer or CDN, set up
# the realip module (set_real_ip_from + real_ip_header) so it is the visitor's address.
proxy_set_header X-Lyli-Visitor-IP $remote_addr;
include /etc/nginx/lyli-proxy-key-s_k3m9x2p7qa.conf;
}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.
LYLI_PROXY_KEY=your-proxy-keyLYLI_PROXY_KEY=your-proxy-keynpx wrangler secret put LYLI_PROXY_KEYproxy_set_header X-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.
<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.
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.req.ip. Behind a load balancer or Cloudflare, set Express’s trust proxy to the number of proxies.CF-Connecting-IP header.$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:
<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 inscript-srcandconnect-src. The recorder still needs our address.
Troubleshooting
X-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.data-site isn’t your site’s ID.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.