-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.py
61 lines (52 loc) · 2.5 KB
/
tracker.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# March 29 2018
# Google Analytics launch tracking logic.
# Generalized and pulled into its own module to simplify use in other apps.
import os, pickle, uuid
import UniversalAnalytics
class Tracker():
# uuid_path - path to pickle file containing UUID
# ga_id_components - list of the components of a GA ID to be combined with '-'
# to avoid hard-coded ID string in code, every so slightly obfuscating it
# to foil only the very dumbest trackers skimming GitHub for GA IDs.
# user_defined_id - if not None, overrides auto-generated UUID with specified string
def __init__(self, uuid_path, ga_id_components, user_defined_uuid=None):
self.uuid_path = uuid_path
self.ga_id = '-'.join(ga_id_components)
self.user_defined_uuid = user_defined_uuid
# ping launch tracker, creating and saving UUID if it none exists
def ping(self):
user_uuid = self.get_uuid()
tracker = UniversalAnalytics.Tracker.create(self.ga_id, client_id=user_uuid)
tracker.send('pageview', path='/', title='launch: UUID={}'.format(user_uuid))
# retrieve existing UUID from uuid_path or create new UUID if none exists
def get_uuid(self):
user_uuid = None
if os.path.exists(self.uuid_path):
self._remove_windows_line_endings()
with open(self.uuid_path, 'rb') as uuidFileStream:
user_uuid = pickle.load(uuidFileStream)
else:
user_uuid = self.create_uuid()
self.write_uuid(user_uuid)
print("Got UUID = {}".format(user_uuid))
return user_uuid
# create UUID, or use user_defined_uuid if defined
def create_uuid(self):
return self.user_defined_uuid if self.user_defined_uuid else uuid.uuid4()
# write UUID to file
def write_uuid(self, out_uuid):
with open(self.uuid_path, 'wb') as uuidFileStream:
pickle.dump(out_uuid, uuidFileStream)
# change UUID without pinging - intended for use by devs to create an ID
# easily distinguishable from the auto-generated UUIDs of actual users
def update_uuid(self, new_uuid):
self.write_uuid(new_uuid)
# Python 3 doesn't play well with Windows line endings - remove
# them from the UUID pickle file if present
def _remove_windows_line_endings(self):
content = ""
with open(self.uuid_path, "rb") as f:
content = f.read()
with open(self.uuid_path, "wb") as f:
for line in content.splitlines():
f.write(line + str.encode("\n"))