# Browser script

> One line in your layout sends page views, single-page app navigation, outbound clicks, scroll depth, time on page, Web Vitals and who is online, with no cookies.

Source: https://status.mettr.app/docs/browser-script

## Install

Put the snippet from the site's settings (**Tracking**, **Browser script**) in the `<head>` of every page:

```html
<script defer data-key="pk_your_site_key" src="https://mettr.app/script.js"></script>
```

`data-key` is the site's **public key**. It is meant to be seen: it can only send page views, events, engagement, Web Vitals and presence, and only from the site's own domains. Anything else needs a secret key from your server.

## What it sends

- **Page views**, on load and on each navigation of a single-page app (`history.pushState`, `replaceState` and the back button), with the page's address and the referrer.
- **Outbound clicks**: a link to another host sends an `outbound` event with the link's address.
- **Presence**: a heartbeat every 30 seconds while the tab is visible, so the dashboard knows who is online and on which page. It pauses while the tab is hidden, stops after 30 minutes with no scroll, key, pointer or touch (a tab left open is not a visitor), and says goodbye when the page is closed. Heartbeats are not stored and do not count toward your plan.
- **Engagement**: when a page is left, how far down it was read (percent) and how long it was in use (the tab visible and touched in the last five minutes). These give scroll depth, time on page and visit duration, and do not count toward your plan either.
- **Web Vitals**: how fast each page loaded and answered for the person on it, sent once as the page is first hidden or left. See [Web Vitals](#web-vitals). They do not count toward your plan.

It sets no cookies and keeps nothing in the browser. A browser driven by automation (`navigator.webdriver`) is flagged as a bot.

An event that does not get through (the visitor is offline, mettr answers `429` or a `5xx`) is held in the page and tried again after 2, 10, 30 and then every 60 seconds, six more times at most; if the page is closed first, it goes with the page's last word. Heartbeats are not tried again, since the next one says the same, and a `4xx` such as `402` is not either.

## Web Vitals

The script measures each page as Google defines Web Vitals, with the browser's own performance timeline (no library), and sends what it measured as one `vital` event as the page is first hidden, left, or replaced by the next page of a single-page app:

| Vital | What it is | Good | Poor |
|---|---|---|---|
| `lcp` | Largest Contentful Paint: when the main content showed | up to 2.5 s | over 4 s |
| `inp` | Interaction to Next Paint: how long a tap, click or key took to answer, the slowest but one in fifty | up to 200 ms | over 500 ms |
| `cls` | Cumulative Layout Shift: how much the page jumped about, its worst burst | up to 0.1 | over 0.25 |
| `fcp` | First Contentful Paint: when anything showed | up to 1.8 s | over 3 s |
| `ttfb` | Time to First Byte: when your server's answer began | up to 0.8 s | over 1.8 s |

LCP, INP and CLS are the Core Web Vitals. The dashboard judges each at the 75th percentile of your pages, as Google and [Search Console](/docs/search-console) do: the [Core Web Vitals panel](/docs/dashboard#web-vitals) says whether they pass.

- A browser sends only what it can measure: Chrome and Edge all five; Firefox no layout shifts; Safari only what its version supports.
- No LCP or FCP for a page opened in a background tab, and no LCP after the first tap or key, as Google counts them.
- INP needs an interaction: a page nobody tapped or typed on has none.
- After a navigation within a single-page app, the next page gets its own INP and CLS; LCP, FCP and TTFB belong to the first page loaded.

Unlike Search Console's report, these are every visit's own numbers as they come in, on pages with few visitors too, with no 28 days to wait.

## Your own events

Once the script has loaded, `window.mettr` sends an event:

```js
window.mettr('event', 'signup', { plan: 'pro', trial: true });
```

To call it before the script has loaded (from an inline handler, say), define a stub first. Calls are queued and sent once the script is ready:

```html
<script>
  window.mettr = window.mettr || function () { (window.mettr.q = window.mettr.q || []).push(arguments); };
</script>
```

Names are up to 100 characters. Props are flat: up to 30, each named with letters, digits, `_`, `.` or `-` (up to 64), each a string (up to 500 characters), number, boolean or null.

Events sent from the page can be blocked or faked like anything in a browser. For events that matter (sign-ups, payments), send from your server with a [server SDK](/docs/laravel) or the [Events API](/docs/events-api).

## Through your own domain

Ad blockers mostly leave first-party requests alone. To load the script and send events through routes on your own domain, point the tag at them with `src` and `data-api`:

```html
<script defer data-key="pk_your_site_key" data-api="/_live/events" src="/_live/script.js"></script>
```

`/_live/script.js` serves `https://mettr.app/script.js`. `/_live/events` must do more than pass the request on: a plain reverse proxy would show mettr your server's address for every visitor, so they would all look like one person in one place. Instead, read the events the script posts and send them to the [Events API](/docs/events-api) with a **secret key**, adding the visitor's `ip` and `user_agent` to each (fields only a secret key may send). The server SDKs ([Laravel](/docs/laravel), [Node.js](/docs/node), [Next.js](/docs/nextjs), [Python](/docs/python), [Ruby](/docs/ruby), [Go](/docs/go)), the [WordPress plugin](/docs/wordpress) and the [Cloudflare Worker](/docs/cloudflare) have these routes built in.

## Content Security Policy

Allow the script and its requests: `script-src https://mettr.app` and `connect-src https://mettr.app`, or your own domain when you proxy. The stub above is an inline script: give it your policy's nonce, or leave it out and call `window.mettr` only after the page has loaded.

## Leaving yourself out

Run `localStorage.mettr_ignore = "true"` in the browser's console: that browser sends nothing from then on. To leave out your whole team, use the Laravel SDK's [staff gate](/docs/laravel#staff), or list your user ids or addresses under the site's **General** settings, **Staff**.

## Errors you may see

The script sends from the site's domains only. If the dashboard stays empty, open the browser's network panel: a `403` on `/api/v1/events` means the page's domain is not one of the site's domains (add it under **General**), and a `402` that the team's plan has used its events for the month.
