PHP
The mettr/mettr package sends any PHP app's own events, errors and subscriptions to mettr, with nothing to set up but a secret key.
PHP 8.3 or later, with ext-curl. The source is on GitHub, MIT licensed. For Laravel, mettr/laravel does more: page views, errors, sign-ins, a queue, and a first-party route for the browser script.
Install
composer require mettr/mettr
Send an event
Make a client with the site's secret key (the site's settings, Tracking, Secret keys). Keep the key on the server, in the environment, never in a page:
$mettr = new Mettr\Client(getenv('METTR_SECRET_KEY')); $mettr->event('signup', ['plan' => 'pro'], userId: (string) $user->id);
Props are flat: up to 30, each a string, number, boolean or null. The user id and the page (url:) are optional.
Several events at once, as the Events API takes them (sent in batches of up to 100):
$mettr->send([ ['type' => 'event', 'name' => 'export', 'props' => ['format' => 'csv']], ['type' => 'error', 'name' => 'PaymentFailed', 'url' => 'https://example.com/pay'], ]);
To place an event where your visitor is (their country, device and visit), send it with the visitor's address and user agent:
$mettr->send([[ 'type' => 'event', 'name' => 'checkout', 'ip' => $_SERVER['REMOTE_ADDR'], 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null, ]]);
mettr uses the address to find the place and to tell visitors apart, then drops it. Behind a proxy or a CDN, send the visitor's address, not the proxy's.
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, and every subscription once for the history:
$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) $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 sendSubscriptions([...]), in batches of up to 100. One-off payments stay events: send([['type' => 'revenue', ...]]). More in Subscriptions API.
Failures
Nothing throws: event() and send() answer false when mettr refused a batch or could not be reached, so analytics never breaks the app sending them. Sending waits for mettr (5 seconds at most by default, timeout: to change it); send from a queue or after the response when that matters.
Another HTTP client can be handed in through the Mettr\Transport interface:
$mettr = new Mettr\Client($key, transport: new MyTransport);
Updated · This page as Markdown · Something unclear? [email protected]