Forced decision methods for the Python SDK
Describes the Forced Decision methods, which you can use to force users into a specific variation in Optimizely Feature Experimentation.
These methods will help test and debug various flows of your client applications by forcing users into a specific variation.
The Optimizely Feature Experimentation Python SDK will check forced decisions before making any decisions. If a matching item is found for the requested flag, the Python SDK will return the forced decision immediately (audience conditions andtraffic allocations are ignored) before making normal decisions:
The following describes specific scenarios the Python SDK will follow:
- Flag-to-Decision – The Python SDK will look up at the beginning of any
decidecall for the given flag. If a matching Flag-to-Decision forced decision is found for the flag, it returns the decision. - Experiment-Rule-to-Decision – The Python SDK will look up at the beginning of the decision for the given experiment rule (of the flag key). If a matching Experiment-Rule-to-Decision forced decision is found for the flag, it returns the decision.
- Delivery-Rule-to-Decision – The Python SDK will look up at the beginning of the decision for the given delivery rule (of the flag key). If a matching Delivery-Rule-to-Decision forced decision is found, it returns the decision.
❗️
WarningYou must associate your variation(s) to aflag rule before calling any forced decision methods.
On forced decisions, SDK will fire impression events and notifications just like other normal decisions (unless disabled by theDISABLE_DECISION_EVENT option).
📘
NoteThese forced decisions are not persistent and will be cleared when theOptimizelyUserContext is re-initialized.
For more information about each method, click on the method name below:
OptimizelyDecisionContext
class OptimizelyDecisionContext(object): def __init__(self, flag_key, rule_key):OptimizelyForcedDecision
class OptimizelyForcedDecision(object): def __init__(self, variation_key):Set forced decision method - set_forced_decision()
Version
4.0.0
Description
Sets a forced decision (variation_key) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the Python SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
decision | Class | An instance of |
Returns
A boolean value that indicates if setting the forced decision (variation_key) was completed successfully.
Example
See the full Python SDK examplehere.
Get forced decision method - get_forced_decision()
Version
4.0.0
Description
Returns the forced decision (variation_key) for a givenOptimizelyDecisionContext. Returns the null if there is no matching item.
Parameters
This table lists the required and optional parameters for the Python SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
Returns
A forced decisionOptimizelyForcedDecision instance for the context or nil if there is no matching item.
Example
See the full Python SDK examplehere.
Remove forced decision method - remove_forced_decision()
Version
4.0.0
Description
Removes the forced decision (variation_key) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the Python SDK.
Parameters | Type | Description |
|---|---|---|
contextrequired | Class | An instance of |
Returns
A success/failure boolean status if the forced decision (variation_key) was removed.
Example
See the full Python SDK examplehere.
Remove all forced decisions method - remove_all_forced_decisions()
Version
4.0.0
Description
Removes all forced decisions (variation_key) for the user context.
Parameters
This table lists the required and optional parameters for the Python SDK.
| Parameters | Type | Description |
|---|---|---|
| None | N/A | N/A |
Returns
A success/failure boolean status.
Example
See the full Python SDK examplehere.
Full code example
from optimizely import optimizely, optimizely_user_contextoptimizely_client = optimizely.Optimizely(sdk_key="<YOUR_SDK_KEY>")user = optimizely_client.create_user_context("test_user", attributes)flag_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1",None)flag_and_ab_test_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1","ab-test-1")flag_and_delivery_rule_context = optimizely_user_context.OptimizelyUserContext.OptimizelyDecisionContext("flag-1","delivery-1")variation_a_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("variation-a")variation_b_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("variation-b")variation_on_forced_decision = optimizely_user_context.OptimizelyUserContext.OptimizelyForcedDecision("on")# set a forced decision for a flagsuccess = user.set_forced_decision(flag_context, variation_a_forced_decision)decision = user.decide("flag-1")# set a forced decision for an ab-test rulesuccess = user.set_forced_decision(flag_and_ab_test_context, variation_b_forced_decision)decision = user.decide("flag-1")# set a forced variation for a delivery rulesuccess = user.set_forced_decision(flag_and_delivery_rule_context, variation_on_forced_decision)decision = user.decide("flag-1")# get forced variationsforced_decision = user.get_forced_decision(flag_context)print(f"[ForcedDecision] variation_key = {forced_decision}")# remove forced variationssuccess = user.remove_forced_decision(flag_and_ab_test_context)success = user.remove_all_forced_decision()See also
Source files
The source files are available onGitHub.
Updated 17 days ago