Skip to content

Commit 25dbca5

Browse files
committed
Sending event when variation of an experiment has feature disabled (#128)
1 parent f38cc21 commit 25dbca5

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

optimizely/optimizely.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,20 @@ def is_feature_enabled(self, feature_key, user_id, attributes=None):
387387
return False
388388

389389
decision = self.decision_service.get_variation_for_feature(feature, user_id, attributes)
390-
if decision.variation and decision.variation.featureEnabled:
391-
self.logger.log(enums.LogLevels.INFO, 'Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
390+
if decision.variation:
392391
# Send event if Decision came from an experiment.
393392
if decision.source == decision_service.DECISION_SOURCE_EXPERIMENT:
394393
self._send_impression_event(decision.experiment,
395394
decision.variation,
396395
user_id,
397396
attributes)
398397

399-
return True
398+
if decision.variation.featureEnabled:
399+
self.logger.log(enums.LogLevels.INFO, 'Feature "%s" is enabled for user "%s".' % (feature_key, user_id))
400+
return True
401+
else:
402+
self.logger.log(enums.LogLevels.INFO, 'Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
403+
return False
400404

401405
self.logger.log(enums.LogLevels.INFO, 'Feature "%s" is not enabled for user "%s".' % (feature_key, user_id))
402406
return False

tests/test_optimizely.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,9 @@ def test_is_feature_enabled__returns_false_for_invalid_feature(self):
11821182
# Check that no event is sent
11831183
self.assertEqual(0, mock_dispatch_event.call_count)
11841184

1185-
def test_is_feature_enabled__returns_true_for_feature_experiment_if_property_featureEnabled_is_true(self):
1185+
def test_is_feature_enabled__returns_true_for_feature_experiment_if_feature_enabled_for_variation(self):
11861186
""" Test that the feature is enabled for the user if bucketed into variation of an experiment and
1187-
the variation's featureEnabled property is True.
1188-
Also confirm that impression event is dispatched. """
1187+
the variation's featureEnabled property is True. Also confirm that impression event is dispatched. """
11891188

11901189
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
11911190
project_config = opt_obj.config
@@ -1241,10 +1240,9 @@ def test_is_feature_enabled__returns_true_for_feature_experiment_if_property_fea
12411240
'https://logx.optimizely.com/v1/events',
12421241
expected_params, 'POST', {'Content-Type': 'application/json'})
12431242

1244-
def test_is_feature_enabled__returns_false_for_feature_experiment_if_property_featureEnabled_is_false(self):
1243+
def test_is_feature_enabled__returns_false_for_feature_experiment_if_feature_disabled_for_variation(self):
12451244
""" Test that the feature is disabled for the user if bucketed into variation of an experiment and
1246-
the variation's featureEnabled property is False.
1247-
Also confirm that impression event is not dispatched. """
1245+
the variation's featureEnabled property is False. Also confirm that impression event is dispatched. """
12481246

12491247
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
12501248
project_config = opt_obj.config
@@ -1269,13 +1267,41 @@ def test_is_feature_enabled__returns_false_for_feature_experiment_if_property_fe
12691267

12701268
mock_decision.assert_called_once_with(feature, 'test_user', None)
12711269

1272-
# Check that impression event is not sent
1273-
self.assertEqual(0, mock_dispatch_event.call_count)
1270+
# Check that impression event is sent
1271+
expected_params = {
1272+
'account_id': '12001',
1273+
'project_id': '111111',
1274+
'visitors': [{
1275+
'visitor_id': 'test_user',
1276+
'attributes': [],
1277+
'snapshots': [{
1278+
'decisions': [{
1279+
'variation_id': '111128',
1280+
'experiment_id': '111127',
1281+
'campaign_id': '111182'
1282+
}],
1283+
'events': [{
1284+
'timestamp': 42000,
1285+
'entity_id': '111182',
1286+
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
1287+
'key': 'campaign_activated',
1288+
}]
1289+
}]
1290+
}],
1291+
'client_version': version.__version__,
1292+
'client_name': 'python-sdk',
1293+
'anonymize_ip': False,
1294+
'revision': '1'
1295+
}
1296+
# Check that impression event is sent
1297+
self.assertEqual(1, mock_dispatch_event.call_count)
1298+
self._validate_event_object(mock_dispatch_event.call_args[0][0],
1299+
'https://logx.optimizely.com/v1/events',
1300+
expected_params, 'POST', {'Content-Type': 'application/json'})
12741301

1275-
def test_is_feature_enabled__returns_true_for_feature_rollout_if_property_featureEnabled_is_true(self):
1302+
def test_is_feature_enabled__returns_true_for_feature_rollout_if_feature_enabled(self):
12761303
""" Test that the feature is enabled for the user if bucketed into variation of a rollout and
1277-
the variation's featureEnabled property is True.
1278-
Also confirm that no impression event is dispatched. """
1304+
the variation's featureEnabled property is True. Also confirm that no impression event is dispatched. """
12791305

12801306
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
12811307
project_config = opt_obj.config
@@ -1303,10 +1329,9 @@ def test_is_feature_enabled__returns_true_for_feature_rollout_if_property_featur
13031329
# Check that impression event is not sent
13041330
self.assertEqual(0, mock_dispatch_event.call_count)
13051331

1306-
def test_is_feature_enabled__returns_false_for_feature_rollout_if_property_featureEnabled_is_false(self):
1332+
def test_is_feature_enabled__returns_false_for_feature_rollout_if_feature_disabled(self):
13071333
""" Test that the feature is disabled for the user if bucketed into variation of a rollout and
1308-
the variation's featureEnabled property is False.
1309-
Also confirm that no impression event is dispatched. """
1334+
the variation's featureEnabled property is False. Also confirm that no impression event is dispatched. """
13101335

13111336
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
13121337
project_config = opt_obj.config

0 commit comments

Comments
 (0)