@@ -97,198 +97,6 @@ def _add_common_params(self, user_id, attributes):
9797
9898
9999class EventBuilder (BaseEventBuilder ):
100- """ Class which encapsulates methods to build events for tracking
101- impressions and conversions using the new endpoints. """
102-
103- IMPRESSION_ENDPOINT = 'https://logx.optimizely.com/log/decision'
104- CONVERSION_ENDPOINT = 'https://logx.optimizely.com/log/event'
105- HTTP_VERB = 'POST'
106- HTTP_HEADERS = {'Content-Type' : 'application/json' }
107-
108- class EventParams (object ):
109- ACCOUNT_ID = 'accountId'
110- PROJECT_ID = 'projectId'
111- LAYER_ID = 'layerId'
112- EXPERIMENT_ID = 'experimentId'
113- VARIATION_ID = 'variationId'
114- END_USER_ID = 'visitorId'
115- EVENT_ID = 'eventEntityId'
116- EVENT_NAME = 'eventName'
117- EVENT_METRICS = 'eventMetrics'
118- EVENT_FEATURES = 'eventFeatures'
119- USER_FEATURES = 'userFeatures'
120- DECISION = 'decision'
121- LAYER_STATES = 'layerStates'
122- REVISION = 'revision'
123- TIME = 'timestamp'
124- SOURCE_SDK_TYPE = 'clientEngine'
125- SOURCE_SDK_VERSION = 'clientVersion'
126- ACTION_TRIGGERED = 'actionTriggered'
127- IS_GLOBAL_HOLDBACK = 'isGlobalHoldback'
128- IS_LAYER_HOLDBACK = 'isLayerHoldback'
129-
130- def _add_attributes (self , attributes ):
131- """ Add attribute(s) information to the event.
132-
133- Args:
134- attributes: Dict representing user attributes and values which need to be recorded.
135- """
136-
137- self .params [self .EventParams .USER_FEATURES ] = []
138- if not attributes :
139- return
140-
141- for attribute_key in attributes .keys ():
142- attribute_value = attributes .get (attribute_key )
143- # Omit falsy attribute values
144- if attribute_value :
145- attribute = self .config .get_attribute (attribute_key )
146- if attribute :
147- self .params [self .EventParams .USER_FEATURES ].append ({
148- 'id' : attribute .id ,
149- 'name' : attribute_key ,
150- 'type' : 'custom' ,
151- 'value' : attribute_value ,
152- 'shouldIndex' : True
153- })
154-
155- def _add_source (self ):
156- """ Add source information to the event. """
157-
158- self .params [self .EventParams .SOURCE_SDK_TYPE ] = 'python-sdk'
159- self .params [self .EventParams .SOURCE_SDK_VERSION ] = version .__version__
160-
161- def _add_revision (self ):
162- """ Add datafile revision information to the event. """
163- self .params [self .EventParams .REVISION ] = self .config .get_revision ()
164-
165- def _add_time (self ):
166- """ Add time information to the event. """
167-
168- self .params [self .EventParams .TIME ] = int (round (time .time () * 1000 ))
169-
170- def _add_required_params_for_impression (self , experiment , variation_id ):
171- """ Add parameters that are required for the impression event to register.
172-
173- Args:
174- experiment: Experiment for which impression needs to be recorded.
175- variation_id: ID for variation which would be presented to user.
176- """
177-
178- self .params [self .EventParams .IS_GLOBAL_HOLDBACK ] = False
179- self .params [self .EventParams .LAYER_ID ] = experiment .layerId
180- self .params [self .EventParams .DECISION ] = {
181- self .EventParams .EXPERIMENT_ID : experiment .id ,
182- self .EventParams .VARIATION_ID : variation_id ,
183- self .EventParams .IS_LAYER_HOLDBACK : False
184- }
185-
186- def _add_required_params_for_conversion (self , event_key , event_tags , decisions ):
187- """ Add parameters that are required for the conversion event to register.
188-
189- Args:
190- event_key: Key representing the event which needs to be recorded.
191- event_tags: Dict representing metadata associated with the event.
192- decisions: List of tuples representing valid experiments IDs and variation IDs.
193- """
194-
195- self .params [self .EventParams .IS_GLOBAL_HOLDBACK ] = False
196- self .params [self .EventParams .EVENT_FEATURES ] = []
197- self .params [self .EventParams .EVENT_METRICS ] = []
198-
199- if event_tags :
200- event_values = []
201-
202- revenue_value = event_tag_utils .get_revenue_value (event_tags )
203- if revenue_value is not None :
204- event_values .append ({
205- 'name' : event_tag_utils .REVENUE_METRIC_TYPE ,
206- 'value' : revenue_value
207- })
208-
209- numeric_value = event_tag_utils .get_numeric_value (event_tags , self .config .logger )
210- if numeric_value is not None :
211- event_values .append ({
212- 'name' : event_tag_utils .NUMERIC_METRIC_TYPE ,
213- 'value' : numeric_value
214- })
215-
216- self .params [self .EventParams .EVENT_METRICS ] = event_values
217- for event_tag_id in event_tags .keys ():
218- event_tag_value = event_tags .get (event_tag_id )
219- if event_tag_value is None :
220- continue
221-
222- event_feature = {
223- 'name' : event_tag_id ,
224- 'type' : 'custom' ,
225- 'value' : event_tag_value ,
226- 'shouldIndex' : False ,
227- }
228- self .params [self .EventParams .EVENT_FEATURES ].append (event_feature )
229-
230- self .params [self .EventParams .LAYER_STATES ] = []
231- for experiment_id , variation_id in decisions :
232- experiment = self .config .get_experiment_from_id (experiment_id )
233- self .params [self .EventParams .LAYER_STATES ].append ({
234- self .EventParams .LAYER_ID : experiment .layerId ,
235- self .EventParams .REVISION : self .config .get_revision (),
236- self .EventParams .ACTION_TRIGGERED : True ,
237- self .EventParams .DECISION : {
238- self .EventParams .EXPERIMENT_ID : experiment .id ,
239- self .EventParams .VARIATION_ID : variation_id ,
240- self .EventParams .IS_LAYER_HOLDBACK : False
241- }
242- })
243-
244- self .params [self .EventParams .EVENT_ID ] = self .config .get_event (event_key ).id
245- self .params [self .EventParams .EVENT_NAME ] = event_key
246-
247- def create_impression_event (self , experiment , variation_id , user_id , attributes ):
248- """ Create impression Event to be sent to the logging endpoint.
249-
250- Args:
251- experiment: Experiment for which impression needs to be recorded.
252- variation_id: ID for variation which would be presented to user.
253- user_id: ID for user.
254- attributes: Dict representing user attributes and values which need to be recorded.
255-
256- Returns:
257- Event object encapsulating the impression event.
258- """
259-
260- self .params = {}
261- self ._add_common_params (user_id , attributes )
262- self ._add_required_params_for_impression (experiment , variation_id )
263- return Event (self .IMPRESSION_ENDPOINT ,
264- self .params ,
265- http_verb = self .HTTP_VERB ,
266- headers = self .HTTP_HEADERS )
267-
268- def create_conversion_event (self , event_key , user_id , attributes , event_tags , decisions ):
269- """ Create conversion Event to be sent to the logging endpoint.
270-
271- Args:
272- event_key: Key representing the event which needs to be recorded.
273- user_id: ID for user.
274- attributes: Dict representing user attributes and values.
275- event_tags: Dict representing metadata associated with the event.
276- decisions: List of tuples representing experiments IDs and variation IDs.
277-
278- Returns:
279- Event object encapsulating the conversion event.
280- """
281-
282- self .params = {}
283- self ._add_common_params (user_id , attributes )
284- self ._add_required_params_for_conversion (event_key , event_tags , decisions )
285- return Event (self .CONVERSION_ENDPOINT ,
286- self .params ,
287- http_verb = self .HTTP_VERB ,
288- headers = self .HTTP_HEADERS )
289-
290-
291- class EventBuilderV3 (BaseEventBuilder ):
292100 """ Class which encapsulates methods to build events for tracking
293101 impressions and conversions using the new V3 event API (batch). """
294102
0 commit comments