Skip to main content

Trodo Analytics SDK for Python — server-side event tracking

Project description

trodo-python

Server-side Python SDK for Trodo Analytics. Track backend events, identify users, and manage people/groups — all merging seamlessly with your frontend Trodo data under the same site_id.

Installation

pip install trodo-python

For async support (optional):

pip install trodo-python[async]

Requires Python 3.8+.

Quick Start

import trodo

# Initialize once at app startup
trodo.init(
    site_id='your-site-id',
    debug=False,        # optional: log API calls
    auto_events=True,   # optional: hook sys.excepthook
)

# Get a user context
user = trodo.for_user('user-123')

# Track a custom event
user.track('purchase_completed', {'amount': 99.99, 'plan': 'pro'})

# Identify the user (merges with frontend events under the same identity)
user.identify('user@example.com')   # distinct_id becomes id_user@example.com

# Update people profile
user.people.set({'plan': 'pro', 'company': 'Acme'})

# Track a server-side error
user.capture_error(Exception('payment failed'))

# Flush queued events before process exit
trodo.shutdown()

Flask / FastAPI Example

# Flask
from flask import Flask, request
import trodo

app = Flask(__name__)
trodo.init(site_id='your-site-id')

@app.route('/purchase', methods=['POST'])
def purchase():
    user_id = request.json['user_id']
    user = trodo.for_user(user_id)
    user.track('purchase_completed', {'amount': request.json['amount']})
    return {'ok': True}

Cross-SDK Identity Merging

Frontend and backend events merge when both sides call identify() with the same value:

# Python SDK
user.identify('user@example.com')    # → id_user@example.com

# Browser SDK (same value)
# Trodo.identify('user@example.com') → id_user@example.com

# Both event streams now appear together in the Trodo dashboard

API Reference

trodo.init(config)

Parameter Type Default Description
site_id str required Your Trodo site ID
api_base str https://sdkapi.trodo.ai API base URL
debug bool False Log API requests/responses
auto_events bool False Hook sys.excepthook + threading.excepthook
retries int 3 HTTP retry attempts on 5xx errors
timeout int 10 HTTP timeout in seconds
batch_enabled bool False Queue events and flush in bulk
batch_size int 50 Max events per batch flush
batch_flush_interval float 5.0 Flush interval in seconds
on_error callable None Callback for SDK errors

trodo.for_user(distinct_id, session_id=None)

Returns a user-bound context. All subsequent calls use this user's session.

user = trodo.for_user(
    'user-123',
    session_id=request.cookies.get('trodo_session'),  # optional: correlate with browser session
)

User Context Methods

user.track(event_name, properties=None)          # Track custom event
user.track_event(event_name, properties=None)    # Alias for track()
user.identify(identify_id)                       # Merge identity
user.wallet_address(address)                     # Set crypto wallet address
user.reset()                                     # Clear session context
user.capture_error(exception)                    # Track server_error event

# People profile
user.people.set(properties)
user.people.set_once(properties)
user.people.unset(keys)
user.people.increment(properties)
user.people.append(properties)
user.people.union(properties)
user.people.remove(properties)
user.people.track_charge(amount, properties=None)
user.people.clear_charges()
user.people.delete_user()

# Groups
user.set_group(group_key, group_id)
user.add_group(group_key, group_id)
user.remove_group(group_key, group_id)
group = user.get_group(group_key, group_id)
group.set(properties)
group.set_once(properties)
group.union(properties)
group.remove(properties)
group.unset(keys)
group.increment(properties)
group.append(properties)
group.delete()

Direct Call Pattern (for pipelines/scripts)

trodo.track('user-123', 'event_name', {'key': 'value'})
trodo.identify('user-123', 'identify_id')
trodo.people_set('user-123', {'plan': 'pro'})
trodo.set_group('user-123', 'company', 'acme')

Global Methods

trodo.enable_auto_events()    # Enable sys.excepthook hooks
trodo.disable_auto_events()   # Disable hooks
trodo.flush()                 # Flush pending batch queue
trodo.shutdown()              # Flush + stop background timers

Auto Events

When auto_events=True, the SDK wraps Python's exception hooks and sends server_error events to Trodo:

  • sys.excepthook — unhandled exceptions in the main thread
  • threading.excepthook — unhandled exceptions in threads

These events use distinct_id: 'server_global' in the dashboard.

Per-user error capture: user.capture_error(e) uses the user's own distinct_id.

Batching

trodo.init(
    site_id='your-site-id',
    batch_enabled=True,
    batch_size=100,
    batch_flush_interval=3.0,
)

# Events are queued and flushed every 3s or when 100 events accumulate
user.track('page_view')

# Always flush before process exit
import atexit
atexit.register(trodo.shutdown)

Thread Safety

The SDK is thread-safe. SessionManager, EventQueue, and BatchFlusher all use threading.Lock internally. Safe to use in multi-threaded Flask/Django/FastAPI applications.

License

ISC

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

trodo_python-1.0.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

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

trodo_python-1.0.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file trodo_python-1.0.0.tar.gz.

File metadata

  • Download URL: trodo_python-1.0.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for trodo_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 963748f6cdba8c5b8cf48e35b7090b4cfe59d6452b441ecb293e8ced344a1b4e
MD5 b84b1adf084c1cacd45a3409fbaf7fed
BLAKE2b-256 dd6f2d8e4d6d1489f9ffcc42c9989bd85a8e03472e40c643c718219a150ac48e

See more details on using hashes here.

File details

Details for the file trodo_python-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: trodo_python-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for trodo_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c439c1b5c71e5f497ca4438a0f01d87b761daa5bb1d9185454ea610c17bbe3
MD5 af82dc28718701ea8925b67f28ddcb37
BLAKE2b-256 fc85a210f393a3b412f7e39ffdd522737bc236131679e0d9bb4e459b8898ad0f

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