|
| 1 | +# klime-analytics |
| 2 | + |
| 3 | +Klime Analytics SDK for Python. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install klime-analytics |
| 9 | +``` |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```python |
| 14 | +from klime import KlimeClient |
| 15 | + |
| 16 | +client = KlimeClient( |
| 17 | + write_key='your-write-key' |
| 18 | +) |
| 19 | + |
| 20 | +# Track an event |
| 21 | +client.track('Button Clicked', { |
| 22 | + 'button_name': 'Sign up', |
| 23 | + 'plan': 'pro' |
| 24 | +}) |
| 25 | + |
| 26 | +# Identify a user |
| 27 | +client.identify('user_123', { |
| 28 | + 'email': 'user@example.com', |
| 29 | + 'name': 'Stefan' |
| 30 | +}) |
| 31 | + |
| 32 | +# Group a user with an organization |
| 33 | +client.group('org_456', { |
| 34 | + 'name': 'Acme Inc', |
| 35 | + 'plan': 'enterprise' |
| 36 | +}, user_id='user_123') |
| 37 | + |
| 38 | +# Shutdown gracefully |
| 39 | +client.shutdown() |
| 40 | +``` |
| 41 | + |
| 42 | +## API Reference |
| 43 | + |
| 44 | +### Constructor |
| 45 | + |
| 46 | +```python |
| 47 | +KlimeClient( |
| 48 | + write_key: str, # Required: Your Klime write key |
| 49 | + endpoint: Optional[str] = None, # Optional: API endpoint (default: https://ingest.klime.com) |
| 50 | + flush_interval: Optional[int] = None, # Optional: Milliseconds between flushes (default: 2000) |
| 51 | + max_batch_size: Optional[int] = None, # Optional: Max events per batch (default: 20, max: 100) |
| 52 | + max_queue_size: Optional[int] = None, # Optional: Max queued events (default: 1000) |
| 53 | + retry_max_attempts: Optional[int] = None, # Optional: Max retry attempts (default: 5) |
| 54 | + retry_initial_delay: Optional[int] = None, # Optional: Initial retry delay in ms (default: 1000) |
| 55 | + flush_on_shutdown: Optional[bool] = None # Optional: Auto-flush on exit (default: True) |
| 56 | +) |
| 57 | +``` |
| 58 | + |
| 59 | +### Methods |
| 60 | + |
| 61 | +#### `track(event: str, properties: Optional[Dict] = None, user_id: Optional[str] = None, group_id: Optional[str] = None, ip: Optional[str] = None) -> None` |
| 62 | + |
| 63 | +Track a user event. |
| 64 | + |
| 65 | +```python |
| 66 | +# Simple usage |
| 67 | +client.track('Button Clicked', { |
| 68 | + 'button_name': 'Sign up', |
| 69 | + 'plan': 'pro' |
| 70 | +}) |
| 71 | + |
| 72 | +# With user context and IP |
| 73 | +client.track('Button Clicked', { |
| 74 | + 'button_name': 'Sign up', |
| 75 | + 'plan': 'pro' |
| 76 | +}, user_id='user_123', group_id='org_456', ip='192.168.1.1') |
| 77 | +``` |
| 78 | + |
| 79 | +#### `identify(user_id: str, traits: Optional[Dict] = None, ip: Optional[str] = None) -> None` |
| 80 | + |
| 81 | +Identify a user with traits. |
| 82 | + |
| 83 | +```python |
| 84 | +client.identify('user_123', { |
| 85 | + 'email': 'user@example.com', |
| 86 | + 'name': 'Stefan' |
| 87 | +}, ip='192.168.1.1') |
| 88 | +``` |
| 89 | + |
| 90 | +#### `group(group_id: str, traits: Optional[Dict] = None, user_id: Optional[str] = None, ip: Optional[str] = None) -> None` |
| 91 | + |
| 92 | +Associate a user with a group and set group traits. |
| 93 | + |
| 94 | +```python |
| 95 | +# Simple usage |
| 96 | +client.group('org_456', { |
| 97 | + 'name': 'Acme Inc', |
| 98 | + 'plan': 'enterprise' |
| 99 | +}) |
| 100 | + |
| 101 | +# With user ID and IP |
| 102 | +client.group('org_456', { |
| 103 | + 'name': 'Acme Inc', |
| 104 | + 'plan': 'enterprise' |
| 105 | +}, user_id='user_123', ip='192.168.1.1') |
| 106 | +``` |
| 107 | + |
| 108 | +#### `flush() -> None` |
| 109 | + |
| 110 | +Manually flush queued events immediately. |
| 111 | + |
| 112 | +```python |
| 113 | +client.flush() |
| 114 | +``` |
| 115 | + |
| 116 | +#### `shutdown() -> None` |
| 117 | + |
| 118 | +Gracefully shutdown the client, flushing remaining events. |
| 119 | + |
| 120 | +```python |
| 121 | +client.shutdown() |
| 122 | +``` |
| 123 | + |
| 124 | +## Features |
| 125 | + |
| 126 | +- **Automatic Batching**: Events are automatically batched and sent every 2 seconds or when the batch size reaches 20 events |
| 127 | +- **Automatic Retries**: Failed requests are automatically retried with exponential backoff |
| 128 | +- **Thread-Safe**: Safe to use from multiple threads |
| 129 | +- **Process Exit Handling**: Automatically flushes events on process exit (via `atexit`) |
| 130 | +- **Zero Dependencies**: Uses only Python standard library |
| 131 | + |
| 132 | +## Configuration |
| 133 | + |
| 134 | +### Default Values |
| 135 | + |
| 136 | +- `flush_interval`: 2000ms |
| 137 | +- `max_batch_size`: 20 events |
| 138 | +- `max_queue_size`: 1000 events |
| 139 | +- `retry_max_attempts`: 5 attempts |
| 140 | +- `retry_initial_delay`: 1000ms |
| 141 | +- `flush_on_shutdown`: True |
| 142 | + |
| 143 | +## Error Handling |
| 144 | + |
| 145 | +The SDK automatically handles: |
| 146 | + |
| 147 | +- **Transient errors** (429, 503, network failures): Retries with exponential backoff |
| 148 | +- **Permanent errors** (400, 401): Logs error and drops event |
| 149 | +- **Rate limiting**: Respects `Retry-After` header |
| 150 | + |
| 151 | +## Size Limits |
| 152 | + |
| 153 | +- Maximum event size: 200KB |
| 154 | +- Maximum batch size: 10MB |
| 155 | +- Maximum events per batch: 100 |
| 156 | + |
| 157 | +Events exceeding these limits are rejected and logged. |
| 158 | + |
| 159 | +## Flask Example |
| 160 | + |
| 161 | +```python |
| 162 | +from flask import Flask, request |
| 163 | +from klime import KlimeClient |
| 164 | + |
| 165 | +app = Flask(__name__) |
| 166 | +client = KlimeClient(write_key='your-write-key') |
| 167 | + |
| 168 | +@app.route('/api/button-clicked', methods=['POST']) |
| 169 | +def button_clicked(): |
| 170 | + client.track('Button Clicked', { |
| 171 | + 'button_name': request.json.get('buttonName') |
| 172 | + }, user_id=request.json.get('userId'), ip=request.remote_addr) |
| 173 | + |
| 174 | + return {'success': True} |
| 175 | + |
| 176 | +# Graceful shutdown |
| 177 | +import atexit |
| 178 | +atexit.register(client.shutdown) |
| 179 | +``` |
| 180 | + |
| 181 | +## Django Example |
| 182 | + |
| 183 | +```python |
| 184 | +from django.http import JsonResponse |
| 185 | +from django.views import View |
| 186 | +from klime import KlimeClient |
| 187 | + |
| 188 | +client = KlimeClient(write_key='your-write-key') |
| 189 | + |
| 190 | +class ButtonClickView(View): |
| 191 | + def post(self, request): |
| 192 | + client.track('Button Clicked', { |
| 193 | + 'button_name': request.POST.get('buttonName') |
| 194 | + }, user_id=str(request.user.id), ip=request.META.get('REMOTE_ADDR')) |
| 195 | + |
| 196 | + return JsonResponse({'success': True}) |
| 197 | +``` |
| 198 | + |
| 199 | +## Requirements |
| 200 | + |
| 201 | +- Python 3.7 or higher |
| 202 | +- No external dependencies (uses only standard library) |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +MIT |
| 207 | + |
0 commit comments