1- # Copyright 2016, Optimizely
1+ # Copyright 2016-2017 , 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
1919from . import project_config
2020from . import version
2121from .helpers import enums
22+ from .helpers import event_tag_utils
2223
2324
2425class Event (object ):
@@ -225,19 +226,22 @@ def create_impression_event(self, experiment, variation_id, user_id, attributes)
225226 return Event (self .OFFLINE_API_PATH .format (project_id = self .params [self .EventParams .PROJECT_ID ]),
226227 self .params )
227228
228- def create_conversion_event (self , event_key , user_id , attributes , event_value , valid_experiments ):
229+ def create_conversion_event (self , event_key , user_id , attributes , event_tags , valid_experiments ):
229230 """ Create conversion Event to be sent to the logging endpoint.
230231
231232 Args:
232233 event_key: Event key representing the event which needs to be recorded.
233234 user_id: ID for user.
234- event_value: Value associated with the event. Can be used to represent revenue in cents.
235+ attributes: Dict representing user attributes and values.
236+ event_tags: Dict representing metadata associated with the event.
235237 valid_experiments: List of tuples representing valid experiments for the event.
236238
237239 Returns:
238240 Event object encapsulating the conversion event.
239241 """
240242
243+ event_value = event_tag_utils .get_revenue_value (event_tags )
244+
241245 self .params = {}
242246 self ._add_common_params (user_id , attributes )
243247 self ._add_conversion_goal (event_key , event_value )
@@ -254,7 +258,6 @@ class EventBuilderV2(BaseEventBuilder):
254258 CONVERSION_ENDPOINT = 'https://logx.optimizely.com/log/event'
255259 HTTP_VERB = 'POST'
256260 HTTP_HEADERS = {'Content-Type' : 'application/json' }
257- EVENT_VALUE_METRIC = 'revenue'
258261
259262 class EventParams (object ):
260263 ACCOUNT_ID = 'accountId'
@@ -329,25 +332,40 @@ def _add_required_params_for_impression(self, experiment, variation_id):
329332 self .EventParams .IS_LAYER_HOLDBACK : False
330333 }
331334
332- def _add_required_params_for_conversion (self , event_key , user_id , event_value , valid_experiments ):
335+ def _add_required_params_for_conversion (self , event_key , user_id , event_tags , valid_experiments ):
333336 """ Add parameters that are required for the conversion event to register.
334337
335338 Args:
336339 event_key: Key representing the event which needs to be recorded.
337340 user_id: ID for user.
338- event_value: Value associated with the event. Can be used to represent revenue in cents .
341+ event_tags: Dict representing metadata associated with the event.
339342 valid_experiments: List of tuples representing valid experiments for the event.
340343 """
341344
342345 self .params [self .EventParams .IS_GLOBAL_HOLDBACK ] = False
343346 self .params [self .EventParams .EVENT_FEATURES ] = []
344347 self .params [self .EventParams .EVENT_METRICS ] = []
345348
346- if event_value :
347- self .params [self .EventParams .EVENT_METRICS ] = [{
348- 'name' : self .EVENT_VALUE_METRIC ,
349- 'value' : event_value
350- }]
349+ if event_tags :
350+ event_value = event_tag_utils .get_revenue_value (event_tags )
351+ if event_value is not None :
352+ self .params [self .EventParams .EVENT_METRICS ] = [{
353+ 'name' : event_tag_utils .EVENT_VALUE_METRIC ,
354+ 'value' : event_value
355+ }]
356+
357+ for event_tag_id in event_tags .keys ():
358+ event_tag_value = event_tags .get (event_tag_id )
359+ if event_tag_value is None :
360+ continue
361+
362+ event_feature = {
363+ 'id' : event_tag_id ,
364+ 'type' : 'custom' ,
365+ 'value' : event_tag_value ,
366+ 'shouldIndex' : False ,
367+ }
368+ self .params [self .EventParams .EVENT_FEATURES ].append (event_feature )
351369
352370 self .params [self .EventParams .LAYER_STATES ] = []
353371 for experiment in valid_experiments :
@@ -387,23 +405,23 @@ def create_impression_event(self, experiment, variation_id, user_id, attributes)
387405 http_verb = self .HTTP_VERB ,
388406 headers = self .HTTP_HEADERS )
389407
390- def create_conversion_event (self , event_key , user_id , attributes , event_value , valid_experiments ):
408+ def create_conversion_event (self , event_key , user_id , attributes , event_tags , valid_experiments ):
391409 """ Create conversion Event to be sent to the logging endpoint.
392410
393411 Args:
394412 event_key: Key representing the event which needs to be recorded.
395413 user_id: ID for user.
396- event_value: Value associated with the event. Can be used to represent revenue in cents.
397- valid_experiments: List of tuples representing valid experiments for the event.
398414 attributes: Dict representing user attributes and values.
415+ event_tags: Dict representing metadata associated with the event.
416+ valid_experiments: List of tuples representing valid experiments for the event.
399417
400418 Returns:
401419 Event object encapsulating the conversion event.
402420 """
403421
404422 self .params = {}
405423 self ._add_common_params (user_id , attributes )
406- self ._add_required_params_for_conversion (event_key , user_id , event_value , valid_experiments )
424+ self ._add_required_params_for_conversion (event_key , user_id , event_tags , valid_experiments )
407425 return Event (self .CONVERSION_ENDPOINT ,
408426 self .params ,
409427 http_verb = self .HTTP_VERB ,
0 commit comments