Snowplow event tracker for Python
The Snowplow Python Tracker allows you to collect and track event data from your Python applications, games, and web servers/frameworks, including Django. It provides a robust way to implement analytics by defining subjects, emitters, and trackers to send events to a Snowplow collector or custom event store. The library is actively maintained, with the current version being 1.1.0, and receives regular updates.
Warnings
- gotcha When initializing the tracker, the `namespace` argument is mandatory. Failing to provide it will result in an error or default behavior that might not be desired for identifying events from specific tracker instances.
- gotcha The documentation mentions that `track_screen_view()` and `track_unstruct_event()` were not fully supported in the Snowplow data pipeline's enrichment, storage, or analytics stages in older versions (e.g., v0.2, v0.4), meaning events would be logged but not processed further.
- breaking The Snowplow ecosystem, particularly for mobile trackers (iOS and Android), underwent significant API changes from version 1.x to 2.0, with `Snowplow.createTracker` becoming the new entry point and the old API being deprecated. While `snowplow-tracker` for Python is currently at version 1.x.x, future major releases might introduce similar breaking changes.
Install
-
pip install snowplow-tracker
Imports
- Snowplow
from snowplow_tracker import Snowplow
- Tracker
from snowplow_tracker import Tracker
- Emitter
from snowplow_tracker import Emitter
- Subject
from snowplow_tracker import Subject
Quickstart
import os
from snowplow_tracker import Snowplow, SelfDescribingJson
# Configure your Snowplow collector endpoint
COLLECTOR_ENDPOINT = os.environ.get('SNOWPLOW_COLLECTOR_ENDPOINT', 'collector.example.com')
# Initialize the Snowplow tracker
# The namespace is mandatory and helps identify events from this tracker instance.
tracker = Snowplow.create_tracker(namespace='my-app-tracker', endpoint=COLLECTOR_ENDPOINT)
# Track a page view event
tracker.track_page_view(
page_url='http://www.example.com/home',
page_title='Homepage',
referrer='http://www.example.com/previous'
)
# Track a structured event
tracker.track_struct_event(
category='engagement',
action='click',
label='hero-button',
property='primary-cta',
value=1.0
)
# Track an unstructured event (Self-Describing Event)
custom_event_schema = SelfDescribingJson(
'iglu:com.example/my_custom_event/jsonschema/1-0-0',
{'feature': 'new-feature', 'version': '1.0'}
)
tracker.track_self_describing_event(custom_event_schema)
print(f"Events tracked to {COLLECTOR_ENDPOINT} (check your Snowplow pipeline). ")