Forced decision methods for the JavaScript (Browser) SDK v6
Use the forced decision methods for the JavaScript (Browser) SDK to force users into a specific variation in Optimizely Feature Experimentation.
Warning
This content covers the Feature Experimentation JavaScript (Browser) SDK v6 features currently in pre-production testing and is subject to change before release
For the latest released version, see JavaScript (Browser) SDK.
These methods help test and debug various flows of your client applications by forcing users into a specific variation.
The JavaScript (Browser) SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, audience conditions and traffic allocation are ignored, and the SDK returns the forced decision immediately before making normal decisions.
Minimum SDK version
v6.0.0+
Scenarios
The following describes specific scenarios the JavaScript (Browser) SDK follows:
Flag-to-Decision
- SDK looks up at the beginning of any decide call 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
- SDK looks 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
- SDK looks 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.
Warning
You must associate your variations to a flag rule before calling any forced decision methods.
On forced decisions, the JavaScript (Browser) SDK fires impression events and notifications just like other normal decisions (unless disabled by the disableDecisionEvent option).
Note
These forced decisions are not persistent and are cleared when the OptimizelyUserContext is re-initialized.
For more information about each method see the following sections:
Version
6.0.0 and higher
OptimizelyDecisionContext
export interface OptimizelyDecisionContext {
flagKey: string;
ruleKey?: string;
}
OptimizelyForcedDecision
export interface OptimizelyForcedDecision {
variationKey: string;
}
Set forced decision method (setForcedDecision()
)
setForcedDecision()
)Sets a forced decision (variationKey
) for a given OptimizelyDecisionContext
.
Parameters
This table lists the required and optional parameters for the Javascript (Browser) SDK.
Parameter | Type | Description |
---|---|---|
context required | Interface | An instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to set. |
decision required | Interface | An instance of OptimizelyForcedDecision with the required variationKey for the forced decision you want to set. |
Returns
A boolean value that indicates if setting the forced decision (variationKey
) was completed successfully.
Example
See the full Javascript (Browser) SDK example in the full code example section.
Get Forced Decision Method (getForcedDecision()
)
getForcedDecision()
)Returns the forced decision (variationKey
) for a given OptimizelyDecisionContext
. Returns the OptimizelyForcedDecision
instance or null if there is no matching item.
Parameters
This table lists the required and optional parameters for the Javascript (Browser) SDK.
Parameter | Type | Description |
---|---|---|
context required | Interface | An instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to get. |
Returns
A forced decision OptimizelyForcedDecision
instance for the context or null if there is no matching item.
Example
See the full Javascript (Browser) SDK example.
Remove forced decision method (removeForcedDecision()
)
removeForcedDecision()
)Removes the forced decision (variationKey
) for a given OptimizelyDecisionContext
.
Parameters
This table lists the required and optional parameters for the Javascript (Browser) SDK.
Parameters | Type | Description |
---|---|---|
context required | struct | An instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to remove. |
Returns
A success or failure boolean status if the forced decision (variationKey
) was removed.
Example
See the full Javascript (Browser) SDK example.
Remove all forced decisions method (removeAllForcedDecisions()
)
removeAllForcedDecisions()
)Parameters
There are no required parameters for removeAllForcedDecisions()
.
Returns
A success or failure boolean status.
Example
See the full Javascript (Browser) SDK example.
Full code example
import { createInstance, createPollingProjectConfigManager } from '@optimizely/optimizely-sdk';
const SDK_KEY = "YOUR_SDK_KEY";
const pollingConfigManager = createPollingProjectConfigManager({
sdkKey: SDK_KEY,
});
const optimizely = createInstance({
projectConfigManager: pollingConfigManager,
});
if (optimizely) {
optimizely.onReady().then(() => {
const attributes = { logged_in: true };
const user = optimizely.createUserContext('user123', attributes);
if (!user) {
throw new Error('failed to create user context');
}
let result, decision, forcedDecision;
// set a forced decision for a flag
result = user.setForcedDecision({flagKey: 'flag-1'}, {variationKey: 'off'});
decision = user.decide('flag-1');
// set a forced decision for an ab-test rule
result = user.setForcedDecision({flagKey: 'flag-2', ruleKey: 'ab-test-1'}, {variationKey: 'variation-b'});
decision = user.decide('flag-2');
// set a forced variation for a delivery rule
result = user.setForcedDecision({flagKey: 'flag-3', ruleKey: 'delivery-1'}, {variationKey: 'on'});
decision = user.decide('flag-3');
// get forced variations
forcedDecision = user.getForcedDecision({flagKey: 'flag-2'});
// remove forced variations
result = user.removeForcedDecision({flagKey: 'flag-2', ruleKey: 'ab-test-1'});
user.removeAllForcedDecisions();
user.decideAll();
}).catch((err) => {
console.log(err);
// handle error
});
} else {
// handle instantiation error
}
See also
Updated about 22 hours ago