Decide methods for the JavaScript SDK v6
Overview of the decide methods for the JavaScript (Browser) SDK, which can be used to return a flag decision for a user 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.
Use the Decide methods to return flag decisions for a user. The flag decision includes flag-enabled or disabled status and flag variation.
This page describes the following Decide methods:
Version
v6.0.0+
Decide
Returns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision
object, and contains all data required to deliver the flag rule.
Decide is a method of the UserContext
object. See OptimizelyUserContext
for details.
See the OptimizelyDecision
for details of the returned decision object.
Parameters
The following table describes parameters for the Decide method:
Parameter | Type | Description |
---|---|---|
flagKey | String | The key of the feature flag |
options (optional) | Array | Array of OptimizelyDecideOption enums. See following table. |
OptimizelyDecideOption
The following example shows how you can set options individually on any Decide method, or as global defaults when you instantiate the Optimizely client. See Initialize SDK.
import {
createBatchEventProcessor,
createInstance,
createOdpManager,
createPollingProjectConfigManager,
OptimizelyDecideOption
} from "@optimizely/optimizely-sdk";
const SDK_KEY="YOUR_SDK_KEY";
const pollingConfigManager = createPollingProjectConfigManager({
sdkKey: SDK_KEY,
autoUpdate: true,
updateInterval: 60000, // 1 minute
});
const batchEventProcessor = createBatchEventProcessor();
const odpManager = createOdpManager();
const optimizelyClient = createInstance({
projectConfigManager: pollingConfigManager,
eventProcessor: batchEventProcessor,
odpManager: odpManager,
defaultDecideOptions: [OptimizelyDecideOption.DISABLE_DECISION_EVENT]
});
if (optimizely) {
optimizely.onReady().then(() => {
const user = optimizely.createUserContext('user123');
if (!user) {
throw new Error('failed to create user context');
}
// set additional options in a decide call
const decisionResults = user.decideAll(
[
OptimizelyDecideOption.ENABLED_FLAGS_ONLY,
OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE,
]
);
console.log(decisionResults);
}).catch((err) => {
// handle error
});
} else {
// handle instantiation error
}
The following table shows details for the OptimizelyDecideOption.
OptimizelyDecideOption enum | If set: |
---|---|
OptimizelyDecideOption.DISABLE_DECISION_EVENT | Prevents the visitor from firing an impression while still being served the variation, which disables displaying results of the Decide method on the Results page. This setting can be why the Decision Event Dispatched enum is false in the returned OptimizelyDecision object or the DECIDE notification listener payload. |
OptimizelyDecideOption.ENABLED_FLAGS_ONLY | Return decisions for enabled flags only. This is a valid option only for methods that decide multiple flags, like the Decide All method. This option is ignored if it is invalid. When this option is not set, the SDK returns all decisions regardless of whether the flag is enabled or not. |
OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE | When set, the SDK bypasses user profile service (UPS) (both lookup and save) for the decision. When this option is not set, UPS overrides audience targeting, traffic allocation, and experiment mutual exclusion groups. |
OptimizelyDecideOption.INCLUDE_REASONS | Return log messages in the Reasons field of OptimizelyDecision object. Note that unlike info or debug messages, critical error messages are always returned, regardless of this setting. |
OptimizelyDecideOption.EXCLUDE_VARIABLES | Exclude flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables. |
Returns
The Decide method returns an OptimizelyDecision object. For more information, see OptimizelyDecision
.
If the method encounters a critical error (SDK not ready, invalid flag key, etc), then it returns a decision with a null Variation Key field and populates the Reasons field with error messages (regardless of the Include Reasons option).
Example decision
The following is an example of calling the Decide method and accessing the returned OptimizelyDecision object:
// create the user and decide which flag rule & variation they bucket into
const attributes = {
logged_in: true
};
const user = optimizely.createUserContext('user123', attributes);
const decision = user.decide('product_sort');
// variation. if null, decision fail with a critical error
const variationKey = decision['variationKey'];
if (variationKey === null) {
console.log(' decision error: ', decision['reasons']);
}
// flag enabled state:
const enabled = decision['enabled'];
// String variable value:
const value = decision.variables['sort_method'];
// all variable values
const allVarValues = decision['variables'];
// flag decision reasons
const reasons = decision['reasons'];
// user for which the decision was made
const userContext = decision['userContext'];
Side Effects
Invokes the DECISION
notification listener if this listener is enabled.
Decide all
Returns a map of flag decisions for all active (unarchived) flags for a user.
See OptimizelyDecision
for details.
Parameters
The following table describes parameters for the Decide All method:
Parameter | Type | Description |
---|---|---|
options (optional) | Array | Array of OptimizelyDecideOption enums. See OptimizelyDecideOption. |
Returns
The Decide All method returns a map of OptimizelyDecisions. For more information, see OptimizelyDecision.
If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), then it returns an empty map. If the method detects an error for a specific flag, it returns error messages in the Reasons field of the decision for that flag.
Examples
The following is an example of getting flags for the user with the Decide All call:
const attributes = {
logged_in: true
};
const user = optimizely.createUserContext('user123', attributes);
// make decisions for all active (unarchived) flags in the project for a user
let decisions = user.decideAll();
// or only for enabled flags
decisions = user.decideAll([OptimizelyDecideOption.ENABLED_FLAGS_ONLY]);
const flagKeys = Object.keys(decisions);
const decisionForFlag1 = decisions['flag_1'];
Side effects
Invokes the DECISION
notification listener for each decision if this listener is enabled.
Decide for keys
Returns a map of flag decision for an array of flag keys.
Parameters
The following table describes parameters for the Decide For Keys method:
Parameter | Type | Description |
---|---|---|
keys | Array | Array of string flag keys. |
options (optional) | Array | Array of OptimizelyDecideOption enums. See OptimizelyDecideOption . |
Returns
Returns a map of OptimizelyDecisions. For more information, see OptimizelyDecision.
If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), then it returns an empty map. If the method detects an error for a specific flag, it returns error messages in the Reasons field of the decision for that flag.
Example
The following is an example of getting specified flags for the user:
// make a decisions for specific enabled flags
const keys = ['flag_1', 'flag_2'];
const decisions = user.decideForKeys(keys);
const decision1 = decisions['flag_1'];
const decision2 = decisions['flag_2'];
Side effects
Invokes the DECISION
notification listener for each decision if this listener is enabled.
Source files
The language and platform source files containing the implementation for JavaScript is available on GitHub.
Updated about 22 hours ago