Looking to hire Laravel developers? Try LaraJobs

laravel maintained by mettr

Description
Send a Laravel app's page views, events, errors and sign-ins to mettr, live
Last update
2026/11/29 23:58 (dev-main)
License
Downloads
2

Comments
comments powered by Disqus

mettr for Laravel

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 /_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).

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