RudderStack Python SDK

2.1.4 · active · verified Thu Apr 16

RudderStack's Python SDK (`rudder-sdk-python`) is an open-source client for tracking and sending server-side customer event data from Python applications. It acts as an open-source Segment alternative, enabling developers to collect and route event data to various downstream destinations. The library is currently at version 2.1.4 and is actively maintained with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

Initialize the RudderStack Python SDK with your source write key and data plane URL, then use `identify`, `track`, and `page` methods to send customer events. Remember to call `flush()` to ensure all buffered events are sent before your application exits.

import os
import rudderstack.analytics as rudder_analytics

# Set your RudderStack Write Key and Data Plane URL from environment variables
# Replace with your actual values or use environment variables for production
WRITE_KEY = os.environ.get('RUDDERSTACK_WRITE_KEY', 'YOUR_WRITE_KEY')
DATA_PLANE_URL = os.environ.get('RUDDERSTACK_DATA_PLANE_URL', 'YOUR_DATA_PLANE_URL')

rudder_analytics.write_key = WRITE_KEY
rudder_analytics.dataPlaneUrl = DATA_PLANE_URL

# Optional: Configure debug mode for verbose logging
rudder_analytics.debug = True

# Identify a user
rudder_analytics.identify(
    user_id='user_123',
    traits={'name': 'John Doe', 'email': 'john.doe@example.com'}
)

# Track an event
rudder_analytics.track(
    user_id='user_123',
    event='Product Viewed',
    properties={'product_id': '456', 'product_name': 'Example Widget'}
)

# You can also use anonymous_id if user_id is not available
rudder_analytics.page(
    anonymous_id='anon_789',
    name='Homepage',
    category='Marketing'
)

# Ensure all queued events are sent before exiting
rudder_analytics.flush()

print('Events sent to RudderStack.')

view raw JSON →