mettr / laravel
Send a Laravel app's page views, events, errors and sign-ins to mettr, live
Requires
- php: ^8.3
- illuminate/auth: ^12.0|^13.0
- illuminate/console: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/http: ^12.0|^13.0
- illuminate/queue: ^12.0|^13.0
- illuminate/routing: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- illuminate/view: ^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-laravel: ^4.0|^5.0
- pestphp/pest-plugin-type-coverage: ^4.0|^5.1
- shipfastlabs/pun: 0.4.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Sends a Laravel app's page views, events, errors and sign-ins to mettr, as they happen. PHP 8.3+, Laravel 12 or 13.
Install
composer require mettr/laravel
Add the site's keys (mettr, site settings, "API keys") to .env:
METTR_SECRET_KEY=sk_... METTR_PUBLIC_KEY=pk_...
and the script to the <head> of your layout:
@mettr
It sends from production only; set METTR_ENABLED=true to try it
elsewhere. To change the defaults:
php artisan vendor:publish --tag=mettr-config
What it sends on its own
- Page views, navigation, outbound clicks and presence, from the browser
script. The script and its events go through
/_liveon 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'sLoginandRegisteredevents. - 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).
Each can be turned off under track in the config.
Your own events
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.
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 instead:
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. Page views, errors and sign-ins are left out, as the
SDK sends those itself, and so are the logs in mettr.activity_log.skip
(auth, response_times, edge_cache, indexing); a row caused by
staff is not sent.
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.
Gate::define('mettr-staff', fn (User $user): bool => $user->is_admin);
A browser can also opt out on its own: localStorage.mettr_ignore = "true".
Importing history
Sites using spatie/laravel-activitylog can send their history:
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> with the subject's type and id in its props.
--skip-log leaves out log names (response_times,edge_cache,indexing by
default: snapshots, not visits), --timezone says what zone created_at
was written in (the app's by default).
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.
Tests
composer install
composer test
License
MIT