forked from snowplow/snowplow-python-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnowplow_app.py
More file actions
59 lines (45 loc) · 1.5 KB
/
Copy pathsnowplow_app.py
File metadata and controls
59 lines (45 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
from snowplow_tracker import (
Snowplow,
EmitterConfiguration,
Subject,
TrackerConfiguration,
SelfDescribingJson,
)
def get_url_from_args():
if len(sys.argv) != 2:
raise ValueError("Collector Endpoint is required")
return sys.argv[1]
def main():
collector_url = get_url_from_args()
# Configure Emitter
custom_retry_codes = {500: False, 401: True}
emitter_config = EmitterConfiguration(batch_size=5, custom_retry_codes=custom_retry_codes)
# Configure Tracker
tracker_config = TrackerConfiguration(encode_base64=True)
# Initialise subject
subject = Subject()
subject.set_user_id("uid")
Snowplow.create_tracker(
namespace="ns",
endpoint=collector_url,
app_id="app1",
subject=subject,
tracker_config=tracker_config,
emitter_config=emitter_config,
)
tracker = Snowplow.get_tracker("ns")
tracker.track_page_view("https://www.snowplow.io", "Homepage")
tracker.track_page_ping("https://www.snowplow.io", "Homepage")
tracker.track_link_click("https://www.snowplow.io/about")
tracker.track_page_view("https://www.snowplow.io/about", "About")
tracker.track_self_describing_event(
SelfDescribingJson(
"iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1",
{"targetUrl": "example.com"},
)
)
tracker.track_struct_event("shop", "add-to-basket", None, "pcs", 2)
tracker.flush()
if __name__ == "__main__":
main()