# 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.

Source: https://status.mettr.app/docs/php

PHP 8.3 or later, with ext-curl. The source is on [GitHub](https://github.com/mettrhq/mettr-php), MIT licensed. For Laravel, [mettr/laravel](/docs/laravel) does more: page views, errors, sign-ins, a queue, and a first-party route for the [browser script](/docs/browser-script).

## Install

```bash
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:

```php
$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](/docs/events-api) takes them (sent in batches of up to 100):

```php
$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:

```php
$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, Paddle or Polar](/docs/revenue), which mettr reads itself), send each subscription as it is now, whenever it changes, 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) $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](/docs/subscriptions-api).

## Your MCP server

If your app runs an MCP server, hand each request to it and its answer to `Mettr\Mcp` once the answer has gone (under PHP-FPM, after `fastcgi_finish_request()`):

```php
$mcp = new Mettr\Mcp($mettr);   // new Mettr\Mcp($mettr, errors: true) keeps failed calls' messages

$started = microtime(true);
$response = $server->handle($request);   // any MCP server, PSR-7 here

$mcp->record(
    (string) $request->getBody(),
    $response->getStatusCode(),
    (string) $response->getBody(),
    (microtime(true) - $started) * 1000,
    $userId,                              // the signed-in user, or null
    $request->getHeaders(),
    $response->getHeaders(),
);
```

Each handshake, tool call, resource read and prompt becomes an `mcp.*` event with the client, how it ended (a 401, 403 or 429 included) and how long it took; never the arguments or the answer. A streamed answer is read for its outcome too. More in [MCP analytics](/docs/mcp-analytics).

## 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:

```php
$mettr = new Mettr\Client($key, transport: new MyTransport);
```
