Skip to content

Commit 3cbea1f

Browse files
Python: Exclude CMAB experiments from user profile updates and update related tests
1 parent 343bdcf commit 3cbea1f

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

optimizely/decision_service.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,15 @@ def get_variation(
413413
- 'error': Boolean indicating if an error occurred during the decision process.
414414
"""
415415
user_id = user_context.user_id
416-
if options:
416+
417+
if experiment.cmab:
418+
# CMAB experiments are excluded from user profile storage to allow dynamic decision-making
419+
ignore_user_profile = True
420+
self.logger.debug(
421+
f'Skipping user profile service for CMAB experiment "{experiment.key}". '
422+
f'CMAB decisions are dynamic and not stored for sticky bucketing.'
423+
)
424+
elif options:
417425
ignore_user_profile = OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE in options
418426
else:
419427
ignore_user_profile = False
@@ -529,18 +537,11 @@ def get_variation(
529537
self.logger.info(message)
530538
decide_reasons.append(message)
531539
# Store this new decision and return the variation for the user
532-
# CMAB experiments are excluded from user profile storage to allow dynamic decision-making
533540
if user_profile_tracker is not None and not ignore_user_profile:
534-
if not experiment.cmab:
535-
try:
536-
user_profile_tracker.update_user_profile(experiment, variation)
537-
except:
538-
self.logger.exception(f'Unable to save user profile for user "{user_id}".')
539-
else:
540-
self.logger.debug(
541-
f'Skipping user profile update for CMAB experiment "{experiment.key}". '
542-
f'CMAB decisions are dynamic and not stored for sticky bucketing.'
543-
)
541+
try:
542+
user_profile_tracker.update_user_profile(experiment, variation)
543+
except:
544+
self.logger.exception(f'Unable to save user profile for user "{user_id}".')
544545
return {
545546
'cmab_uuid': cmab_uuid,
546547
'error': False,

tests/test_decision_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,8 @@ def test_get_variation_cmab_experiment_does_not_save_user_profile(self):
11451145
mock_update_profile.assert_not_called()
11461146

11471147
# Verify debug log was called to explain CMAB exclusion
1148-
mock_logger.debug.assert_called_with(
1149-
'Skipping user profile update for CMAB experiment "cmab_experiment". '
1148+
mock_logger.debug.assert_any_call(
1149+
'Skipping user profile service for CMAB experiment "cmab_experiment". '
11501150
'CMAB decisions are dynamic and not stored for sticky bucketing.'
11511151
)
11521152

0 commit comments

Comments
 (0)