Decide methods for the Flutter SDK
Overview of the decide methods which can be used to return a flag decision for a user in Optimizely Feature Experimentation.
Use the Decide methods to return a flag decision for a user. The flag decision includes flag enabled/disabled status and flag variation.
There are multiple Decide methods you can use:
Decide
Returns a decision result for a flag key for a user.
Version
1.0.1 or higher
Description
The system returns a decision result 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 |
---|---|---|
key | String | The key of the feature flag. |
options (optional) | Set | Set of OptimizelyDecideOption enums. See following table. |
OptimizelyDecideOption
The following table shows details for the OptimizelyDecideOption. You can only set these options individually on a Decide method. See Initialize SDK.
// set options in a decide call
var flutterSDK = OptimizelyFlutterSdk("<Your_SDK_Key>");
var response = await flutterSDK.initializeClient();
// flag decision
var user = await flutterSDK.createUserContext(userId: "user123");
Set<OptimizelyDecideOption> options = {};
options.add(OptimizelyDecideOption.disableDecisionEvent);
var decisions = user!.decideAll(options);
OptimizelyDecideOption enum | If set: |
---|---|
OptimizelyDecideOption.disableDecisionEvent | Prevents the visitor from firing an impression while still being served the variation, which disables displaying results of the Decide method on the Optimizely application's 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.enabledFlagsOnly | Return decisions for enabled flags only. This is a valid option only for methods that decide multiple flags, like the Decide All method. The system ignores this option 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.includeReasons | Return log messages in the Reasons field of OptimizelyDecision object. Unlike info or debug messages, critical error messages are always returned, regardless of this setting. |
OptimizelyDecideOption.excludeVariables | Exclude flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables. |
OptimizelyDecideOption.ignoreUserProfileService | 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. |
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:
Map<String, dynamic> attributes = {};
attributes["logged_in"] = true;
var user = await flutterSDK.createUserContext(userId: "user123", 3", attributes: attributes);
var decideResponse = await user!.decide("product_sort");
// variation. if null, decision fail with a critical error
var variationKey = decideResponse.decision!.variationKey;
// flag enabled state:
var enabled = decideResponse.decision!.enabled;
// all variable values
var variables = decideResponse.decision!.variables;
// String variable value
var vs = variables["sort_method"] as String;
// Boolean variable value
var vb = variables["k_boolean"] as bool;
// flag key for which decision was made
var flagKey = decideResponse.decision!.flagKey;
// user for which the decision was made
var userContext = decideResponse.decision!.userContext;
// reasons for the decision
var reasons = decideResponse.decision!.reasons;
Side effects
Invokes the DECISION
notification listener if this listener is enabled.
Decide All
Returns decisions for all active (unarchived) flags for a user.
See OptimizelyDecision for details.
Version
1.0.1
Description
Use the Decide All method to return a map of flag decisions for the user.
Parameters
The following table describes parameters for the Decide All method:
Parameter | Type | Description |
---|---|---|
options (optional) | Set | Set of OptimizelyDecideOption enums. See Decide Parameters. |
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 a decision response with success status false
, reason and decision as null object. 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:
// make decisions for all active (unarchived) flags in the project for a user
Set<OptimizelyDecideOption> options = const { OptimizelyDecideOption.enabledFlagsOnly };
var decisionsResponse = await user!.decideAll(options);
var decisionMap = decisionsResponse.decisions;
var decisionForFlag1 = decisionMap["flag_1"];
Side effects
Invokes the DECISION
notification listener for each decision, if this listener is enabled.
Decide For Keys
The Decide For Keys method returns a map of flag decisions for specified flag keys.
Version
1.0.1
Description
Get a map of flag decisions for specific 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) | Set | 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 decision response with success status false
, reason and decision as null object. 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:
var keys = ["flag-1", "flag-2"];
var decisionsResponse = await user!.decideForKeys(keys);
var decisionMap = decisionsResponse.decisions;
var decision1 = decisionMap["flag_1"];
var decision2 = decisionMap["flag_2"];
Side effects
Invokes the DECISION
notification listener for each decision, if this listener is enabled.
Source files
The language/platform source files containing the implementation for Flutter Android are at Optimizely.java and Flutter Swift is at OptimizelyClient.swift .
Updated 10 months ago