Movatterモバイル変換


[0]ホーム

URL:


Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog InFeature Experimentation
Dev guide
All
Pages
Start typing to search…

Decide methods for the JavaScript (Node) SDK prior to v6

Overview of the decide methods which you can use to return a flag decision for a user in Optimizely Feature Experimentation.

Minimum SDK version – v4.5+

Description

The decide method handles the variation decision for an individual experiment. It returns a decision result for a flag key for a user in anOptimizelyDecision object and contains the data required to deliver the flag rule.

This method ensures that Feature Experimentation properly buckets the user in experiment analytics, and you should use it if there is no need for pre-rendering decisions.

📘

Note

Decide is a method of theUserContext object. SeeOptimizelyUserContext for details.

See theOptimizelyDecision documentation for details of the returned decision object.

Parameters

The following table describes parameters for the decide method:

ParameterTypeDescription
flagKeyStringThe key of the feature flag
options (optional)ArrayArray ofOptimizelyDecideOption enums.

Returns

The decide method returns anOptimizelyDecision .

If the method encounters a critical error (SDK not ready, invalid flag key, and so on), then it returns a decision with anullvariationKey field and populates thereasons field with error messages (regardless of theOptimizelyDecideOption.INCLUDE_REASONS option).

Exampledecide

// Create the user and decide which flag rule and variation they bucket into const attributes = {  logged_in: true};const user = optimizely.createUserContext('user123', attributes);const decision = user.decide('product_sort');// The variation key. If null, decision fail with a critical errorconst variationKey = decision['variationKey'];if (variationKey === null) {  console.log(' decision error: ', decision['reasons']);}// The flag enabled stateconst enabled = decision['enabled'];// Teh string variable valueconst value = decision.variables['sort_method'];// All variable valuesconst allVarValues = decision['variables'];// Flag decision reasonsconst reasons = decision['reasons'];// The user for which the decision was madeconst userContext = decision['userContext'];

Side Effects

Invokes thedecision notification listener if this listener is enabled.

Decide all

Returns decisions for all active (unarchived) flags for a user.

Minimum SDK version – v4.5+

Description

Use the decide all method to retrieve decisions for all active flags before rendering content. This is particularly useful when you need to serve the correct cached version of your page or component based on the user's variation. For example when using anedge worker orcloud function.

Key features

  • Pre-rendering decision – Lets you know all variation assignments ahead of time.
  • Cache control – Lets you serve the correct cached content based on the user's pre-determined variations.
  • Delay experiment tracking – UseDISABLE_DECISION_EVENT to prevent the SDK from recording a decision before the user sees the feature. This ensures that participation is only tracked when the experience is delivered.

Parameters

The following table describes parameters for the decide all method:

ParameterTypeDescription
options (optional)ArrayArray ofOptimizelyDecideOption enums.

Returns

The decide all method returns a map of OptimizelyDecisions. For more information, seeOptimizelyDecision.

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 thereasons field of the decision for that flag.

ExampledecideAll

const attributes = {  logged_in: true};const user = optimizely.createUserContext('user123', attributes);// Make decisions for all active (unarchived) flags in the project for a userlet decisions = user.decideAll();// Or only for enabled flagsdecisions = user.decideAll([OptimizelyDecideOption.ENABLED_FLAGS_ONLY]);const flagKeys = Object.keys(decisions);const decisionForFlag1 = decisions['flag_1'];

Side effects

Invokes thedecide notification listener for each decision if this listener is enabled.

Decide for keys

Returns a map of flag decisions for specified flag keys.

Minimum SDK version – v4.5+

Description

Returns a map of decisions for the specified flag keys.

Parameters

The following table describes the parameters for the decide for keys method:

ParameterTypeDescription
keysArrayArray of string flag keys.
options (optional)ArrayArray ofOptimizelyDecideOption enums.

Returns

Returns a map of OptimizelyDecisions. For more information, seeOptimizelyDecision.

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 thereasons field of the decision for that flag.

ExampledecideForKeys

// Make a decisions for specific enabled flagsconst keys = ['flag_1', 'flag_2'];const decisions = user.decideForKeys(keys);const decision1 = decisions['flag_1'];const decision2 = decisions['flag_2'];

Side effects

Invokes thedecision notification listener for each decision if this listener is enabled.

OptimizelyDecideOption

The following table lists theOptimizelyDecideOption enum with an explanation what happens if you set them. In addition to setting these options individually for a decide method, you can also set them as global defaults when you instantiate the Optimizely client. SeeInitialize JavaScript (Node) SDK.

OptimizelyDecideOption

enum

If set

OptimizelyDecideOption.DISABLE_DECISION_EVENT

Prevents the SDK from dispatching animpression event when serving a variation. This disables decision tracking on theOptimizely Experiment Results page and thedecision notification listener.

OptimizelyDecideOption.ENABLED_FLAGS_ONLY

Returns decisions only for flags that are currently enabled. Used with thedecide all method anddecide for keys method.

When this option is not set, the Android SDK returns all decisions regardless of whether the flag is enabled.

OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE

Bypasses the user profile service (both lookup and save) for the decision.

When this option is not set, user profile service overrides audience targeting, traffic allocation, and experiment mutual exclusion groups.

OptimizelyDecideOption.INCLUDE_REASONS

Adds log messages to the reasons field of the decision. Critical errors are always returned, even if this option is not set.

OptimizelyDecideOption.EXCLUDE_VARIABLES

Excludes flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables.

The following code sample shows how to implement theOptimizelyDecideOption as a global default and locally in a decide method call.

const { createInstance, OptimizelyDecideOption } = require('@optimizely/optimizely-sdk');// Set global default decide options when initializing the clientconst optimizely = createInstance({  sdkKey: 'YOUR_SDK_KEY', // Replace with your SDK key  defaultDecideOptions: [OptimizelyDecideOption.DISABLE_DECISION_EVENT],});if (optimizely) {  optimizely.onReady().then(({ success, reason }) => {    if (!success) {      throw new Error(reason);    }    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}

Source files

The language and platform source files containing the implementation for the JavaScript (Node) SDK are available onGitHub.

Updated 17 days ago



[8]ページ先頭

©2009-2025 Movatter.jp