Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

OptimizelyDecision for the JavaScript SDK v6

Describes the OptimizelyDecision object for the JavaScript(Browser) SDK, which is returned from the decide methods 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.

Minimum SDK version

v6.0.0+

Object model

The following code shows the interface of the OptimizelyDecision object:

interface OptimizelyDecision {
  variationKey: string | null;
  enabled: boolean;
  variables: { [variableKey: string]: unknown };
  ruleKey: string | null;
  flagKey: string;
  userContext: OptimizelyUserContext;
  reasons: string[];
}

The following table describes the fields of the OptimizelyDecision object:

FieldTypeDescription
enabledBooleanWhether the flag is enabled or not. Optimizely Feature Experimentation never returns null for this field; it returns false when it fails to make a decision due to an error.
variablesmapKey-value pairs of all the flag variables. When there are no variables defined for the flag or SDK cannot get variables, this field returns an empty map. When SDK fails to get variables for a decision, the Reasons field includes the error messages regardless of the Include Reasons option.
variationKeyString (nullable)When SDK fails to make a decision, this field returns null and the Reasons field includes the log messages if the Include Reasons option is set.
ruleKeyString (nullable)The key of the flag rule that the user bucketed into for this decision.
When a valid rule key is not available, returns a copy of the experiment key.
Returns null if neither a valid rule key nor experiment key is found.
flagKeyStringThe key of the flag for which the decision was made.
userContextOptimizelyUserContextThe user associated with the call to the Decide method.
reasonsArrayAn array of relevant error and log messages, in chronological order. Optimizely Feature Experimentation returns messages in this field in the following situations:


  • When errors are detected while making decisions.

  • When errors are detected while making decisions.

  • An empty array when there are no messages.See the following section for more information.




Reasons

The returned decision object includes error and log messages in the Reasons field.

Error reasons

For the sake of consistency with other decision results, Optimizely Feature Experimentation returns a decision with null fields instead of throwing errors or exceptions.

Critical errors are always included in the reasons field of the returned decision object regardless of the include reasons option you set on the Decide method.

ErrorPossible reasons
null variationKey
  • Optimizely Feature Experimentation SDK not configured properly yet
  • User context is not valid
  • No flag was found for key
empty variables
  • Invalid variables for OptimizelyJSON
  • Variable value for key is invalid or wrong type

Log reasons

Optimizely Feature Experimentation collects log messages in the Reasons field of the returned OptimizelyDecision object in the order in which they are logged.

The following types of log messages are only included in the Reasons field if you set the Include Reasons option on the Decide method:

  • Log messages related to bucketing users based on audiences or attributes.
  • Log messages related to bucketing users based on traffic percentages.

Examples

The language and platform source files containing the implementation for JavaScript are available on GitHub.