Skip to main content
These recipes use the relay helpers in @getlurk/web. Hosted collection is coming soon. The relay is a small route on your own domain. The script posts to it, and it calls Lurk with your project key. Install web analytics shows the first setup. This page shows more, and what to watch for. Every recipe needs two things:
  • the project key in your server’s environment, never in the page;
  • ipHeader, the name of a header that your host sets and overwrites. There is no default and no fallback.

Next.js

In app/api/lurk/[...lurk]/route.ts:

Express

Mount the relay before any body parser such as express.json(). The relay reads the raw body itself to hold it to its size limit. A body that something else already read is answered 502.

Cloudflare Workers

Any other host

createRelay from @getlurk/web/relay returns a function from a Request to a Response. Use it wherever those two exist.

Choose the address header

The relay reads the visitor’s address only from the header you name. It answers 400 when the header is missing or does not hold an address. A header a visitor can set, or a wrong forwardedHops, lets a visitor fake addresses, and the relay cannot tell.
  • With x-forwarded-for, set forwardedHops to the number of proxies you run. The relay counts that many entries from the right.
  • Vercel replaces x-real-ip and x-forwarded-for. This holds when Vercel is the first hop. With your own proxy in front, use the header your outermost trusted proxy sets.
  • Without countryHeader, the country is unknown.

What the relay does

  • It serves script.js and auto.js itself, and handles any path that ends in /script.js, /auto.js or /collect. Mount it only under your own endpoint.
  • It needs an Origin header that is your site. Set allowedOrigins when a proxy rewrites Host.
  • A request with Sec-GPC: 1 or DNT: 1 is answered 204 and nothing goes to Lurk.
  • Cookies, other headers and the query string are not forwarded. After consent, in mode full, the relay reads one cookie of its own, lurk_vid, and sends its value to Lurk as the visitor id.
  • It never logs. Pass onError to get an error code for your own logs.
  • Lurk’s errors and your key are never passed to the browser.

Limits

A request over the size limit gets 413. A wrong method gets 405, and so does HEAD. A missing or foreign Origin gets 403.

Other options

  • mode: "off" sends nothing for a visitor. Pass a function of the country to switch collection off for some countries.
  • sites limits the relay to site ids you list. Any other gets 403.
  • apiUrl must be https, except for a Lurk on the same machine.