Skip to main content

Lightweight Python SDK for tgram-analytics

Project description

tgram-analytics Python SDK

Lightweight Python SDK for tgram-analytics. Track events and pageviews from your Python backend.

Install

pip install tgram-analytics

Get a free proj_ API key from @MyTelegramAnalyticsBot on Telegram (1 project free), or self-host the server and create keys via your own bot.

Quick start (sync)

from tgram_analytics import TGA

with TGA("proj_xxx", "https://analytics.example.com") as tga:
    tga.track("signup", session_id="user-session-123", properties={"plan": "pro"})
    tga.pageview(session_id="user-session-123", url="/dashboard")

Quick start (async)

from tgram_analytics import AsyncTGA

async with AsyncTGA("proj_xxx", "https://analytics.example.com") as tga:
    tga.track("signup", session_id="user-session-123")
    await tga.flush()

Batching

Buffer events and send them in batches to reduce HTTP requests:

from tgram_analytics import TGA, BatchOptions

tga = TGA("proj_xxx", "https://analytics.example.com", batch=BatchOptions(max_size=20, max_wait=3.0))
# Events are buffered and auto-flushed when max_size is reached or max_wait seconds elapse
tga.track("click", "session-1")
tga.flush()  # manual flush
tga.close()  # flushes + closes HTTP client

Identifying users

Attach persistent properties to a session. All subsequent track() and pageview() calls for that session will include them:

tga.identify("session-123", {"plan": "pro", "locale": "en-US"})
tga.track("purchase", "session-123", {"amount": 49})
# sent properties: {"plan": "pro", "locale": "en-US", "amount": 49}

Per-event properties override identified properties when keys conflict.

Call tga.forget("session-123") to clear stored properties for a session.

API reference

TGA(api_key, server_url, *, batch=False, timeout=10.0)

Sync client. api_key must start with "proj_".

  • batchFalse (default), True (default thresholds), or BatchOptions(max_size=10, max_wait=5.0)
  • timeout — HTTP request timeout in seconds

AsyncTGA(api_key, server_url, *, batch=False, timeout=10.0)

Async client with the same constructor signature.

.track(event_name, session_id, properties=None)

Track a custom event. Fire-and-forget — errors are logged, never raised.

.pageview(session_id, url, referrer=None, properties=None)

Track a pageview event.

.identify(session_id, properties)

Store properties that are merged into all subsequent events for this session.

.forget(session_id)

Remove stored identify() properties for a session.

.flush()

Send all buffered events immediately. No-op if batching is disabled.

.close() / await .close()

Flush pending events and close the HTTP client.

Both clients support context managers (with / async with) for automatic cleanup.

Error handling

Analytics should never break your application. All HTTP and network errors are caught and logged via Python's logging module under the tgram_analytics logger:

import logging
logging.getLogger("tgram_analytics").setLevel(logging.DEBUG)

Only the constructor raises exceptions (on invalid api_key or missing server_url).

License

MIT

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

tgram_analytics-0.1.1.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

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

tgram_analytics-0.1.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file tgram_analytics-0.1.1.tar.gz.

File metadata

  • Download URL: tgram_analytics-0.1.1.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for tgram_analytics-0.1.1.tar.gz
Algorithm Hash digest
SHA256 067434c434e9482910e53a52dbc4a6b0c66b7d61b8619dd399da0e5b3fcc4a84
MD5 06516b48cd2e02459488f959b784eed3
BLAKE2b-256 465a34fb4f253a2d35c7bcaffa8c0d6d92803e063ecc6eb2b2ce0da555a116af

See more details on using hashes here.

File details

Details for the file tgram_analytics-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tgram_analytics-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e167dd0cf6cb7a258711677cc372a5f83f99b5c5ebade3cb66a0f5bcdc105c95
MD5 1be464ef4dd02384e382c367df8ca004
BLAKE2b-256 864977d05cb5889a1330fefe7481a863d034e4058bf7505c37837805bd4844b9

See more details on using hashes here.

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