mettr for Python
Live analytics for websites and SaaS products: who is on your site right now, what they do, where they came from and what broke, with no delay. This package sends a Python app's side of it to mettr: your product's own events, crawlers' visits (search engines and AI crawlers seldom run the browser script), 404s, exceptions, response times and subscriptions, and serves the browser script from your own domain.
pip install mettr
Python 3.10 or later, no dependencies. Django, FastAPI, Starlette and Flask have their middleware below.
Settings
The client reads the environment; arguments win.
| variable | what |
|---|---|
METTR_SECRET_KEY |
the site's secret key (mettr, 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 first-party path, _live by default; empty serves 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.
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
For MRR 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",
)
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"), # who signed in, from your auth
)
Each call an AI assistant makes is sent as an event: mcp.connect (the
handshake), mcp.tool, mcp.resource and mcp.prompt, with the tool's,
resource's or prompt's name, the client (Claude, ChatGPT, Cursor…), its
version, the protocol, how the call ended (ok, tool_error, error,
sign_in, forbidden, throttled, failed), the HTTP status of a
failure, the JSON-RPC error code and the time it took, streamed answers
included. Lists and pings are not sent. The MCP panels read them with
mettr's Pro plan.
The arguments and what came back are never sent; a failed call's message
only with errors=True (its first 200 characters), as it may quote what
the user's assistant sent. The client's address and user agent are not
sent: they are the assistant's servers', so mettr counts the person by
user (or a hash of the session). Requests through it are never sent as
crawler visits, 404s or response times by MettrMiddleware.
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")
What is sent on its own
- A crawler's request: its page and status, as a crawl (mettr names the crawler and checks it against its owner's network).
- A person's 404, as an error.
- An exception, by its class only, never its message; a client's error (a status under 500) is not one.
- One request in twenty's response time, by route.
- Nothing for
/up(ignore), for staff, or while mettr is off.
Sending never raises into your app: a failed batch answers False and is
told to on_error, without its events.
License
MIT
Metadata
Release files for mettr 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mettr-0.2.0.tar.gz | 132.6 kB | With provenance | Details for mettr-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mettr-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for mettr-0.2.0-py3-none-any.whl |
Total release size: 158.9 kB
Release files / mettr-0.2.0.tar.gz
| Download URL | mettr-0.2.0.tar.gz |
|---|---|
| Size | 132.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0a2958bf668ebfdea250fc1853925b1927dd3f62052bd805e0f704c5a53b706d
|
|
BLAKE2b-256 checksum How to use checksums |
019fff330cb0b798b45972b35e14b52f0f2ebcc674b2c91c256bb9d9c151e8f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logRelease files / mettr-0.2.0-py3-none-any.whl
| Download URL | mettr-0.2.0-py3-none-any.whl |
|---|---|
| Size | 26.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
845e53e65149d0d99d1434446483be4d5df15bee60a4923acd3d59e71957c831
|
|
BLAKE2b-256 checksum How to use checksums |
c98700a63cb1ef94d3c7e9baaaebee66ac7d944ba65e2ce58973e13391fa4609
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logSecurity for mettr 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mettr-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.