Python SDK Pro

Send events from any Python server runtime using a project API key. Works with Django, FastAPI, Flask, Celery workers, scheduled jobs, and anything else that runs Python and can make outbound HTTPS calls.

clamp-sh/analytics-pythonMIT

Paste to your agent

Wire Clamp into a Python backend
Adds clamp-analytics, initializes it on app startup, and tracks the project's primary conversion event from the relevant handler.

Install

terminal
pip install clamp-analytics

Python 3.9+ supported. The package depends only on httpx for the HTTP call.

Use an API key that starts with sk_proj_ from Settings → API Keys. Keys are scoped to one project. Set it as an environment variable; never commit it.

Quick start

app.py
from clamp_analytics import init, track, Money

init(project_id="proj_xxx", api_key=os.environ["CLAMP_API_KEY"])

# Simple event
track("signup", properties={"plan": "pro", "method": "email"})

# Link a server event to a browser visitor
track(
    "subscription_started",
    anonymous_id="aid_xxx",
    properties={"plan": "pro", "total": Money(29.00, "USD")},
)

API

init(project_id, api_key, endpoint=None)

Initializes the SDK. Call once at process startup. Stores config in module-level state; subsequent track() calls use it.

endpoint is optional and overrides the default https://api.clamp.sh. Use this for self-hosted Clamp deployments or integration testing.

track(name, properties=None, anonymous_id=None, timestamp=None)

Sends a server event. Returns True on success.

Raises ClampHTTPError on a non-2xx response or ClampNotInitializedError if init()wasn't called.

Money(amount, currency)

A typed monetary value. Use it for revenue, refunds, taxes — anywhere a currency-denominated amount belongs.

purchase.py
track("purchase", properties={
    "plan": "pro",
    "total": Money(29.00, "USD"),
    "tax": Money(4.35, "USD"),
})

amount is in major units (29.00, not 2900). currency is an ISO 4217 code (uppercase, three letters).

Framework integrations

Django

Initialize in settings.py so the singleton is ready for any view, signal, or management command.

myapp/settings.py
import os
from clamp_analytics import init

init(
    project_id=os.environ["CLAMP_PROJECT_ID"],
    api_key=os.environ["CLAMP_API_KEY"],
)
myapp/views.py
from clamp_analytics import track

def signup_complete(request):
    user = create_user(request)
    track("signup", anonymous_id=request.COOKIES.get("clamp_aid"), properties={
        "plan": user.plan,
        "method": "email",
    })
    return redirect("/welcome")

FastAPI

Initialize in the lifespan handler so the SDK is ready before the first request.

main.py
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from clamp_analytics import init, track

@asynccontextmanager
async def lifespan(app: FastAPI):
    init(
        project_id=os.environ["CLAMP_PROJECT_ID"],
        api_key=os.environ["CLAMP_API_KEY"],
    )
    yield

app = FastAPI(lifespan=lifespan)

@app.post("/checkout")
async def checkout(payload: CheckoutPayload):
    session = create_stripe_session(payload)
    track("checkout_started", anonymous_id=payload.anon_id, properties={
        "plan": payload.plan,
    })
    return {"url": session.url}

Flask

Initialize in the app factory.

app.py
import os
from flask import Flask
from clamp_analytics import init, track

def create_app():
    app = Flask(__name__)
    init(
        project_id=os.environ["CLAMP_PROJECT_ID"],
        api_key=os.environ["CLAMP_API_KEY"],
    )
    return app

Celery / background workers

For high-throughput webhook handlers, defer the track() call to a background task so the request handler returns fast.

tasks.py
from celery import Celery
from clamp_analytics import init, track, Money

celery = Celery(__name__)

@celery.task
def report_subscription_started(plan, amount, currency, anonymous_id):
    init(...)  # idempotent; called once per worker
    track("subscription_started", anonymous_id=anonymous_id, properties={
        "plan": plan,
        "total": Money(amount, currency),
    })

Errors and retries

The SDK is synchronous and raises on failure. There are no automatic retries. If you want fire-and-forget behaviour, wrap the call yourself:

safe_track.py
import logging
from clamp_analytics import track, ClampError

def safe_track(*args, **kwargs):
    try:
        return track(*args, **kwargs)
    except ClampError:
        logging.exception("failed to send to Clamp")

Webhook handlers that absolutely must not block on Clamp should defer to a Celery / RQ / Dramatiq queue.

Linking browser and server events

Pass the anonymous ID from the browser to your API, then include it in server-side calls. Events from both sides then share a visitor. See the linking pattern on the server-side docs page; the same pattern applies in Python.