@@ -99,163 +99,7 @@ def _add_common_params(self, user_id, attributes):
99
99
self ._add_time ()
100
100
101
101
102
- class EventBuilderV1 (BaseEventBuilder ):
103
- """ Class which encapsulates methods to build events for tracking
104
- impressions and conversions using the old endpoint. """
105
-
106
- # Attribute mapping format
107
- ATTRIBUTE_PARAM_FORMAT = '{segment_prefix}{segment_id}'
108
- # Experiment mapping format
109
- EXPERIMENT_PARAM_FORMAT = '{experiment_prefix}{experiment_id}'
110
- # Event API format
111
- OFFLINE_API_PATH = 'https://{project_id}.log.optimizely.com/event'
112
-
113
-
114
- class EventParams (object ):
115
- ACCOUNT_ID = 'd'
116
- PROJECT_ID = 'a'
117
- EXPERIMENT_PREFIX = 'x'
118
- GOAL_ID = 'g'
119
- GOAL_NAME = 'n'
120
- END_USER_ID = 'u'
121
- EVENT_VALUE = 'v'
122
- SEGMENT_PREFIX = 's'
123
- SOURCE = 'src'
124
- TIME = 'time'
125
-
126
- def _add_attributes (self , attributes ):
127
- """ Add attribute(s) information to the event.
128
-
129
- Args:
130
- attributes: Dict representing user attributes and values which need to be recorded.
131
- """
132
-
133
- if not attributes :
134
- return
135
-
136
- for attribute_key in attributes .keys ():
137
- attribute_value = attributes .get (attribute_key )
138
- # Omit falsy attribute values
139
- if attribute_value :
140
- attribute = self .config .get_attribute (attribute_key )
141
- if attribute :
142
- self .params [self .ATTRIBUTE_PARAM_FORMAT .format (
143
- segment_prefix = self .EventParams .SEGMENT_PREFIX , segment_id = attribute .segmentId )] = attribute_value
144
-
145
- def _add_source (self ):
146
- """ Add source information to the event. """
147
-
148
- self .params [self .EventParams .SOURCE ] = 'python-sdk-{version}' .format (version = version .__version__ )
149
-
150
- def _add_time (self ):
151
- """ Add time information to the event. """
152
-
153
- self .params [self .EventParams .TIME ] = int (time .time ())
154
-
155
- def _add_impression_goal (self , experiment ):
156
- """ Add impression goal information to the event.
157
-
158
- Args:
159
- experiment: Object representing experiment being activated.
160
- """
161
-
162
- # For tracking impressions, goal ID is set equal to experiment ID of experiment being activated
163
- self .params [self .EventParams .GOAL_ID ] = experiment .id
164
- self .params [self .EventParams .GOAL_NAME ] = 'visitor-event'
165
-
166
- def _add_experiment (self , experiment , variation_id ):
167
- """ Add experiment to variation mapping to the impression event.
168
-
169
- Args:
170
- experiment: Object representing experiment being activated.
171
- variation_id: ID for variation which would be presented to user.
172
- """
173
-
174
- self .params [self .EXPERIMENT_PARAM_FORMAT .format (experiment_prefix = self .EventParams .EXPERIMENT_PREFIX ,
175
- experiment_id = experiment .id )] = variation_id
176
-
177
- def _add_experiment_variation_params (self , user_id , valid_experiments ):
178
- """ Maps experiment and corresponding variation as parameters to be used in the event tracking call.
179
-
180
- Args:
181
- user_id: ID for user.
182
- valid_experiments: List of tuples representing valid experiments for the event.
183
- """
184
-
185
- for experiment in valid_experiments :
186
- variation = self .bucketer .bucket (experiment , user_id )
187
- if variation :
188
- self .params [self .EXPERIMENT_PARAM_FORMAT .format (experiment_prefix = self .EventParams .EXPERIMENT_PREFIX ,
189
- experiment_id = experiment .id )] = variation .id
190
-
191
- def _add_conversion_goal (self , event_key , event_value ):
192
- """ Add conversion goal information to the event.
193
-
194
- Args:
195
- event_key: Event key representing the event which needs to be recorded.
196
- event_value: Value associated with the event. Can be used to represent revenue in cents.
197
- """
198
-
199
- event = self .config .get_event (event_key )
200
-
201
- if not event :
202
- return
203
-
204
- event_ids = event .id
205
-
206
- if event_value :
207
- event_ids = '{goal_id},{revenue_goal_id}' .format (goal_id = event .id ,
208
- revenue_goal_id = self .config .get_revenue_goal ().id )
209
- self .params [self .EventParams .EVENT_VALUE ] = event_value
210
-
211
- self .params [self .EventParams .GOAL_ID ] = event_ids
212
- self .params [self .EventParams .GOAL_NAME ] = event_key
213
-
214
- def create_impression_event (self , experiment , variation_id , user_id , attributes ):
215
- """ Create impression Event to be sent to the logging endpoint.
216
-
217
- Args:
218
- experiment: Object representing experiment for which impression needs to be recorded.
219
- variation_id: ID for variation which would be presented to user.
220
- user_id: ID for user.
221
- attributes: Dict representing user attributes and values which need to be recorded.
222
-
223
- Returns:
224
- Event object encapsulating the impression event.
225
- """
226
-
227
- self .params = {}
228
- self ._add_common_params (user_id , attributes )
229
- self ._add_impression_goal (experiment )
230
- self ._add_experiment (experiment , variation_id )
231
- return Event (self .OFFLINE_API_PATH .format (project_id = self .params [self .EventParams .PROJECT_ID ]),
232
- self .params )
233
-
234
- def create_conversion_event (self , event_key , user_id , attributes , event_tags , valid_experiments ):
235
- """ Create conversion Event to be sent to the logging endpoint.
236
-
237
- Args:
238
- event_key: Event key representing the event which needs to be recorded.
239
- user_id: ID for user.
240
- attributes: Dict representing user attributes and values.
241
- event_tags: Dict representing metadata associated with the event.
242
- valid_experiments: List of tuples representing valid experiments for the event.
243
-
244
- Returns:
245
- Event object encapsulating the conversion event.
246
- """
247
-
248
- event_value = event_tag_utils .get_revenue_value (event_tags )
249
-
250
- self .params = {}
251
- self ._add_common_params (user_id , attributes )
252
- self ._add_conversion_goal (event_key , event_value )
253
- self ._add_experiment_variation_params (user_id , valid_experiments )
254
- return Event (self .OFFLINE_API_PATH .format (project_id = self .params [self .EventParams .PROJECT_ID ]),
255
- self .params )
256
-
257
-
258
- class EventBuilderV2 (BaseEventBuilder ):
102
+ class EventBuilder (BaseEventBuilder ):
259
103
""" Class which encapsulates methods to build events for tracking
260
104
impressions and conversions using the new endpoints. """
261
105
@@ -454,9 +298,7 @@ def get_event_builder(config, bucketer):
454
298
"""
455
299
456
300
config_version = config .get_version ()
457
- if config_version == project_config .V1_CONFIG_VERSION :
458
- return EventBuilderV1 (config , bucketer )
459
301
if config_version == project_config .V2_CONFIG_VERSION :
460
- return EventBuilderV2 (config , bucketer )
302
+ return EventBuilder (config , bucketer )
461
303
462
304
raise exceptions .InvalidInputException (enums .Errors .UNSUPPORTED_DATAFILE_VERSION )
0 commit comments