> ## Documentation Index
> Fetch the complete documentation index at: https://docs.telemetree.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Set up your Telemetree Python SDK

<img className="block dark:hidden" src="https://mintcdn.com/telemetree/akp6XNv3V6IKBA56/images/tele-hero-light.svg?fit=max&auto=format&n=akp6XNv3V6IKBA56&q=85&s=c92236583b01bcf1c327de87393c9b11" alt="Hero Light" width="700" height="320" data-path="images/tele-hero-light.svg" />

<img className="hidden dark:block" src="https://mintcdn.com/telemetree/akp6XNv3V6IKBA56/images/tele-hero-dark.svg?fit=max&auto=format&n=akp6XNv3V6IKBA56&q=85&s=6c753a4fcf02917cda0ae770ed798fe5" alt="Hero Dark" width="700" height="320" data-path="images/tele-hero-dark.svg" />

<Note>    [Github](https://github.com/TONSolutions/telemetree-python) | [PyPi](https://pypi.org/project/telemetree/)</Note>
Telemetree is a comprehensive free analytics tool designed specifically for **Telegram Mini Apps**. With our SDKs, developers, marketers, and product managers can easily track and optimize user engagement, making data-driven decisions to boost user acquisition and retention. Telemetree simplifies **Analytics for Telegram Mini Apps** by delivering insights into user behaviors, acquisition channels, and in-app interactions.

# Getting started

#### System Requirements

* pip package manager
* Python 3.7+
* A Telegram bot project set up with Python

#### Installation

Install Telemetree Python SDK using pip

<CodeGroup>
  ```bash pip theme={null}
  pip install telemetree
  ```
</CodeGroup>

This will add the Telemetree Python SDK to your current developer environment and you're ready to proceed to the next steps of configuration and event tracking.

# Initialization

To being using the Telemetree Python SDK in your application, you'll need to initialize it with your project's specific configuration. Initialization is a crucial step that allows the SDK to start capturing and sending analytics data.

#### Step-by-Step Initialization

1. Import the Telemetree SDK:
2. Initialize the SDK with your project ID and API key.
3. Connect the client to your webhook.

<CodeGroup>
  ```Python Initiate Library theme={null}
  from fastapi import APIRouter, Request
  from telegram import Update
  from telemetree import TelemetreeClient

  router = APIRouter()
  telemetree = TelemetreeClient(
      "your_api_key", "your_project_key"
  )
  ```

  ```Python Connect to Webhook theme={null}
  @router.post("/webhook")
  async def webhook(request: Request):
      """Telegram webhook endpoint."""
      incoming_request_json = await request.json()
      telemetree.track(incoming_request_json)
      de_json_object = Update.de_json(data=await request.json(), bot=application.bot)
      await application.update_queue.put(de_json_object)
  ```
</CodeGroup>

After wrapping your root component with `TwaAnalyticsProvider`, the SDK will automatically start capturing data based on your configuration.

# Event Tracking

Alternatively, you can pass on the Telegram Update dictionary directly

```Python theme={null}
event = {
    "update_id": 123456789,
    "message": {
        "message_id": 1,
        "from": {
            "id": 987654321,
            "is_bot": False,
            "first_name": "John",
            "last_name": "Doe",
            "username": "johndoe",
            "language_code": "en"
        },
        "chat": {
            "id": 987654321,
            "first_name": "John",
            "last_name": "Doe",
            "username": "johndoe",
            "type": "private"
        },
        "date": 1621234567,
        "text": "Hello, world!"
    }
}

response_status_code = client.track(event)
print(response_status_code)
```

# Configuration

The Telemetree Python SDK provides some configuration options that you can customize:

* `auto_capture_telegram`: Enables or disables automatic capturing of Telegram events (default: `True`)
* `auto_capture_telegram_events`: Specifies the types of Telegram events to capture automatically (default: `["message"]`)
* `auto_capture_commands`: Specifies the Telegram commands to capture automatically (default: `["/start", "/help"]`)

Other configuration options include the Telemetree API endpoint, encryption keys, and logging settings. You can modify these options either within the Telemetree dashboard or by updating the `config.py` file in the SDK.
