@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
Inapp/api/lurk/[...lurk]/route.ts:
Express
Mount the relay before any body parser such asexpress.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 wrongforwardedHops, lets a visitor fake addresses, and the relay cannot tell.
- With
x-forwarded-for, setforwardedHopsto the number of proxies you run. The relay counts that many entries from the right. - Vercel replaces
x-real-ipandx-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.jsandauto.jsitself, and handles any path that ends in/script.js,/auto.jsor/collect. Mount it only under your own endpoint. - It needs an
Originheader that is your site. SetallowedOriginswhen a proxy rewritesHost. - A request with
Sec-GPC: 1orDNT: 1is 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
onErrorto 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.siteslimits the relay to site ids you list. Any other gets 403.apiUrlmust behttps, except for a Lurk on the same machine.