Skip to main content

Server-side analytics SDK for Clamp. Send tracked events from Python apps (Django, FastAPI, Flask, etc.).

Project description

clamp-analytics

Server-side analytics SDK for Clamp Analytics in Python.

Send tracked events from any Python server runtime to Clamp. Works with Django, FastAPI, Flask, Celery workers, scheduled jobs, and anything else that runs Python and can make outbound HTTPS calls.

Install

pip install clamp-analytics

Python 3.9+ supported.

Quick start

from clamp_analytics import init, track, Money

init(project_id="proj_xxx", api_key="sk_proj_xxx")

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

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

Get a server API key at https://clamp.sh/dashboard (Settings → API Keys, format sk_proj_...). Set it as an environment variable; never commit it.

API

init(project_id, api_key, endpoint=None)

Initializes the SDK. Call once at process startup (e.g. in your Django settings.py, FastAPI lifespan, Flask app factory). 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.

  • name: event name string. Examples: "signup", "subscription_started", "feature_used".
  • properties: optional dict. Values may be str, int, float, bool, or Money. No nested dicts (other than Money) and no arrays.
  • anonymous_id: optional string. Links the server event to a browser visitor. Read the browser's anonymous ID via the JS SDK's getAnonymousId() and pass it through your auth flow (e.g. Stripe's client_reference_id).
  • timestamp: optional. Pass a datetime (timezone-aware preferred; naive datetimes are assumed UTC) or an ISO 8601 string. If omitted, the SDK uses the current UTC time.

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.

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

Per-framework integration patterns (Django middleware, FastAPI dependency, Flask after_request, Celery task hook) are documented at https://clamp.sh/docs/sdk/python.

Errors

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

import logging

try:
    track("subscription_started", properties={...})
except Exception:
    logging.exception("failed to send to Clamp")

For high-throughput webhook handlers, consider sending events through a background task queue (Celery, RQ, Dramatiq).

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clamp_analytics-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clamp_analytics-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file clamp_analytics-0.1.0.tar.gz.

File metadata

  • Download URL: clamp_analytics-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for clamp_analytics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 feac5bee33e4b216f035b02c42cf9cd84aee3c709f8f2b08401f4a7789552ba1
MD5 3e0f0d3f0e854340145700d5940290ca
BLAKE2b-256 049314644878e0db53c035cff55f36bf91adc0868a1eaaa08da4bef4c5730277

See more details on using hashes here.

Provenance

The following attestation bundles were made for clamp_analytics-0.1.0.tar.gz:

Publisher: sdk-python-release.yml on clamp-sh/clamp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clamp_analytics-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for clamp_analytics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f59da69ad95441d7d233446900e4836080c66b5bdf2ba2ab5ea6e30f4459c84c
MD5 91f36b69f736d78ded0318fa9718d700
BLAKE2b-256 b950d2a13c29e2870fa3d4582001a7e74b795bd6277f35a3756e554118cc5f5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for clamp_analytics-0.1.0-py3-none-any.whl:

Publisher: sdk-python-release.yml on clamp-sh/clamp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page