-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
44 lines (34 loc) · 1.09 KB
/
util.py
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
import time
import json
from redis import StrictRedis
from flask import Flask, request
import config
db = StrictRedis(host='localhost', port=6379, db=config.database)
app = Flask(__name__)
app.jinja_env.line_statement_prefix = '%'
# Allow keeping track of a per-request distinct_id
distinct_id = [None]
def get_distinct_id():
return distinct_id[0]
def set_distinct_id(value):
distinct_id[0] = value
# Log events
def log_event(event, properties={}):
remote_addr = request.headers.get(config.ip_header, None)
if not remote_addr:
remote_addr = request.remote_addr
db.rpush('events', json.dumps({
'event': event,
'distinct_id': get_distinct_id(),
'timestamp': time.time(),
'remote_addr': remote_addr,
'platform': request.user_agent.platform,
'browser': request.user_agent.browser,
'version': request.user_agent.version,
'language': request.user_agent.language,
'path': request.path,
'url': request.url,
'referrer': request.referrer,
'method': request.method,
'properties': properties
}))