Skip to main content

MetricsFirst SDK for Python - Analytics for Telegram bots

Project description

MetricsFirst Python SDK

Official Python SDK for MetricsFirst - Analytics for Telegram bots.

Note: Commands and interactions are tracked automatically when you add your bot to MetricsFirst. This SDK is for tracking custom events like services, purchases, and errors.

Installation

# Basic installation (sync only)
pip install metricsfirst

# With async support
pip install metricsfirst[async]

Features

  • Fire-and-forget: All tracking calls are non-blocking and don't add latency
  • Background sending: Events are sent in a separate thread (sync) or task (async)
  • Error resilience: Errors are logged, never thrown to your code
  • Memory safe: Queue is limited to 1000 events to prevent memory issues

Quick Start

Synchronous Client

from metricsfirst import MetricsFirst, ServiceEventData

# Initialize
mf = MetricsFirst(
    bot_id="your_bot_id",
    api_key="your_api_key",
)

# Track a service (fire-and-forget, non-blocking)
mf.track_service(ServiceEventData(
    user_id=123456789,
    service_name="image_generation",
    is_free=False,
    price=10,
    currency="USD",
))

# Shutdown flushes remaining events
mf.shutdown()

Asynchronous Client

import asyncio
from metricsfirst import AsyncMetricsFirst, ServiceEventData

async def main():
    # Initialize
    mf = AsyncMetricsFirst(
        bot_id="your_bot_id",
        api_key="your_api_key",
    )
    await mf.start()

    # Track events (fire-and-forget)
    await mf.track_service(ServiceEventData(
        user_id=123456789,
        service_name="image_generation",
    ))

    # Shutdown
    await mf.shutdown()

asyncio.run(main())

Context Manager

# Sync
with MetricsFirst(bot_id="...", api_key="...") as mf:
    mf.track_service(...)

# Async
async with AsyncMetricsFirst(bot_id="...", api_key="...") as mf:
    await mf.track_service(...)

Available Events

Method Description
track_service() Track services provided
track_error() Track errors
track_error_from_exception() Track error from exception
track_purchase_initiated() Track purchase start
track_purchase_completed() Track successful purchase
track_purchase_error() Track failed purchase
track_recurring_charge_success() Track subscription charge
track_recurring_charge_failed() Track failed charge
identify() Identify user with properties

Configuration

mf = MetricsFirst(
    bot_id="your_bot_id",
    api_key="your_api_key",
    api_url="https://api.metricsfirst.com",  # Custom API URL
    batch_events=True,      # Batch events before sending
    batch_size=10,          # Events per batch
    batch_interval=5.0,     # Seconds between flushes
    debug=False,            # Enable debug logging
    timeout=10.0,           # HTTP timeout
)

License

MIT

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

metricsfirst-0.1.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

metricsfirst-0.1.2-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file metricsfirst-0.1.2.tar.gz.

File metadata

  • Download URL: metricsfirst-0.1.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for metricsfirst-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9f7d0d9aea39e49c92ccd202b3fb3fa30a7b78aaa61a601ec91162b4c6740197
MD5 b4e16dbd6bbe58e617dd92be711cfdbc
BLAKE2b-256 58e5254b75f9513f7c1d7c02a6a3bc5f9c7bfd720d44c9be6c83395297c8c735

See more details on using hashes here.

File details

Details for the file metricsfirst-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: metricsfirst-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for metricsfirst-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7ad634f595a45fa54eb8298d3f026a2dca700dc2694dae6a8def32bf2418d161
MD5 cc12dc375a5b31e2c988f354e5d07116
BLAKE2b-256 6d6bb1a84efc54c11807187431eab392d78bcb9b67ef9b6cadda7c3a8e918fb2

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