Skip to content

Commit b901c5f

Browse files
update: modify get_variation to return VariationResult and adjust related logic for improved variation handling
1 parent c2b3d96 commit b901c5f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

optimizely/optimizely.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .decision.optimizely_decide_option import OptimizelyDecideOption
3030
from .decision.optimizely_decision import OptimizelyDecision
3131
from .decision.optimizely_decision_message import OptimizelyDecisionMessage
32-
from .decision_service import Decision
32+
from .decision_service import Decision, VariationResult
3333
from .error_handler import NoOpErrorHandler, BaseErrorHandler
3434
from .event import event_factory, user_event_factory
3535
from .event.event_processor import BatchEventProcessor, BaseEventProcessor
@@ -535,8 +535,10 @@ def activate(self, experiment_key: str, user_id: str, attributes: Optional[UserA
535535
self.logger.error(enums.Errors.INVALID_PROJECT_CONFIG.format('activate'))
536536
return None
537537

538-
variation_key = self.get_variation(experiment_key, user_id, attributes)
539-
538+
variation_result = self.get_variation(experiment_key, user_id, attributes)
539+
variation_key = None
540+
if variation_result:
541+
variation_key = variation_result['variation'].key
540542
if not variation_key:
541543
self.logger.info(f'Not activating user "{user_id}".')
542544
return None
@@ -612,17 +614,18 @@ def track(
612614

613615
def get_variation(
614616
self, experiment_key: str, user_id: str, attributes: Optional[UserAttributes] = None
615-
) -> Optional[str]:
616-
""" Gets variation where user will be bucketed.
617+
) -> Optional[VariationResult]:
618+
"""
619+
Returns the variation result for the given user in the specified experiment.
617620
618621
Args:
619-
experiment_key: Experiment for which user variation needs to be determined.
620-
user_id: ID for user.
621-
attributes: Dict representing user attributes.
622+
experiment_key: The key identifying the experiment.
623+
user_id: The user ID.
624+
attributes: Optional dictionary of user attributes.
622625
623626
Returns:
624-
Variation key representing the variation the user will be bucketed in.
625-
None if user is not in experiment or if experiment is not Running.
627+
A VariationResult object containing the variation assigned to the user, or None if the user is not
628+
bucketed into any variation or the experiment is not running.
626629
"""
627630

628631
if not self.is_valid:
@@ -675,7 +678,7 @@ def get_variation(
675678
{'experiment_key': experiment_key, 'variation_key': variation_key},
676679
)
677680

678-
return variation_key
681+
return variation_result
679682

680683
def is_feature_enabled(self, feature_key: str, user_id: str, attributes: Optional[UserAttributes] = None) -> bool:
681684
""" Returns true if the feature is enabled for the given user.

0 commit comments

Comments
 (0)