clipwright/cloud/analytics.py
juancarlos.cavero 51aafb5290 feat(analytics): report job outcomes to OpenPanel, with the user's job index
The browser reports the funnel, but not what decides whether this product
retains: on 26-jul-2026, 491 of the 564 users who ever processed a video did it
exactly once. Every clip-quality fix that shipped that day — 1080p delivery,
captions by default, loudness normalisation — was aimed at that 87%, and
nothing in the stack could measure whether it moved.

Client-side cannot answer it. A render finishes minutes later, often after the
tab is gone, and ad-blockers eat a share of the rest. So the backend now emits
ClipsDelivered / JobFailed with the user's profileId and a `job_index` — the
count of process jobs that user has run. Counting distinct users at index 1
versus index >= 2, before and after today, answers the question directly.
Failures carry the same reason classification the Telegram alerts use, so the
"did it break or did they just not come back" split is visible too.

Fire-and-forget throughout: no config means a silent no-op (the normal state
for self-hosted), every send is a background task, and every failure is
swallowed. Analytics must never slow down or break a job.

Needs OPENPANEL_CLIENT_ID and OPENPANEL_CLIENT_SECRET in the environment — a
WRITE client, which is a different credential from the read/root client the MCP
needs. Verified against the live instance: the write path returns 200 and the
module no-ops cleanly when unconfigured.
2026-07-28 21:49:40 +02:00

77 lines
2.7 KiB
Python

"""Server-side product events to OpenPanel.
The browser already reports the funnel (Signup, CheckoutStarted, Subscribed,
QuotaWall*, UpsellModal*). This module reports what the browser cannot be
trusted for:
* **Job outcomes.** Ad-blockers eat a chunk of client-side analytics, and the
interesting moment — a render finishing minutes later — often happens after
the tab is gone. Server-side is the only place these are complete.
* **Repeat usage.** The one number that decides whether the product retains:
on 26-jul-2026, 491 of 564 users who ever processed a video did it exactly
once. Every clip-quality fix shipped that day (1080p, captions, loudness) was
aimed at that 87%, and nothing in the stack could measure whether it moved.
``job_index`` on each event answers it directly: count distinct users at
index 1 versus index >= 2, before and after.
Fire-and-forget by design. Analytics must never slow down or break a job, so
every failure is swallowed and every send runs in the background.
"""
import asyncio
import os
import httpx
_TIMEOUT = 5.0
def _config():
"""(api_url, client_id, client_secret) or None when not configured.
Absent config is the normal state for self-hosted installs, so this stays
silent rather than warning.
"""
client_id = os.environ.get("OPENPANEL_CLIENT_ID", "").strip()
if not client_id:
return None
api = (os.environ.get("OPENPANEL_API_URL", "").strip()
or "https://api.openpanel.fotoexamen.com").rstrip("/")
return api, client_id, os.environ.get("OPENPANEL_CLIENT_SECRET", "").strip()
async def _post(payload):
cfg = _config()
if not cfg:
return
api, client_id, secret = cfg
headers = {"openpanel-client-id": client_id, "Content-Type": "application/json"}
if secret:
headers["openpanel-client-secret"] = secret
try:
async with httpx.AsyncClient(timeout=_TIMEOUT) as c:
await c.post(f"{api}/track", json=payload, headers=headers)
except Exception:
pass # analytics must never surface into a job
def track(name: str, user_id=None, **props):
"""Queue an event. Returns immediately; never raises.
``user_id`` becomes OpenPanel's profileId, which is what makes retention
and repeat-usage questions answerable per user.
"""
if not _config():
return
payload = {
"type": "track",
"payload": {
"name": name,
"properties": {k: v for k, v in props.items() if v is not None},
},
}
if user_id is not None:
payload["payload"]["profileId"] = str(user_id)
try:
asyncio.get_running_loop().create_task(_post(payload))
except RuntimeError:
pass # no loop (CLI/self-host path) — nothing to report to anyway