Skip to content

Jongsy/fix multiple experiment event params #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions optimizely/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,18 @@ def _get_required_params_for_conversion(self, event_key, event_tags, decisions):
Returns:
Dict consisting of the decisions and events info for conversion event.
"""

snapshot = {}
for experiment_id, variation_id in decisions:
snapshot = {}

experiment = self.config.get_experiment_from_id(experiment_id)

if variation_id:
snapshot[self.EventParams.DECISIONS] = [{

snapshot.setdefault(self.EventParams.DECISIONS, []).append({
self.EventParams.EXPERIMENT_ID: experiment_id,
self.EventParams.VARIATION_ID: variation_id,
self.EventParams.CAMPAIGN_ID: experiment.layerId
}]
})

event_dict = {
self.EventParams.EVENT_ID: self.config.get_event(event_key).id,
Expand All @@ -273,10 +274,9 @@ def _get_required_params_for_conversion(self, event_key, event_tags, decisions):

if len(event_tags) > 0:
event_dict[self.EventParams.TAGS] = event_tags
snapshot.setdefault(self.EventParams.EVENTS, []).append(event_dict)

snapshot[self.EventParams.EVENTS] = [event_dict]

return snapshot
return snapshot

def create_impression_event(self, experiment, variation_id, user_id, attributes):
""" Create impression Event to be sent to the logging endpoint.
Expand Down Expand Up @@ -319,7 +319,7 @@ def create_conversion_event(self, event_key, user_id, attributes, event_tags, de
conversion_params = self._get_required_params_for_conversion(event_key, event_tags, decisions)

params[self.EventParams.USERS][0][self.EventParams.SNAPSHOTS].append(conversion_params)

print(params)
return Event(self.EVENTS_URL,
params,
http_verb=self.HTTP_VERB,
Expand Down
151 changes: 149 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class BaseTest(unittest.TestCase):

def setUp(self):
def setUp(self, config_dict='config_dict'):
self.config_dict = {
'revision': '42',
'version': '2',
Expand Down Expand Up @@ -375,5 +375,152 @@ def setUp(self):
}]
}

self.optimizely = optimizely.Optimizely(json.dumps(self.config_dict))
self.config_dict_with_multiple_experiments = {
'revision': '42',
'version': '2',
'events': [{
'key': 'test_event',
'experimentIds': ['111127', '111130'],
'id': '111095'
}, {
'key': 'Total Revenue',
'experimentIds': ['111127'],
'id': '111096'
}],
'experiments': [{
'key': 'test_experiment',
'status': 'Running',
'forcedVariations': {
'user_1': 'control',
'user_2': 'control'
},
'layerId': '111182',
'audienceIds': ['11154'],
'trafficAllocation': [{
'entityId': '111128',
'endOfRange': 4000
}, {
'entityId': '',
'endOfRange': 5000
}, {
'entityId': '111129',
'endOfRange': 9000
}],
'id': '111127',
'variations': [{
'key': 'control',
'id': '111128'
}, {
'key': 'variation',
'id': '111129'
}]
}, {
'key': 'test_experiment_2',
'status': 'Running',
'forcedVariations': {
'user_1': 'control',
'user_2': 'control'
},
'layerId': '111182',
'audienceIds': ['11154'],
'trafficAllocation': [{
'entityId': '111131',
'endOfRange': 4000
}, {
'entityId': '',
'endOfRange': 5000
}, {
'entityId': '111132',
'endOfRange': 9000
}],
'id': '111130',
'variations': [{
'key': 'control',
'id': '111133'
}, {
'key': 'variation',
'id': '111134'
}]
}],
'groups': [{
'id': '19228',
'policy': 'random',
'experiments': [{
'id': '32222',
'key': 'group_exp_1',
'status': 'Running',
'audienceIds': [],
'layerId': '111183',
'variations': [{
'key': 'group_exp_1_control',
'id': '28901'
}, {
'key': 'group_exp_1_variation',
'id': '28902'
}],
'forcedVariations': {
'user_1': 'group_exp_1_control',
'user_2': 'group_exp_1_control'
},
'trafficAllocation': [{
'entityId': '28901',
'endOfRange': 3000
}, {
'entityId': '28902',
'endOfRange': 9000
}]
}, {
'id': '32223',
'key': 'group_exp_2',
'status': 'Running',
'audienceIds': [],
'layerId': '111184',
'variations': [{
'key': 'group_exp_2_control',
'id': '28905'
}, {
'key': 'group_exp_2_variation',
'id': '28906'
}],
'forcedVariations': {
'user_1': 'group_exp_2_control',
'user_2': 'group_exp_2_control'
},
'trafficAllocation': [{
'entityId': '28905',
'endOfRange': 8000
}, {
'entityId': '28906',
'endOfRange': 10000
}]
}],
'trafficAllocation': [{
'entityId': '32222',
"endOfRange": 3000
}, {
'entityId': '32223',
'endOfRange': 7500
}]
}],
'accountId': '12001',
'attributes': [{
'key': 'test_attribute',
'id': '111094'
}],
'audiences': [{
'name': 'Test attribute users 1',
'conditions': '["and", ["or", ["or", '
'{"name": "test_attribute", "type": "custom_attribute", "value": "test_value_1"}]]]',
'id': '11154'
}, {
'name': 'Test attribute users 2',
'conditions': '["and", ["or", ["or", '
'{"name": "test_attribute", "type": "custom_attribute", "value": "test_value_2"}]]]',
'id': '11159'
}],
'projectId': '111001'
}

config = getattr(self, config_dict)
self.optimizely = optimizely.Optimizely(json.dumps(config))
self.project_config = self.optimizely.config
79 changes: 78 additions & 1 deletion tests/test_event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_init(self):
class EventBuilderTest(base.BaseTest):

def setUp(self):
base.BaseTest.setUp(self)
base.BaseTest.setUp(self, 'config_dict_with_multiple_experiments')
self.event_builder = self.optimizely.event_builder

def _validate_event_object(self, event_obj, expected_url, expected_params, expected_verb, expected_headers):
Expand Down Expand Up @@ -655,3 +655,80 @@ def test_create_conversion_event__with_invalid_event_tags(self):
expected_params,
event_builder.EventBuilder.HTTP_VERB,
event_builder.EventBuilder.HTTP_HEADERS)

def test_create_conversion_event__when_event_is_used_in_multiple_experiments(self):
""" Test that create_conversion_event creates Event object
with right params when event tags are provided. """

# base.BaseTest.setUp(self)
# self.event_builder = self.optimizely.event_builder

expected_params = {
'client_version': version.__version__,
'project_id': '111001',
'visitors': [{
'attributes': [{
'entity_id': '111094',
'type': 'custom',
'value': 'test_value',
'key': 'test_attribute'
}],
'visitor_id': 'test_user',
'snapshots': [{
'decisions': [{
'variation_id': '111129',
'experiment_id': '111127',
'campaign_id': '111182'
}, {
'experiment_id': '111130',
'variation_id': '111131',
'campaign_id': '111182'
}
],
'events': [{
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'tags': {
'non-revenue': 'abc',
'revenue': 4200,
'value': 1.234
},
'timestamp': 42123,
'revenue': 4200,
'value': 1.234,
'key': 'test_event',
'entity_id': '111095'
}, {
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'tags': {
'non-revenue': 'abc',
'revenue': 4200,
'value': 1.234
},
'timestamp': 42123,
'revenue': 4200,
'value': 1.234,
'key': 'test_event',
'entity_id': '111095'
}]
}]
}],
'account_id': '12001',
'client_name': 'python-sdk',
'anonymize_ip': False,
'revision': '42'
}

with mock.patch('time.time', return_value=42.123), \
mock.patch('uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'):
event_obj = self.event_builder.create_conversion_event(
'test_event',
'test_user',
{'test_attribute': 'test_value'},
{'revenue': 4200, 'value': 1.234, 'non-revenue': 'abc'},
[('111127', '111129'), ('111130', '111131')]
)
self._validate_event_object(event_obj,
event_builder.EventBuilder.EVENTS_URL,
expected_params,
event_builder.EventBuilder.HTTP_VERB,
event_builder.EventBuilder.HTTP_HEADERS)