RudderStack is an open-source Segment alternative written in Go, built for the enterprise.
Actively maintained. Last release 4 weeks ago, commits in 10 of the last 26 weeks. A safe choice.
pip install rudder-sdk-pythonRudderStack Python SDK
RudderStack’s Python SDK lets you track events from your Python application. Once enabled, the event requests hit the RudderStack servers. RudderStack then transforms and routes these events to your specified destination platforms.
More details on the Python SDK can be found in our documentation
Getting Started with Python SDK
Install rudder-sdk-python using pip:
pip install rudder-sdk-python
Initializing the RudderStack Client
import rudderstack.analytics as analytics
analytics.write_key = <SOURCE_WRITE_KEY>
analytics.on_error = <FAILURE CALLBACK>
analytics.debug = <True or False>
analytics.dataPlaneUrl = <RUDDERSTACK_DATA_PLANE_URL>
analytics.gzip = <True or False>
Sending Events
Once the RudderStack client is initialized, you can use it to send your customer events. A sample track call is shown below:
rudder_analytics.track('developer_user_id', 'Simple Track Event', {
'key1': 'val1'
})
analytics.track('user_id', 'Simple Track Event', anonymous_id='anonymousId',
properties={
'key1': 'val1'
}, context={
'key1': 'val1'
})
For more information on the supported calls, refer to the documentation.
Editing or dropping events before enqueue
Set before_enqueue to edit or drop events before they leave your process. It
runs once per event, on the calling thread, after the SDK fills in messageId,
timestamp, channel and context. It receives its own copy of the event as a
dict. Return the (possibly edited) dict to send it, or None to drop it.
import rudderstack.analytics as analytics
def before_enqueue(msg):
# Drop: internal test traffic never leaves the process
if (msg.get('userId') or '').startswith('test-'):
return None
# Edit: strip the email before it is transmitted
for container in (msg, msg.get('context')):
if isinstance(container, dict):
traits = container.get('traits')
if isinstance(traits, dict):
traits.pop('email', None)
# Must return the event: an implicit None drops it
return msg
analytics.before_enqueue = before_enqueue
# This hook redacts PII, so never fall back to the unredacted original
analytics.before_enqueue_on_error = 'drop'