Python
The OpenPanel Python SDK allows you to track user behavior in your Python applications. This guide provides instructions for installing and using the Python SDK in your project.
Installation
Install dependencies
pip install openpanelInitialize
Import and initialize the OpenPanel SDK with your credentials:
from openpanel import OpenPanel
op = OpenPanel(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET"
)Configuration Options
Common options
apiUrl- The url of the openpanel API or your self-hosted instanceclientId- The client id of your applicationclientSecret- The client secret of your application (only required for server-side events)filter- A function that will be called before sending an event. If it returns false, the event will not be sentdisabled- If true, the library will not send any events
Additional Python-specific options:
filter- A function that will be called before tracking an event. If it returns false the event will not be trackeddisabled- Set toTrueto disable all event trackingglobal_properties- Dictionary of properties that will be sent with every event
Filter Function Example
def my_filter(event):
# Skip events named 'my_event'
return event.get('name') != 'my_event'
op = OpenPanel(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
filter=my_filter
)Usage
Tracking Events
To track an event, use the track method:
# Track a simple event
op.track("button_clicked")
# Track with properties
op.track("purchase_completed", {
"product_id": "123",
"price": 99.99,
"currency": "USD"
})
# Track for a specific user
op.track("login_successful", {
"method": "google"
}, profile_id="user_123")Identifying Users
To identify a user, use the identify method with the profile ID as the first argument and a dictionary of traits as the second:
op.identify("user123", {
"firstName": "John",
"lastName": "Doe",
"email": "john@example.com",
"tier": "premium",
"company": "Acme Inc"
})Setting Global Properties
To set properties that will be sent with every event:
op.set_global_properties({
"app_version": "1.0.2",
"environment": "production",
"deployment": "us-east-1"
})Incrementing Properties
To increment a numeric property on a user profile:
op.increment({
"profile_id": "1",
"property": "visits",
"value": 1 # optional, defaults to 1
})Decrementing Properties
To decrement a numeric property on a user profile:
op.decrement({
"profile_id": "1",
"property": "credits",
"value": 1 # optional, defaults to 1
})Clearing User Data
To clear the current user's data:
op.clear()Advanced Usage
Thread Safety
The OpenPanel SDK is thread-safe. You can safely use a single instance across multiple threads in your application.
Error Handling
The SDK includes built-in error handling and will not raise exceptions during normal operation. However, you can wrap SDK calls in try-except blocks for additional safety:
try:
op.track("important_event", {"critical": True})
except Exception as e:
logger.error(f"Failed to track event: {e}")Disabling Tracking
You can temporarily disable all tracking:
# Disable during initialization
op = OpenPanel(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
disabled=True
)
# Or disable after initialization
op.disabled = True