-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathredis_app.py
More file actions
87 lines (68 loc) · 2.1 KB
/
Copy pathredis_app.py
File metadata and controls
87 lines (68 loc) · 2.1 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from snowplow_tracker import (
Tracker,
ScreenView,
PagePing,
PageView,
SelfDescribing,
StructuredEvent,
SelfDescribingJson,
)
from snowplow_tracker.typing import PayloadDict
import json
import redis
import logging
# logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
class RedisEmitter(object):
"""
Sends Snowplow events to a Redis database
"""
def __init__(self, rdb=None, key: str = "redis_key") -> None:
"""
:param rdb: Optional custom Redis database
:type rdb: redis | None
:param key: The Redis key for the list of events
:type key: string
"""
if rdb is None:
rdb = redis.StrictRedis()
self.rdb = rdb
self.key = key
def input(self, payload: PayloadDict) -> None:
"""
:param payload: The event properties
:type payload: dict(string:*)
"""
logger.info("Pushing event to Redis queue...")
self.rdb.rpush(self.key, json.dumps(payload))
logger.info("Finished sending event to Redis.")
def flush(self) -> None:
logger.warning("The RedisEmitter class does not need to be flushed")
return
def sync_flush(self) -> None:
self.flush()
def main():
emitter = RedisEmitter()
t = Tracker(namespace="snowplow_tracker", emitters=emitter)
page_view = PageView(page_url="https://www.snowplow.io", page_title="Homepage")
t.track(page_view)
page_ping = PagePing(page_url="https://www.snowplow.io", page_title="Homepage")
t.track(page_ping)
link_click = SelfDescribing(
SelfDescribingJson(
"iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1",
{"targetUrl": "https://www.snowplow.io"},
)
)
t.track(link_click)
id = t.get_uuid()
screen_view = ScreenView(id_=id, name="name")
t.track(screen_view)
struct_event = StructuredEvent(
category="shop", action="add-to-basket", property_="pcs", value=2
)
t.track(struct_event)
if __name__ == "__main__":
main()