Snowplow event tracker for Python

1.1.0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Snowplow tracker and send various event types, including page views, structured events, and custom self-describing events. Ensure `SNOWPLOW_COLLECTOR_ENDPOINT` is set in your environment or replace `'collector.example.com'` with your actual Snowplow collector URI.

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). ")

view raw JSON →