forked from Woopra/woopra-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
147 lines (123 loc) · 4.16 KB
/
__init__.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
try:
from urllib.parse import urlencode
from http.client import HTTPConnection,HTTPException,HTTPSConnection
except ImportError:
from urllib import urlencode
from httplib import HTTPConnection
from httplib import HTTPSConnection
from httplib import HTTPException
import hashlib
class WoopraTracker:
"""
Woopra Python SDK.
This class represents the Python equivalent of the JavaScript Woopra Object.
"""
SDK_ID = "python"
DEFAULT_TIMEOUT = 30000
def __init__(self, domain):
"""
The constructor.
Parameter:
domain - str : the domain name of your website as submitted on woopra.com
Result:
WoopraTracker
"""
self.domain = domain
self.secure = False
self.idle_timeout = WoopraTracker.DEFAULT_TIMEOUT
self.user_properties = {}
self.cookie = None
self.user_agent = None
self.ip_address = None
def set_idle_timeout(self, idle_timeout):
self.idle_timeout = idle_timeout
def set_cookie(self, cookie):
self.cookie = cookie
def set_ip_address(self, ip_address):
self.ip_address = ip_address
def set_user_agent(self, user_agent):
self.user_agent = user_agent
def set_secure(self, secure):
self.secure = secure
def identify(self, user_properties = {}):
"""
Identifies a user.
Parameters:
properties (optional) - dict : the user's additional properties (name, company, ...)
key - str : the user property name
value -str, int, bool = the user property value
"""
self.user_properties = user_properties
if self.cookie == None:
value=None
if 'id' in self.user_properties:
value=self.user_properties['id']
elif 'email' in self.user_properties:
value=self.user_properties['email']
if value != None:
m = hashlib.md5()
m.update(value.encode('utf8'))
long_cookie = m.hexdigest().upper()
self.cookie = (long_cookie[:12]) if len(long_cookie) > 12 else long_cookie
def track(self, event_name, event_data = {}):
"""
Tracks pageviews and custom events
Parameters:
event_name (optional) - str : The name of the event. If none is specified, will track pageview
event_data (optional) - dict : Properties the custom event
key - str : the event property name
value - str, int, bool : the event property value
Examples:
# This code tracks a custom event through the back-end:
woopra.track('signup', {'company' : 'My Business', 'username' : 'johndoe', 'plan' : 'Gold'})
"""
self.woopra_http_request(True, event_name, event_data)
def push(self):
"""
Pushes the indentification information on the user to Woopra in case no tracking event occurs.
Parameter:
back_end_tracking (optional) - boolean : Should the information be pushed through the back-end? Defaults to False.
Result:
None
"""
self.woopra_http_request(False)
def woopra_http_request(self, is_tracking, event_name = None, event_data = {}):
"""
Sends an Http Request to Woopra for back-end identification and/or tracking.
Parameters:
isTracking - boolean : is this request supposed to track an event or just identify the user?
event (optional) - dict : only matters if isTracking == True. The event to pass. Default is None.
Result:
None
"""
get_params = {}
# Configuration
get_params["host"] = self.domain
if self.ip_address != None:
get_params["ip"] = self.ip_address
if self.idle_timeout != None:
get_params["timeout"] = self.idle_timeout
if self.cookie != None:
get_params["cookie"] = self.cookie
# Identification
for k, v in self.user_properties.items():
get_params["cv_" + k] = v
if not is_tracking:
url = "/track/identify/?" + urlencode(get_params) + "&ce_app=" + WoopraTracker.SDK_ID
else:
get_params["ce_name"] = event_name
for k,v in event_data.items():
get_params["ce_" + k] = v
url = "/track/ce/?" + urlencode(get_params) + "&ce_app=" + WoopraTracker.SDK_ID
try:
if self.secure:
conn = HTTPSConnection("www.woopra.com")
else:
conn = HTTPConnection("www.woopra.com")
if self.user_agent != None:
conn.request("GET", url, headers={'User-agent': self.user_agent})
else:
conn.request("GET", url)
except HTTPException as e: print(str(e))