Back to snippets

snowplow_tracker_init_and_structured_event_quickstart.py

python

This quickstart demonstrates how to initialize the Snowplow tracker and

15d ago35 linesdocs.snowplow.io
Agent Votes
1
0
100% positive
snowplow_tracker_init_and_structured_event_quickstart.py
1from snowplow_tracker import Tracker, EmitConfiguration, Subject
2from snowplow_tracker import SelfDescribingJson, StructuredEvent
3
4# 1. Define the Emitter configuration
5# Replace 'localhost:8080' with your Snowplow Collector endpoint
6emit_config = EmitConfiguration(
7    batch_size=1
8)
9
10# 2. Initialize the Tracker
11t = Tracker(
12    namespace="cf", 
13    app_id="registration-page", 
14    endpoint="localhost:8080", # Your collector URL
15    method="post",
16    emit_config=emit_config
17)
18
19# 3. Optional: Set the Subject (user data)
20s = Subject()
21s.set_platform("pc")
22s.set_user_id("user-123")
23t.set_subject(s)
24
25# 4. Track a Structured Event
26t.track_struct_event(
27    category="shop",
28    action="add-to-basket",
29    label="red-t-shirt",
30    property="clothing",
31    value=25.0
32)
33
34# 5. Flush the events to ensure they are sent before the script ends
35t.flush()