Skip to content
New Bring your history: import it from Google Analytics, Plausible, Simple Analytics, Umami, Matomo or GoatCounter, on every plan

Python

The mettr package sends a Django, FastAPI, Starlette or Flask app's events, crawler visits, 404s, errors and response times, with no dependencies.

Python 3.10 or later, no dependencies. The source is on GitHub, MIT licensed.

Install

pip install mettr

The client reads the environment; arguments win.

Variable What
METTR_SECRET_KEY the site's secret key (the site's settings, Tracking); keep it on the server
METTR_PUBLIC_KEY the site's public key, for the script tag and the proxy
METTR_ENABLED true or false; by default on once a key is set (Django: off while DEBUG)
METTR_PROXY_PATH the path on your own domain, _live by default; empty loads the script from mettr
METTR_ENDPOINT where mettr runs, https://mettr.app

Django

INSTALLED_APPS = [..., "mettr.django"]

MIDDLEWARE = [
    ...,
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "mettr.django.MettrMiddleware",  # after auth, so events carry the user
    ...,
]

METTR = {"SECRET_KEY": env("METTR_SECRET_KEY"), "PUBLIC_KEY": env("METTR_PUBLIC_KEY")}
{% load mettr %}
<head>{% mettr_script %}</head>

Sign-ins and sign-ups are sent as login and signup, staff (user.is_staff) send nothing, and an app behind its own proxy sets "TRUST_FORWARDED": True. Sync and async views alike.

from mettr.django import get_mettr, visitor

get_mettr().event("export", {"format": "csv"}, visitor(request))

FastAPI and Starlette

from mettr import Mettr
from mettr.starlette import MettrMiddleware, visitor

mettr = Mettr()
app.add_middleware(MettrMiddleware, mettr=mettr)

@app.post("/signup")
def signup(request: Request):
    request.state.mettr_user_id = user.id  # the request's events carry it
    mettr.event("signup", {"plan": "pro"}, visitor(request))

Behind a proxy of your own, pass trust_forwarded=True (or run uvicorn with --proxy-headers). In a Jinja template: {{ mettr.script_tag()|safe }}.

Flask

from mettr.flask import MettrFlask

mettr = MettrFlask(app)  # or MettrFlask().init_app(app)
<head>{{ mettr_script() }}</head>

Flask-Login's user is read when it is installed; pass user= and staff= otherwise. Behind a proxy, wrap the app in ProxyFix.

The script and the proxy

The script tag loads the browser script from /_live/script.js and sends its events to /_live/events, which the middleware serves: ad blockers mostly leave first-party requests alone, and the events carry the visitor's address and user as your server saw them. Only what a browser may send passes.

What it sends on its own

  • Crawler visits: a crawler's request, with its page and status. mettr names the crawler and checks it against its owner's network.
  • 404s of people, as errors.
  • Exceptions, by their class only, never the message. A client's error (a status under 500) is not one.
  • Response times of one request in twenty, by route.
  • Nothing for /up (ignore), for staff, or while it is off.

Sending never raises into your app: a failed batch answers False and is passed to on_error, without its events.

Your own events

from mettr import Mettr, Visitor

mettr = Mettr()
mettr.event("signup", {"plan": "pro"}, Visitor(user_id="42"))
mettr.revenue("order", 49.5, "EUR", {"items": 2})
mettr.error("PaymentFailed", {"provider": "stripe"})

Props are flat: up to 30, each a string, number, boolean or null. Events wait a second to go together, sent from a background thread; a serverless function or a script calls mettr.flush() before it ends.

Subscriptions

When your billing is your own (anything but Stripe, Paddle or Polar, which mettr reads itself), send each subscription whenever it changes, and every one once for the history:

mettr.subscription(
    "sub_1",
    "active",  # active, past_due, trialing, paused, canceled
    19,  # each cycle, before tax, in the main unit
    "USD",
    interval="month",  # day, week, month, year
    user_id="42",
    plan="Pro",
)

See the Subscriptions API for what each field means.

Your MCP server

If you run an MCP server over HTTP (FastMCP's streamable_http_app() or sse_app(), or any other ASGI app), wrap it with McpMiddleware, outside its auth, so the 401 that sends a client to sign in is counted too:

from mettr.mcp import McpMiddleware

app = McpMiddleware(mcp.streamable_http_app(), mettr=mettr, user=lambda scope: scope["state"].get("user_id"))

Each tool, resource, prompt and handshake is sent as an mcp.* event with the client, how it ended and how long it took, streamed answers included; never the arguments or the answer, and a failed call's message only with errors=True. MettrMiddleware sends no crawler visit, 404 or response time for these requests. More in MCP analytics.

History and deploys

mettr.import_events(rows)  # each with ts and source_id: a second run adds nothing
mettr.annotation("Deployed v2.4", url="https://github.com/acme/app/releases/v2.4")

Rows are those of the Import API; notes are those of the Annotations API.

Updated · This page as Markdown · Something unclear? [email protected]

Search the docs, features, comparisons, blog and changelog