Skip to content

Commit 2390f8b

Browse files
Updating event builder (#99)
1 parent fef6401 commit 2390f8b

File tree

2 files changed

+162
-85
lines changed

2 files changed

+162
-85
lines changed

optimizely/event_builder.py

Lines changed: 78 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016-2017, Optimizely
1+
# Copyright 2016-2018, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -35,78 +35,85 @@ class BaseEventBuilder(object):
3535

3636
def __init__(self, config):
3737
self.config = config
38-
self.params = {}
3938

4039
@abstractproperty
4140
class EventParams(object):
4241
pass
4342

44-
def _add_project_id(self):
45-
""" Add project ID to the event. """
43+
def _get_project_id(self):
44+
""" Get project ID.
4645
47-
self.params[self.EventParams.PROJECT_ID] = self.config.get_project_id()
48-
49-
def _add_account_id(self):
50-
""" Add account ID to the event. """
46+
Returns:
47+
Project ID of the datafile.
48+
"""
5149

52-
self.params[self.EventParams.ACCOUNT_ID] = self.config.get_account_id()
50+
return self.config.get_project_id()
5351

54-
@abstractmethod
55-
def _add_user_id(self, user_id):
56-
""" Add user ID to the event.
52+
def _get_account_id(self):
53+
""" Get account ID.
5754
58-
Args:
59-
user_id: ID of the user.
55+
Returns:
56+
Account ID in the datafile.
6057
"""
61-
pass
58+
59+
return self.config.get_account_id()
6260

6361
@abstractmethod
64-
def _add_attributes(self, attributes):
65-
""" Add attribute(s) information to the event.
62+
def _get_attributes(self, attributes):
63+
""" Get attribute(s) information.
6664
6765
Args:
6866
attributes: Dict representing user attributes and values which need to be recorded.
6967
"""
7068
pass
7169

72-
@abstractmethod
73-
def _add_source(self):
74-
""" Add source information to the event. """
75-
pass
76-
77-
def _add_revision(self):
78-
""" Add datafile revision information to the event. """
79-
pass
70+
def _get_anonymize_ip(self):
71+
""" Get IP anonymization bool
8072
81-
def _add_anonymize_ip(self):
82-
""" Add IP anonymization bool to the event """
73+
Returns:
74+
bool 'anonymizeIP' value in the datafile.
75+
"""
8376

84-
self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()
77+
return self.config.get_anonymize_ip_value()
8578

8679
@abstractmethod
8780
def _get_time(self):
88-
""" Get time in milliseconds to be added to the event.
81+
""" Get time in milliseconds to be added.
8982
9083
Returns:
91-
Current time in milliseconds.
84+
int Current time in milliseconds.
9285
"""
9386

9487
return int(round(time.time() * 1000))
9588

96-
def _add_common_params(self, user_id, attributes):
97-
""" Add params which are used same in both conversion and impression events.
89+
def _get_common_params(self, user_id, attributes):
90+
""" Get params which are used same in both conversion and impression events.
9891
9992
Args:
10093
user_id: ID for user.
10194
attributes: Dict representing user attributes and values which need to be recorded.
95+
96+
Returns:
97+
Dict consisting of parameters common to both impression and conversion events.
10298
"""
99+
commonParams = {}
100+
101+
commonParams[self.EventParams.PROJECT_ID] = self._get_project_id()
102+
commonParams[self.EventParams.ACCOUNT_ID] = self._get_account_id()
103+
104+
visitor = {}
105+
visitor[self.EventParams.END_USER_ID] = user_id
106+
visitor[self.EventParams.SNAPSHOTS] = []
107+
108+
commonParams[self.EventParams.USERS] = []
109+
commonParams[self.EventParams.USERS].append(visitor)
110+
commonParams[self.EventParams.USERS][0][self.EventParams.ATTRIBUTES] = self._get_attributes(attributes)
103111

104-
self._add_project_id()
105-
self._add_account_id()
106-
self._add_user_id(user_id)
107-
self._add_attributes(attributes)
108-
self._add_source()
109-
self._add_anonymize_ip()
112+
commonParams[self.EventParams.SOURCE_SDK_TYPE] = 'python-sdk'
113+
commonParams[self.EventParams.SOURCE_SDK_VERSION] = version.__version__
114+
commonParams[self.EventParams.ANONYMIZE_IP] = self._get_anonymize_ip()
115+
116+
return commonParams
110117

111118

112119
class EventBuilder(BaseEventBuilder):
@@ -139,58 +146,45 @@ class EventParams(object):
139146
CUSTOM = 'custom'
140147
ANONYMIZE_IP = 'anonymize_ip'
141148

142-
def _add_attributes(self, attributes):
143-
""" Add attribute(s) information to the event.
149+
def _get_attributes(self, attributes):
150+
""" Get attribute(s) information.
144151
145152
Args:
146153
attributes: Dict representing user attributes and values which need to be recorded.
154+
155+
Returns:
156+
List consisting of valid attributes for the user. Empty otherwise.
147157
"""
148158

149-
visitor = self.params[self.EventParams.USERS][0]
150-
visitor[self.EventParams.ATTRIBUTES] = []
159+
params = []
151160

152161
if not attributes:
153-
return
162+
return []
154163

155164
for attribute_key in attributes.keys():
156165
attribute_value = attributes.get(attribute_key)
157166
# Omit falsy attribute values
158167
if attribute_value:
159168
attribute = self.config.get_attribute(attribute_key)
160169
if attribute:
161-
visitor[self.EventParams.ATTRIBUTES].append({
170+
params.append({
162171
self.EventParams.EVENT_ID: attribute.id,
163172
'key': attribute_key,
164173
'type': self.EventParams.CUSTOM,
165174
'value': attribute_value,
166175
})
167176

168-
def _add_source(self):
169-
""" Add source information to the event. """
177+
return params
170178

171-
self.params[self.EventParams.SOURCE_SDK_TYPE] = 'python-sdk'
172-
self.params[self.EventParams.SOURCE_SDK_VERSION] = version.__version__
173-
174-
def _add_user_id(self, user_id):
175-
""" Add user ID to the event.
176-
177-
Args:
178-
user_id: ID of the user.
179-
"""
180-
181-
self.params[self.EventParams.USERS] = []
182-
# Add a single visitor
183-
visitor = {}
184-
visitor[self.EventParams.END_USER_ID] = user_id
185-
visitor[self.EventParams.SNAPSHOTS] = []
186-
self.params[self.EventParams.USERS].append(visitor)
187-
188-
def _add_required_params_for_impression(self, experiment, variation_id):
189-
""" Add parameters that are required for the impression event to register.
179+
def _get_required_params_for_impression(self, experiment, variation_id):
180+
""" Get parameters that are required for the impression event to register.
190181
191182
Args:
192183
experiment: Experiment for which impression needs to be recorded.
193184
variation_id: ID for variation which would be presented to user.
185+
186+
Returns:
187+
Dict consisting of decisions and events info for impression event.
194188
"""
195189
snapshot = {}
196190

@@ -207,19 +201,19 @@ def _add_required_params_for_impression(self, experiment, variation_id):
207201
self.EventParams.UUID: str(uuid.uuid4())
208202
}]
209203

210-
visitor = self.params[self.EventParams.USERS][0]
211-
visitor[self.EventParams.SNAPSHOTS].append(snapshot)
204+
return snapshot
212205

213-
def _add_required_params_for_conversion(self, event_key, event_tags, decisions):
214-
""" Add parameters that are required for the conversion event to register.
206+
def _get_required_params_for_conversion(self, event_key, event_tags, decisions):
207+
""" Get parameters that are required for the conversion event to register.
215208
216209
Args:
217210
event_key: Key representing the event which needs to be recorded.
218211
event_tags: Dict representing metadata associated with the event.
219212
decisions: List of tuples representing valid experiments IDs and variation IDs.
220-
"""
221213
222-
visitor = self.params[self.EventParams.USERS][0]
214+
Returns:
215+
Dict consisting of the decisions and events info for conversion event.
216+
"""
223217

224218
for experiment_id, variation_id in decisions:
225219
snapshot = {}
@@ -252,7 +246,8 @@ def _add_required_params_for_conversion(self, event_key, event_tags, decisions):
252246
event_dict[self.EventParams.TAGS] = event_tags
253247

254248
snapshot[self.EventParams.EVENTS] = [event_dict]
255-
visitor[self.EventParams.SNAPSHOTS].append(snapshot)
249+
250+
return snapshot
256251

257252
def create_impression_event(self, experiment, variation_id, user_id, attributes):
258253
""" Create impression Event to be sent to the logging endpoint.
@@ -267,12 +262,13 @@ def create_impression_event(self, experiment, variation_id, user_id, attributes)
267262
Event object encapsulating the impression event.
268263
"""
269264

270-
self.params = {}
271-
self._add_common_params(user_id, attributes)
272-
self._add_required_params_for_impression(experiment, variation_id)
265+
params = self._get_common_params(user_id, attributes)
266+
impression_params = self._get_required_params_for_impression(experiment, variation_id)
267+
268+
params[self.EventParams.USERS][0][self.EventParams.SNAPSHOTS].append(impression_params)
273269

274270
return Event(self.EVENTS_URL,
275-
self.params,
271+
params,
276272
http_verb=self.HTTP_VERB,
277273
headers=self.HTTP_HEADERS)
278274

@@ -290,10 +286,12 @@ def create_conversion_event(self, event_key, user_id, attributes, event_tags, de
290286
Event object encapsulating the conversion event.
291287
"""
292288

293-
self.params = {}
294-
self._add_common_params(user_id, attributes)
295-
self._add_required_params_for_conversion(event_key, event_tags, decisions)
289+
params = self._get_common_params(user_id, attributes)
290+
conversion_params = self._get_required_params_for_conversion(event_key, event_tags, decisions)
291+
292+
params[self.EventParams.USERS][0][self.EventParams.SNAPSHOTS].append(conversion_params)
293+
296294
return Event(self.EVENTS_URL,
297-
self.params,
295+
params,
298296
http_verb=self.HTTP_VERB,
299297
headers=self.HTTP_HEADERS)

0 commit comments

Comments
 (0)