# Laravel

> The mettr/laravel package sends a Laravel app's page views, events, errors, sign-ins, failed jobs, response times and crawler visits, after the response has gone.

Source: https://mettr.app/docs/laravel

PHP 8.3 or later, Laravel 12 or 13. The source is on [GitHub](https://github.com/mettrhq/mettr-laravel), MIT licensed.

## Install

```bash
composer require mettr/laravel
```

Add the site's keys (the site's settings, **Tracking**) to `.env`:

```dotenv
METTR_SECRET_KEY=sk_...
METTR_PUBLIC_KEY=pk_...
```

and the script to the `<head>` of your layout:

```blade
@mettr
```

It sends from production only. Set `METTR_ENABLED=true` to try it elsewhere. To change the defaults, publish the config:

```bash
php artisan vendor:publish --tag=mettr-config
```

## What it sends on its own

- **Page views, navigation, outbound clicks and presence**, from the [browser script](/docs/browser-script). The script and its events go through `/_live` on your own domain (`METTR_PROXY_PATH`), so ad blockers mostly leave them alone and each event carries your visitor's address and signed-in user. Set the path to null to talk to mettr directly instead.
- **Sign-ins and sign-ups** (`login`, `signup`), from Laravel's `Login` and `Registered` events.
- **404s and exceptions**, as errors. An exception is sent by its class only, never its message.
- **Failed queue jobs** (`job_failed`), with the job, queue and exception class.
- **Response times** of 5% of requests (`METTR_RESPONSE_TIME_SAMPLE`).
- **Crawler visits** (`crawl`): each request whose user agent matches `crawler_pattern` (search engines, AI crawlers, SEO tools, link previews, uptime checks), with its page and HTTP status. mettr names the crawler, checks it against its owner's network where it can, keeps crawls apart from your visitors and does not count them toward your plan.

Each can be turned off under `track` in the config. The paths under `ignore` (the health check, `up`, by default) send nothing: no event leaves during such a request, neither the SDK's own (404s, response times, sign-ins, exceptions) nor yours from `Mettr::event()`. Put a wall screen's polling or an internal dashboard there, and keep a webhook handler whose events you want outside it. Subscriptions are still sent.

## Your own events

```php
use Mettr\Laravel\Facades\Mettr;

Mettr::event('upgrade', ['plan' => 'pro']);
```

Props are flat: up to 30, named with letters, digits, `_`, `.` or `-`, each a string (up to 500 characters), number, boolean or null. The signed-in user's id, the current page and the visitor's address and user agent go with each event, so it lands in the visitor's visit and place.

Events are sent in one batch after the response has gone, so visitors never wait on mettr. In queue workers and console commands they go when the job or command ends. To send through a queue instead, set `METTR_QUEUE` to a queue connection.

An app that already logs its clicks, downloads and likes with `spatie/laravel-activitylog` can send them as they are written: set `METTR_ACTIVITY_LOG=true`. Each row is named as the history import names it (`engagement.download`, `ui.quick_view`), so live and imported events read the same.

## Subscriptions

For MRR when your billing is your own (anything but Stripe or Paddle, which mettr reads itself), send each subscription as it is now, whenever it changes (an observer on your subscription model is a good place), and every subscription once for the history:

```php
Mettr::subscription(
    id: (string) $subscription->id,
    status: 'active',           // active, past_due, trialing, paused, canceled
    amount: 19.00,              // each cycle, before tax, in the main unit
    currency: 'USD',
    interval: 'month',          // day, week, month, year
    userId: (string) $subscription->user_id,
    plan: 'Pro',
    startedAt: $subscription->created_at,
    canceledAt: $subscription->ended_at,  // when it stopped paying
    changedAt: null,            // when it became as sent; now if null
);
```

The first one connects the site's revenue to your own billing. Several at once with `Mettr::subscriptions([...])`. Staff are not left out here: a subscription is billing, not a visit. More in [Subscriptions API](/docs/subscriptions-api).

## Staff

Define a gate and name it in `METTR_STAFF_GATE`: nothing is sent for users it allows, from the server or through the proxy.

```php
Gate::define('mettr-staff', fn (User $user): bool => $user->is_admin);
```

```dotenv
METTR_STAFF_GATE=mettr-staff
```

## Importing history

Sites using `spatie/laravel-activitylog` can send their history through the [Import API](/docs/import-api):

```bash
php artisan mettr:import-activity-log --dry-run
php artisan mettr:import-activity-log --skip-causer=1,2
```

Rows are sent in id order, a thousand at a time, each with its own id, so a second run adds nothing twice. If a run stops, it prints the `--after` to resume from. `navigation/page_view` and `navigation/view` become page views, `error/not_found` and `error/exception` errors, `auth/*` events of that name (`register` as `signup`), and every other row an event named `<log_name>.<description>`. `--skip-log` leaves out log names, `--timezone` says what zone `created_at` was written in.

If the site already sends live, pass `--before` the time it started (`--before="2026-11-29 20:48"`, in the log's zone), so those hours are not counted twice.

## Privacy

mettr uses the visitor's address to find the place and to tell visitors apart, and keeps nothing of it. Behind Cloudflare the address is the one in `CF-Connecting-IP`, taken only from requests that came from Cloudflare's own addresses. Messages of exceptions, request bodies and headers other than the browser's user agent are never sent.

## Settings

| Variable | Default | What it does |
|---|---|---|
| `METTR_SECRET_KEY` | none | The site's secret key, for everything the server sends |
| `METTR_PUBLIC_KEY` | none | The site's public key, for the browser script |
| `METTR_ENABLED` | production only | Send or not |
| `METTR_PROXY_PATH` | `_live` | The first-party route for the script; null to load it from mettr |
| `METTR_STAFF_GATE` | none | A gate whose users send nothing |
| `METTR_QUEUE` | none | A queue connection to send through, instead of after the response |
| `METTR_RESPONSE_TIME_SAMPLE` | `0.05` | The share of requests whose response time is sent |
| `METTR_ACTIVITY_LOG` | `false` | Send activity log rows as they are written |
| `METTR_ENDPOINT` | `https://mettr.app` | Where to send |
