mettr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mettr-0.1.0/.github/workflows/ci.yml +41 -0
- mettr-0.1.0/.github/workflows/publish.yml +56 -0
- mettr-0.1.0/.gitignore +10 -0
- mettr-0.1.0/CLAUDE.md +35 -0
- mettr-0.1.0/LICENSE +21 -0
- mettr-0.1.0/PKG-INFO +175 -0
- mettr-0.1.0/README.md +147 -0
- mettr-0.1.0/pyproject.toml +84 -0
- mettr-0.1.0/sdk-contract.json +41 -0
- mettr-0.1.0/src/mettr/__init__.py +23 -0
- mettr-0.1.0/src/mettr/_client.py +464 -0
- mettr-0.1.0/src/mettr/_contract.py +53 -0
- mettr-0.1.0/src/mettr/_ip.py +52 -0
- mettr-0.1.0/src/mettr/_transport.py +48 -0
- mettr-0.1.0/src/mettr/django/__init__.py +13 -0
- mettr-0.1.0/src/mettr/django/_client.py +89 -0
- mettr-0.1.0/src/mettr/django/apps.py +35 -0
- mettr-0.1.0/src/mettr/django/middleware.py +94 -0
- mettr-0.1.0/src/mettr/django/templatetags/__init__.py +0 -0
- mettr-0.1.0/src/mettr/django/templatetags/mettr.py +14 -0
- mettr-0.1.0/src/mettr/flask.py +110 -0
- mettr-0.1.0/src/mettr/py.typed +0 -0
- mettr-0.1.0/src/mettr/starlette.py +160 -0
- mettr-0.1.0/tests/__init__.py +0 -0
- mettr-0.1.0/tests/conftest.py +70 -0
- mettr-0.1.0/tests/django_settings.py +24 -0
- mettr-0.1.0/tests/django_urls.py +35 -0
- mettr-0.1.0/tests/test_client.py +407 -0
- mettr-0.1.0/tests/test_contract.py +47 -0
- mettr-0.1.0/tests/test_django.py +194 -0
- mettr-0.1.0/tests/test_flask.py +132 -0
- mettr-0.1.0/tests/test_ip.py +33 -0
- mettr-0.1.0/tests/test_starlette.py +168 -0
- mettr-0.1.0/tests/test_transport.py +59 -0
- mettr-0.1.0/uv.lock +1576 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
# Read only: no job writes to the repository.
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
tests:
|
|
14
|
+
name: Lint, types and tests (100% coverage) on Python ${{ matrix.python }}
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
strategy:
|
|
17
|
+
matrix:
|
|
18
|
+
python: ['3.10', '3.13']
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- name: Checkout
|
|
22
|
+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
23
|
+
with:
|
|
24
|
+
persist-credentials: false
|
|
25
|
+
|
|
26
|
+
- name: Setup uv
|
|
27
|
+
uses: astral-sh/setup-uv@36300bab3f61b84e19f32b6bd26b7897f3c7e951 # v10.4.2
|
|
28
|
+
with:
|
|
29
|
+
python-version: ${{ matrix.python }}
|
|
30
|
+
|
|
31
|
+
- name: Install
|
|
32
|
+
run: uv sync --locked
|
|
33
|
+
|
|
34
|
+
- name: Lint
|
|
35
|
+
run: uv run ruff check src tests && uv run ruff format --check src tests
|
|
36
|
+
|
|
37
|
+
- name: Types
|
|
38
|
+
run: uv run mypy
|
|
39
|
+
|
|
40
|
+
- name: Tests with coverage (100%)
|
|
41
|
+
run: uv run pytest
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Publishes mettr to PyPI when a vX.Y.Z tag is pushed, with Trusted
|
|
2
|
+
# Publishing (no token stored anywhere).
|
|
3
|
+
#
|
|
4
|
+
# One-time setup on pypi.org → Your projects (or Publishing, for a pending
|
|
5
|
+
# publisher before the first release) → Add a trusted publisher → GitHub:
|
|
6
|
+
# Owner: mettrhq Repository: mettr-python
|
|
7
|
+
# Workflow: publish.yml Environment: pypi
|
|
8
|
+
# and a "pypi" environment in the repository's settings, with the user as
|
|
9
|
+
# a required reviewer, so each release waits for approval.
|
|
10
|
+
#
|
|
11
|
+
# Releasing: bump "version" in pyproject.toml and VERSION in
|
|
12
|
+
# src/mettr/_client.py, commit, then
|
|
13
|
+
# git tag v0.1.0 && git push origin v0.1.0
|
|
14
|
+
|
|
15
|
+
name: Publish
|
|
16
|
+
|
|
17
|
+
on:
|
|
18
|
+
push:
|
|
19
|
+
tags: ['v*']
|
|
20
|
+
|
|
21
|
+
permissions:
|
|
22
|
+
contents: read
|
|
23
|
+
|
|
24
|
+
jobs:
|
|
25
|
+
publish:
|
|
26
|
+
name: Publish on PyPI
|
|
27
|
+
runs-on: ubuntu-latest
|
|
28
|
+
environment: pypi
|
|
29
|
+
permissions:
|
|
30
|
+
contents: read
|
|
31
|
+
id-token: write
|
|
32
|
+
|
|
33
|
+
steps:
|
|
34
|
+
- name: Checkout
|
|
35
|
+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
36
|
+
with:
|
|
37
|
+
persist-credentials: false
|
|
38
|
+
|
|
39
|
+
- name: Setup uv
|
|
40
|
+
uses: astral-sh/setup-uv@36300bab3f61b84e19f32b6bd26b7897f3c7e951 # v10.4.2
|
|
41
|
+
|
|
42
|
+
- name: Tag matches the version
|
|
43
|
+
env:
|
|
44
|
+
TAG: ${{ github.ref_name }}
|
|
45
|
+
run: |
|
|
46
|
+
test "$TAG" = "v$(uv version --short)"
|
|
47
|
+
grep -q "VERSION = \"${TAG#v}\"" src/mettr/_client.py
|
|
48
|
+
|
|
49
|
+
- name: Lint, types and tests
|
|
50
|
+
run: uv sync --locked && uv run ruff check src tests && uv run mypy && uv run pytest
|
|
51
|
+
|
|
52
|
+
- name: Build
|
|
53
|
+
run: uv build
|
|
54
|
+
|
|
55
|
+
- name: Publish
|
|
56
|
+
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
|
mettr-0.1.0/.gitignore
ADDED
mettr-0.1.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
The PyPI package `mettr` (mettr is always lower case): mettr for Python,
|
|
4
|
+
with Django, FastAPI/Starlette and Flask middleware. Part of the mettr
|
|
5
|
+
project folder (`~/Projects/mettr`): the app is `../www` (read its
|
|
6
|
+
CLAUDE.md and `docs/spec.md`, section 6, for what an SDK does), the
|
|
7
|
+
Laravel SDK `../laravel` is the reference for what a server SDK sends,
|
|
8
|
+
`../js` has `@mettr/node`, built the same way.
|
|
9
|
+
|
|
10
|
+
- Public, MIT. Repo `mettrhq/mettr-python`.
|
|
11
|
+
- Runs in customers' apps: Python 3.10+, no dependencies (the transport is
|
|
12
|
+
`urllib`, never following a redirect, TLS verified). Never raises into the
|
|
13
|
+
app: a failed batch answers False and is told to `on_error` without its
|
|
14
|
+
events. Events go from a daemon `threading.Timer` after `flush_interval`;
|
|
15
|
+
`atexit` sends what is left.
|
|
16
|
+
- `mettr/_client.py` holds what every framework shares (`finished`,
|
|
17
|
+
`proxy_script`, `proxy_events`, `script_tag`, `exception`, which skips a
|
|
18
|
+
status under 500); `django/`, `starlette.py` and `flask.py` only read the
|
|
19
|
+
request and call it. Django is L3: sign-ins, sign-ups, `is_staff`.
|
|
20
|
+
- `sdk-contract.json` is a copy of the app's `resources/sdk-contract.json`;
|
|
21
|
+
`tests/test_contract.py` holds `_contract.py` to it. Copy it again when
|
|
22
|
+
the app's changes.
|
|
23
|
+
- Gates: `uv run ruff check src tests`, `uv run ruff format --check src
|
|
24
|
+
tests`, `uv run mypy` (strict), `uv run pytest` (100% line and branch
|
|
25
|
+
coverage). CI on Python 3.10 and 3.13.
|
|
26
|
+
- Published by `.github/workflows/publish.yml` on a `v*` tag through PyPI
|
|
27
|
+
Trusted Publishing, behind a `pypi` environment the user approves. The
|
|
28
|
+
version lives in `pyproject.toml` and `VERSION` (a test holds them
|
|
29
|
+
together). Never publish.
|
|
30
|
+
|
|
31
|
+
## Working with the user
|
|
32
|
+
|
|
33
|
+
Same rules as the app: reply in Russian; code, docs and commits in English;
|
|
34
|
+
never `git push` or publish; commit on `main` after each logical block,
|
|
35
|
+
Conventional Commits, no Claude attribution; Edit tool for edits.
|
mettr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Babintsev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mettr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mettr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Live analytics for websites and SaaS products: mettr for Python, with Django, FastAPI, Starlette and Flask middleware
|
|
5
|
+
Project-URL: Homepage, https://mettr.app
|
|
6
|
+
Project-URL: Documentation, https://mettr.app/docs/python
|
|
7
|
+
Project-URL: Repository, https://github.com/mettrhq/mettr-python
|
|
8
|
+
Author: mettr
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: analytics,crawlers,dashboard,django,fastapi,flask,live,privacy,realtime
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Framework :: Flask
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Site Management
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Provides-Extra: django
|
|
22
|
+
Requires-Dist: django>=4.2; extra == 'django'
|
|
23
|
+
Provides-Extra: flask
|
|
24
|
+
Requires-Dist: flask>=3.0; extra == 'flask'
|
|
25
|
+
Provides-Extra: starlette
|
|
26
|
+
Requires-Dist: starlette>=0.37; extra == 'starlette'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# mettr for Python
|
|
30
|
+
|
|
31
|
+
Live analytics for websites and SaaS products: who is on your site right
|
|
32
|
+
now, what they do, where they came from and what broke, with no delay.
|
|
33
|
+
This package sends a Python app's side of it to [mettr](https://mettr.app):
|
|
34
|
+
your product's own events, crawlers' visits (search engines and AI
|
|
35
|
+
crawlers seldom run the browser script), 404s, exceptions, response times
|
|
36
|
+
and subscriptions, and serves the browser script from your own domain.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install mettr
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Python 3.10 or later, no dependencies. Django, FastAPI, Starlette and
|
|
43
|
+
Flask have their middleware below.
|
|
44
|
+
|
|
45
|
+
## Settings
|
|
46
|
+
|
|
47
|
+
The client reads the environment; arguments win.
|
|
48
|
+
|
|
49
|
+
| variable | what |
|
|
50
|
+
|---|---|
|
|
51
|
+
| `METTR_SECRET_KEY` | the site's secret key (mettr, the site's settings, Tracking); keep it on the server |
|
|
52
|
+
| `METTR_PUBLIC_KEY` | the site's public key, for the script tag and the proxy |
|
|
53
|
+
| `METTR_ENABLED` | `true` or `false`; by default on once a key is set (Django: off while `DEBUG`) |
|
|
54
|
+
| `METTR_PROXY_PATH` | the first-party path, `_live` by default; empty serves the script from mettr |
|
|
55
|
+
| `METTR_ENDPOINT` | where mettr runs, `https://mettr.app` |
|
|
56
|
+
|
|
57
|
+
## Django
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
INSTALLED_APPS = [..., "mettr.django"]
|
|
61
|
+
|
|
62
|
+
MIDDLEWARE = [
|
|
63
|
+
...,
|
|
64
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
65
|
+
"mettr.django.MettrMiddleware", # after auth, so events carry the user
|
|
66
|
+
...,
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
METTR = {"SECRET_KEY": env("METTR_SECRET_KEY"), "PUBLIC_KEY": env("METTR_PUBLIC_KEY")}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```django
|
|
73
|
+
{% load mettr %}
|
|
74
|
+
<head>{% mettr_script %}</head>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Sign-ins and sign-ups are sent as `login` and `signup`, staff
|
|
78
|
+
(`user.is_staff`) send nothing, and an app behind its own proxy sets
|
|
79
|
+
`"TRUST_FORWARDED": True`. Sync and async views alike.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from mettr.django import get_mettr, visitor
|
|
83
|
+
|
|
84
|
+
get_mettr().event("export", {"format": "csv"}, visitor(request))
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## FastAPI and Starlette
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from mettr import Mettr
|
|
91
|
+
from mettr.starlette import MettrMiddleware, visitor
|
|
92
|
+
|
|
93
|
+
mettr = Mettr()
|
|
94
|
+
app.add_middleware(MettrMiddleware, mettr=mettr)
|
|
95
|
+
|
|
96
|
+
@app.post("/signup")
|
|
97
|
+
def signup(request: Request):
|
|
98
|
+
request.state.mettr_user_id = user.id # the request's events carry it
|
|
99
|
+
mettr.event("signup", {"plan": "pro"}, visitor(request))
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Behind a proxy of your own, pass `trust_forwarded=True` (or run uvicorn
|
|
103
|
+
with `--proxy-headers`). In a Jinja template: `{{ mettr.script_tag()|safe }}`.
|
|
104
|
+
|
|
105
|
+
## Flask
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from mettr.flask import MettrFlask
|
|
109
|
+
|
|
110
|
+
mettr = MettrFlask(app) # or MettrFlask().init_app(app)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```jinja
|
|
114
|
+
<head>{{ mettr_script() }}</head>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Flask-Login's user is read when it is installed; pass `user=` and `staff=`
|
|
118
|
+
otherwise. Behind a proxy, wrap the app in `ProxyFix`.
|
|
119
|
+
|
|
120
|
+
## Your own events
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from mettr import Mettr, Visitor
|
|
124
|
+
|
|
125
|
+
mettr = Mettr()
|
|
126
|
+
mettr.event("signup", {"plan": "pro"}, Visitor(user_id="42"))
|
|
127
|
+
mettr.revenue("order", 49.5, "EUR", {"items": 2})
|
|
128
|
+
mettr.error("PaymentFailed", {"provider": "stripe"})
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Props are flat: up to 30, each a string, number, boolean or null. Events
|
|
132
|
+
wait a second to go together, sent from a background thread; a serverless
|
|
133
|
+
function (or a script) calls `mettr.flush()` before it ends.
|
|
134
|
+
|
|
135
|
+
## Subscriptions
|
|
136
|
+
|
|
137
|
+
For MRR when your billing is your own (anything but Stripe, Paddle or
|
|
138
|
+
Polar, which mettr reads itself), send each subscription whenever it
|
|
139
|
+
changes, and every one once for the history:
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
mettr.subscription(
|
|
143
|
+
"sub_1",
|
|
144
|
+
"active", # active, past_due, trialing, paused, canceled
|
|
145
|
+
19, # each cycle, before tax, in the main unit
|
|
146
|
+
"USD",
|
|
147
|
+
interval="month", # day, week, month, year
|
|
148
|
+
user_id="42",
|
|
149
|
+
plan="Pro",
|
|
150
|
+
)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## History and deploys
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
mettr.import_events(rows) # each with ts and source_id: a second run adds nothing
|
|
157
|
+
mettr.annotation("Deployed v2.4", url="https://github.com/acme/app/releases/v2.4")
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## What is sent on its own
|
|
161
|
+
|
|
162
|
+
- A crawler's request: its page and status, as a crawl (mettr names the
|
|
163
|
+
crawler and checks it against its owner's network).
|
|
164
|
+
- A person's 404, as an error.
|
|
165
|
+
- An exception, by its class only, never its message; a client's error
|
|
166
|
+
(a status under 500) is not one.
|
|
167
|
+
- One request in twenty's response time, by route.
|
|
168
|
+
- Nothing for `/up` (`ignore`), for staff, or while mettr is off.
|
|
169
|
+
|
|
170
|
+
Sending never raises into your app: a failed batch answers `False` and is
|
|
171
|
+
told to `on_error`, without its events.
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT
|
mettr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# mettr for Python
|
|
2
|
+
|
|
3
|
+
Live analytics for websites and SaaS products: who is on your site right
|
|
4
|
+
now, what they do, where they came from and what broke, with no delay.
|
|
5
|
+
This package sends a Python app's side of it to [mettr](https://mettr.app):
|
|
6
|
+
your product's own events, crawlers' visits (search engines and AI
|
|
7
|
+
crawlers seldom run the browser script), 404s, exceptions, response times
|
|
8
|
+
and subscriptions, and serves the browser script from your own domain.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install mettr
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Python 3.10 or later, no dependencies. Django, FastAPI, Starlette and
|
|
15
|
+
Flask have their middleware below.
|
|
16
|
+
|
|
17
|
+
## Settings
|
|
18
|
+
|
|
19
|
+
The client reads the environment; arguments win.
|
|
20
|
+
|
|
21
|
+
| variable | what |
|
|
22
|
+
|---|---|
|
|
23
|
+
| `METTR_SECRET_KEY` | the site's secret key (mettr, the site's settings, Tracking); keep it on the server |
|
|
24
|
+
| `METTR_PUBLIC_KEY` | the site's public key, for the script tag and the proxy |
|
|
25
|
+
| `METTR_ENABLED` | `true` or `false`; by default on once a key is set (Django: off while `DEBUG`) |
|
|
26
|
+
| `METTR_PROXY_PATH` | the first-party path, `_live` by default; empty serves the script from mettr |
|
|
27
|
+
| `METTR_ENDPOINT` | where mettr runs, `https://mettr.app` |
|
|
28
|
+
|
|
29
|
+
## Django
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
INSTALLED_APPS = [..., "mettr.django"]
|
|
33
|
+
|
|
34
|
+
MIDDLEWARE = [
|
|
35
|
+
...,
|
|
36
|
+
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
|
37
|
+
"mettr.django.MettrMiddleware", # after auth, so events carry the user
|
|
38
|
+
...,
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
METTR = {"SECRET_KEY": env("METTR_SECRET_KEY"), "PUBLIC_KEY": env("METTR_PUBLIC_KEY")}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```django
|
|
45
|
+
{% load mettr %}
|
|
46
|
+
<head>{% mettr_script %}</head>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Sign-ins and sign-ups are sent as `login` and `signup`, staff
|
|
50
|
+
(`user.is_staff`) send nothing, and an app behind its own proxy sets
|
|
51
|
+
`"TRUST_FORWARDED": True`. Sync and async views alike.
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from mettr.django import get_mettr, visitor
|
|
55
|
+
|
|
56
|
+
get_mettr().event("export", {"format": "csv"}, visitor(request))
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## FastAPI and Starlette
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from mettr import Mettr
|
|
63
|
+
from mettr.starlette import MettrMiddleware, visitor
|
|
64
|
+
|
|
65
|
+
mettr = Mettr()
|
|
66
|
+
app.add_middleware(MettrMiddleware, mettr=mettr)
|
|
67
|
+
|
|
68
|
+
@app.post("/signup")
|
|
69
|
+
def signup(request: Request):
|
|
70
|
+
request.state.mettr_user_id = user.id # the request's events carry it
|
|
71
|
+
mettr.event("signup", {"plan": "pro"}, visitor(request))
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Behind a proxy of your own, pass `trust_forwarded=True` (or run uvicorn
|
|
75
|
+
with `--proxy-headers`). In a Jinja template: `{{ mettr.script_tag()|safe }}`.
|
|
76
|
+
|
|
77
|
+
## Flask
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from mettr.flask import MettrFlask
|
|
81
|
+
|
|
82
|
+
mettr = MettrFlask(app) # or MettrFlask().init_app(app)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```jinja
|
|
86
|
+
<head>{{ mettr_script() }}</head>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Flask-Login's user is read when it is installed; pass `user=` and `staff=`
|
|
90
|
+
otherwise. Behind a proxy, wrap the app in `ProxyFix`.
|
|
91
|
+
|
|
92
|
+
## Your own events
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from mettr import Mettr, Visitor
|
|
96
|
+
|
|
97
|
+
mettr = Mettr()
|
|
98
|
+
mettr.event("signup", {"plan": "pro"}, Visitor(user_id="42"))
|
|
99
|
+
mettr.revenue("order", 49.5, "EUR", {"items": 2})
|
|
100
|
+
mettr.error("PaymentFailed", {"provider": "stripe"})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Props are flat: up to 30, each a string, number, boolean or null. Events
|
|
104
|
+
wait a second to go together, sent from a background thread; a serverless
|
|
105
|
+
function (or a script) calls `mettr.flush()` before it ends.
|
|
106
|
+
|
|
107
|
+
## Subscriptions
|
|
108
|
+
|
|
109
|
+
For MRR when your billing is your own (anything but Stripe, Paddle or
|
|
110
|
+
Polar, which mettr reads itself), send each subscription whenever it
|
|
111
|
+
changes, and every one once for the history:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
mettr.subscription(
|
|
115
|
+
"sub_1",
|
|
116
|
+
"active", # active, past_due, trialing, paused, canceled
|
|
117
|
+
19, # each cycle, before tax, in the main unit
|
|
118
|
+
"USD",
|
|
119
|
+
interval="month", # day, week, month, year
|
|
120
|
+
user_id="42",
|
|
121
|
+
plan="Pro",
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## History and deploys
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
mettr.import_events(rows) # each with ts and source_id: a second run adds nothing
|
|
129
|
+
mettr.annotation("Deployed v2.4", url="https://github.com/acme/app/releases/v2.4")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## What is sent on its own
|
|
133
|
+
|
|
134
|
+
- A crawler's request: its page and status, as a crawl (mettr names the
|
|
135
|
+
crawler and checks it against its owner's network).
|
|
136
|
+
- A person's 404, as an error.
|
|
137
|
+
- An exception, by its class only, never its message; a client's error
|
|
138
|
+
(a status under 500) is not one.
|
|
139
|
+
- One request in twenty's response time, by route.
|
|
140
|
+
- Nothing for `/up` (`ignore`), for staff, or while mettr is off.
|
|
141
|
+
|
|
142
|
+
Sending never raises into your app: a failed batch answers `False` and is
|
|
143
|
+
told to `on_error`, without its events.
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mettr"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Live analytics for websites and SaaS products: mettr for Python, with Django, FastAPI, Starlette and Flask middleware"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
authors = [{ name = "mettr" }]
|
|
10
|
+
keywords = ["analytics", "realtime", "live", "dashboard", "privacy", "django", "fastapi", "flask", "crawlers"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Framework :: Django",
|
|
14
|
+
"Framework :: FastAPI",
|
|
15
|
+
"Framework :: Flask",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Internet :: WWW/HTTP :: Site Management",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
dependencies = []
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://mettr.app"
|
|
25
|
+
Documentation = "https://mettr.app/docs/python"
|
|
26
|
+
Repository = "https://github.com/mettrhq/mettr-python"
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
django = ["django>=4.2"]
|
|
30
|
+
starlette = ["starlette>=0.37"]
|
|
31
|
+
flask = ["flask>=3.0"]
|
|
32
|
+
|
|
33
|
+
[dependency-groups]
|
|
34
|
+
dev = [
|
|
35
|
+
"django>=5.2",
|
|
36
|
+
"starlette>=0.47",
|
|
37
|
+
"fastapi>=0.115",
|
|
38
|
+
"flask>=3.1",
|
|
39
|
+
"httpx>=0.28",
|
|
40
|
+
"pytest>=8.4",
|
|
41
|
+
"pytest-cov>=6.2",
|
|
42
|
+
"pytest-django>=4.11",
|
|
43
|
+
"mypy>=1.17",
|
|
44
|
+
"django-stubs>=5.2",
|
|
45
|
+
"ruff>=0.12",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[build-system]
|
|
49
|
+
requires = ["hatchling>=1.27"]
|
|
50
|
+
build-backend = "hatchling.build"
|
|
51
|
+
|
|
52
|
+
[tool.hatch.build.targets.wheel]
|
|
53
|
+
packages = ["src/mettr"]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
testpaths = ["tests"]
|
|
57
|
+
pythonpath = ["."]
|
|
58
|
+
DJANGO_SETTINGS_MODULE = "tests.django_settings"
|
|
59
|
+
addopts = "--cov=mettr --cov-branch --cov-report=term-missing --cov-fail-under=100"
|
|
60
|
+
|
|
61
|
+
[tool.coverage.run]
|
|
62
|
+
source = ["mettr"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff]
|
|
65
|
+
line-length = 120
|
|
66
|
+
target-version = "py310"
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["E", "F", "W", "I", "B", "UP", "S", "SIM", "RUF", "PT", "C4", "RET"]
|
|
70
|
+
ignore = ["S101"]
|
|
71
|
+
|
|
72
|
+
[tool.ruff.lint.per-file-ignores]
|
|
73
|
+
"tests/*" = ["S", "B017"]
|
|
74
|
+
|
|
75
|
+
[tool.mypy]
|
|
76
|
+
strict = true
|
|
77
|
+
python_version = "3.10"
|
|
78
|
+
mypy_path = "src"
|
|
79
|
+
packages = ["mettr"]
|
|
80
|
+
|
|
81
|
+
# Optional: read only when the app has it.
|
|
82
|
+
[[tool.mypy.overrides]]
|
|
83
|
+
module = ["flask_login"]
|
|
84
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"about": "What every mettr SDK shares (spec, section 6). Each SDK keeps a copy and tests itself against it; SdkContractTest holds this file to the app.",
|
|
3
|
+
"endpoint": "https://mettr.app",
|
|
4
|
+
"paths": {
|
|
5
|
+
"events": "/api/v1/events",
|
|
6
|
+
"import": "/api/v1/import",
|
|
7
|
+
"subscriptions": "/api/v1/subscriptions",
|
|
8
|
+
"annotations": "/api/v1/annotations",
|
|
9
|
+
"script": "/script.js"
|
|
10
|
+
},
|
|
11
|
+
"limits": {
|
|
12
|
+
"batch": 100,
|
|
13
|
+
"import_batch": 1000,
|
|
14
|
+
"subscriptions_batch": 100,
|
|
15
|
+
"props": 30,
|
|
16
|
+
"prop_length": 500
|
|
17
|
+
},
|
|
18
|
+
"subscription": {
|
|
19
|
+
"statuses": ["active", "trialing", "past_due", "paused", "canceled"],
|
|
20
|
+
"intervals": ["day", "week", "month", "year"]
|
|
21
|
+
},
|
|
22
|
+
"proxy": {
|
|
23
|
+
"path": "_live",
|
|
24
|
+
"max_bytes": 65536,
|
|
25
|
+
"max_events": 20,
|
|
26
|
+
"types": ["pageview", "event", "engagement", "vital", "presence"],
|
|
27
|
+
"fields": ["type", "name", "url", "referrer", "props", "automated"],
|
|
28
|
+
"script_cache_seconds": 3600,
|
|
29
|
+
"script_retry_seconds": 60
|
|
30
|
+
},
|
|
31
|
+
"timeout_seconds": 3,
|
|
32
|
+
"response_time_sample": 0.05,
|
|
33
|
+
"crawler_pattern": "bot|crawl|spider|slurp|facebookexternalhit|facebookcatalog|embedly|iframely|whatsapp|perplexity|chatgpt|claude|anthropic|cohere|diffbot|bytespider|meta-external|uptime|pingdom|statuscake|site24x7|checkly|screaming frog",
|
|
34
|
+
"cloudflare": [
|
|
35
|
+
"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18",
|
|
36
|
+
"108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17",
|
|
37
|
+
"162.158.0.0/15", "104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",
|
|
38
|
+
"2400:cb00::/32", "2606:4700::/32", "2803:f800::/32", "2405:b500::/32", "2405:8100::/32",
|
|
39
|
+
"2a06:98c0::/29", "2c0f:f248::/32"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""
|
|
2
|
+
mettr for Python: live analytics for websites and SaaS products. The client
|
|
3
|
+
sends the app's own events, crawlers' visits, 404s, exceptions and
|
|
4
|
+
subscriptions; mettr.django, mettr.starlette (FastAPI too) and mettr.flask
|
|
5
|
+
send what a request shows on their own.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from mettr._client import VERSION, Event, Mettr, Props, ProxyResponse, Visitor
|
|
9
|
+
from mettr._ip import forwarded_for, visitor_ip
|
|
10
|
+
from mettr._transport import Transport, UrllibTransport
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"VERSION",
|
|
14
|
+
"Event",
|
|
15
|
+
"Mettr",
|
|
16
|
+
"Props",
|
|
17
|
+
"ProxyResponse",
|
|
18
|
+
"Transport",
|
|
19
|
+
"UrllibTransport",
|
|
20
|
+
"Visitor",
|
|
21
|
+
"forwarded_for",
|
|
22
|
+
"visitor_ip",
|
|
23
|
+
]
|