# Browser script

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

Source: https://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 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.

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

## 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 [Laravel SDK](/docs/laravel) has 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.
