|
| 1 | +# Aptabase Python SDK |
| 2 | + |
| 3 | +Python SDK for [Aptabase](https://aptabase.com/) - privacy-first analytics for mobile, desktop and web applications. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🚀 **Fully async** - Built with `httpx` and `asyncio` |
| 8 | +- 🔒 **Privacy-first** - No personal data collection |
| 9 | +- 🏃 **Modern Python** - Requires Python 3.11+ |
| 10 | +- 📦 **Type-safe** - Full typing support |
| 11 | +- 🔄 **Auto-batching** - Efficient event batching and flushing |
| 12 | +- ⚡ **Lightweight** - Minimal dependencies |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +uv add aptabase |
| 18 | +# or |
| 19 | +pip install aptabase |
| 20 | +``` |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +```python |
| 25 | +import asyncio |
| 26 | +from aptabase import Aptabase |
| 27 | + |
| 28 | +async def main(): |
| 29 | + async with Aptabase("A-EU-1234567890") as client: |
| 30 | + # Track a simple event |
| 31 | + await client.track("app_started") |
| 32 | + |
| 33 | + # Track an event with properties |
| 34 | + await client.track("user_action", { |
| 35 | + "action": "button_click", |
| 36 | + "button_id": "login", |
| 37 | + "screen": "home" |
| 38 | + }) |
| 39 | + |
| 40 | + # Events are automatically flushed, but you can force it |
| 41 | + await client.flush() |
| 42 | + |
| 43 | +asyncio.run(main()) |
| 44 | +``` |
| 45 | + |
| 46 | +## Configuration |
| 47 | + |
| 48 | +```python |
| 49 | +client = Aptabase( |
| 50 | + app_key="A-EU-1234567890", # Your Aptabase app key |
| 51 | + app_version="1.2.3", # Your app version |
| 52 | + is_debug=False, # Enable debug mode |
| 53 | + max_batch_size=25, # Max events per batch (max 25) |
| 54 | + flush_interval=10.0, # Auto-flush interval in seconds |
| 55 | + timeout=30.0 # HTTP timeout in seconds |
| 56 | +) |
| 57 | +``` |
| 58 | + |
| 59 | +## App Key Format |
| 60 | + |
| 61 | +Your app key determines the server region: |
| 62 | +- `A-EU-*` - European servers |
| 63 | +- `A-US-*` - US servers |
| 64 | + |
| 65 | +Get your app key from the [Aptabase dashboard](https://aptabase.com/). |
| 66 | + |
| 67 | +## Event Tracking |
| 68 | + |
| 69 | +### Simple Events |
| 70 | + |
| 71 | +```python |
| 72 | +await client.track("page_view") |
| 73 | +``` |
| 74 | + |
| 75 | +### Events with Properties |
| 76 | + |
| 77 | +```python |
| 78 | +await client.track("purchase", { |
| 79 | + "product_id": "abc123", |
| 80 | + "price": 29.99, |
| 81 | + "currency": "USD" |
| 82 | +}) |
| 83 | +``` |
| 84 | + |
| 85 | +### Session Management |
| 86 | + |
| 87 | +```python |
| 88 | +# Set a custom session ID |
| 89 | +client.set_session_id("my-custom-session-id") |
| 90 | + |
| 91 | +# Track events with specific session |
| 92 | +await client.track("login", session_id="user-session-123") |
| 93 | +``` |
| 94 | + |
| 95 | +## Context Manager vs Manual Lifecycle |
| 96 | + |
| 97 | +### Recommended: Context Manager |
| 98 | + |
| 99 | +```python |
| 100 | +async with Aptabase("A-EU-1234567890") as client: |
| 101 | + await client.track("event") |
| 102 | + # Automatically handles start/stop and flushing |
| 103 | +``` |
| 104 | + |
| 105 | +### Manual Lifecycle |
| 106 | + |
| 107 | +```python |
| 108 | +client = Aptabase("A-EU-1234567890") |
| 109 | +await client.start() |
| 110 | +try: |
| 111 | + await client.track("event") |
| 112 | +finally: |
| 113 | + await client.stop() # Ensures all events are flushed |
| 114 | +``` |
| 115 | + |
| 116 | +## Error Handling |
| 117 | + |
| 118 | +```python |
| 119 | +from aptabase import Aptabase, AptabaseError, NetworkError |
| 120 | + |
| 121 | +try: |
| 122 | + async with Aptabase("A-EU-1234567890") as client: |
| 123 | + await client.track("event") |
| 124 | +except NetworkError as e: |
| 125 | + print(f"Network error: {e}, status: {e.status_code}") |
| 126 | +except AptabaseError as e: |
| 127 | + print(f"Aptabase error: {e}") |
| 128 | +``` |
| 129 | + |
| 130 | +## Development |
| 131 | + |
| 132 | +Install development dependencies: |
| 133 | + |
| 134 | +```bash |
| 135 | +uv sync --dev |
| 136 | +``` |
| 137 | + |
| 138 | +Run tests: |
| 139 | + |
| 140 | +```bash |
| 141 | +uv run pytest |
| 142 | +``` |
| 143 | + |
| 144 | +Code formatting: |
| 145 | + |
| 146 | +```bash |
| 147 | +uv run black . |
| 148 | +uv run ruff check . |
| 149 | +``` |
| 150 | + |
| 151 | +Type checking: |
| 152 | + |
| 153 | +```bash |
| 154 | +uv run mypy . |
| 155 | +``` |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +MIT License - see [LICENSE](LICENSE) file for details. |
0 commit comments