> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getlurk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Install web analytics

> Add the Lurk script and a small relay to your own site to count page views and events. No cookie unless a visitor consents.

The browser sends hits through your relay. Your project key stays on the server.

See [how web analytics counts](https://docs.getlurk.com/web/definitions.md), [privacy](https://docs.getlurk.com/web/privacy.md), [relay recipes](https://docs.getlurk.com/web/relay-recipes.md) and [why a report has no numbers](https://docs.getlurk.com/web/no-numbers.md).

## Before you start

1. Add a site in the dashboard, give it its allowed origins and copy its id.
2. **Check that `page_view` is in the site's tracking plan.** A site without it counts nothing, and the script goes quiet for the page, so custom events are lost too.
3. Keep a project key in your server's environment. It never goes into the page.

Install the package:

```bash theme={null}
npm install @getlurk/web
```

## Add the relay

`@getlurk/web` provides Next.js, Express and Cloudflare Workers helpers. Name the address header your trusted host overwrites; there is no default or fallback. Never trust a header a visitor controls.

```ts theme={null}
// app/api/lurk/[...lurk]/route.ts
import { lurkRoute } from "@getlurk/web/next";

export const { GET, POST } = lurkRoute({
  projectKey: process.env.LURK_KEY ?? "",
  ipHeader: "x-forwarded-for",
  forwardedHops: 1,
});
```

* With `x-forwarded-for`, `forwardedHops` is how many proxies you run. The relay counts that many entries from the right.
* Behind Cloudflare, use `cf-connecting-ip`.
* On Vercel as the first hop, use `x-real-ip` and `x-vercel-ip-country` for `countryHeader`. With a proxy in front, use your outermost trusted proxy's header.
* The relay serves the script itself at `/api/lurk/script.js`, the same version as the relay. It handles any path ending in `/script.js`, `/auto.js` or `/collect`, so mount it only under your own endpoint.
* It never logs, sends no CORS header, and never passes the key or Lurk's error text on.

## Hosted collection

Hosted collection is coming soon and is not available yet. The [relay instructions](#add-the-relay) describe how to prepare your integration.

## Add the script

```html theme={null}
<script async src="/api/lurk/script.js" data-site="SITE_ID" data-endpoint="/api/lurk"></script>
```

Page views are sent on load and path changes. Send your own events with `lurk.track("signup_started", { plan: "pro" })`. Both automatic events are off until you set `data-outbound-links="true"` or `data-file-downloads="true"`.

With a bundler, `import { lurk } from "@getlurk/web"` and call `lurk.init({ site: "SITE_ID" })`.

## Your own copy of the files

**Copy both `lurk.js` and `auto.js`** when you serve the script yourself, into the same folder. The tag adds `auto.js` next to itself when an automatic event is on.

* A tag's `integrity` attribute does not cover `auto.js`.
* A policy that allows scripts by nonce alone, without `'strict-dynamic'`, blocks `auto.js`. Allow your own origin, or add `'strict-dynamic'`.

## Content security policy

With the script and the relay on your own domain:

```text theme={null}
script-src 'self'; connect-src 'self'
```

Inline initialization needs your nonce or hash. The script adds no style, image or frame.

For `require-trusted-types-for 'script'`, allow `trusted-types lurk-web`. It accepts only the sibling `auto.js` URL; no default policy is needed.

With a nonce and `'strict-dynamic'`, the browser ignores `'self'` in `script-src`. Put your nonce on the script tag: `auto.js`, which the script adds, is then trusted too. A tag without the nonce does not load.

## The default tracking plan

A site stores only declared hits and properties. New sites start with this plan:

| Hit name | Sent by | Properties |
| - | - | - |
| `page_view` | page loads, route changes and `lurk.page` | none |
| `outbound_link` | `data-outbound-links` | `host` |
| `file_download` | `data-file-downloads` | `file_path`, `file_ext` |

* The three properties are text, and are declared only when they fit within the project's properties per site.
* A site made before the plan, or one whose plan you edited, may lack them: check before you switch an event on.
* **A hit with a property the site did not declare is rejected whole.** A hit name it did not declare is counted as unknown and nothing is stored.

## Limits

| What | Limit |
| - | - |
| Hits waiting in the page | 100, then the oldest are dropped |
| Hits in one request | 10 by default, 1 to 50. A project that lowers its batch size from 50 sets `batchSize` to match |
| A request from the page | 32 KiB |
| A request to the relay | 48 KiB |
| Beacons and keepalive requests together | 64 KiB |
| A request sent again | After 1, 5 and 30 seconds, then dropped |
| Calls a minute for each project key | 600 |

**A batch above your project's batch size is deferred whole.** Nothing is stored and no number in the dashboard shows it, so enable `debug` first.

**Counts are a lower bound.** When a page closes, hits go out as a beacon whose answer cannot be read, and the browser holds only 64 KiB of them. Hits held back by that are lost.

**Each project has a monthly limit** of accepted hits, counted per UTC month. At the limit Lurk accepts no new hits and the Web tab says "This month's limit is reached."; the count starts again on the 1st at 00:00 UTC.

**Lurk can pause collection for a project.** The Web tab says "Lurk paused collection for this project." Hits sent during a pause are not kept and not collected later.

## Privacy

* The script sends nothing for a browser that sends Global Privacy Control or Do Not Track, and the relay checks those headers again and sends nothing. Nothing about that visitor reaches Lurk.
* `lurk.optout()` stops counting a visitor on this and later page loads.
* Without consent, one thing is stored on the device: the visitor's opt-out, in `localStorage` under the key `lurk_optout`. Name that key in your privacy notice, apart from analytics storage.
* After `lurk.consent(true)`, with identified analytics on and the relay in mode `full`, your relay sets one cookie, `lurk_vid`, that holds a random visitor id for at most 13 months. `lurk.consent(false)` removes it.

## What the browser gets back

| Lurk answered | The relay answers | The script |
| - | - | - |
| 200 | 200 and the counts | Goes on, or goes quiet when deferred |
| A refused body | 400 | Drops the batch |
| Too many requests | 429 | Tries again |
| Unavailable or no answer | 503 | Tries again |
| Anything else | 502 | Stops for the page |

The relay itself answers 403 for a missing or foreign `Origin`, 413 over its size limit, 405 for the wrong method (`HEAD` included) and 204 for a visitor who sent GPC or DNT. A site that is off, or a bot, is answered 200 without a ready visit.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.