Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Decide methods for the Go SDK

An 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.

This page describes the following Decide methods:

Decide

Version

SDK 1.6.0 and higher

Description

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:

ParameterTypeDescription
flagKeyStringThe key of the feature flag
optionsArrayArray of OptimizelyDecideOptions. See following table.

OptimizelyDecideOption

The following table shows details for the OptimizelyDecideOption. In addition to setting these options individually on a Decide method, you can also set them as global defaults when you instantiate the Optimizely client. See Initialize SDK.

OptimizelyDecideOptionIf set:
OptimizelyDecideOption.DisableDecisionEventPrevents 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.
EnabledFlagsOnlyReturn 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.
IgnoreUserProfileServiceWhen 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.
IncludeReasonsReturn 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.
ExcludeVariablesExclude 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:

package main

import (
	"fmt"

  optly "github.com/optimizely/go-sdk"      // for v2: "github.com/optimizely/go-sdk/v2"
  "github.com/optimizely/go-sdk/pkg/decide" // for v2: "github.com/optimizely/go-sdk/v2/pkg/decide"
)

func main() {
	optimizely_client, err := optly.Client("SDK_KEY_HERE")
	if err != nil {
		panic(err)
	}

	user := optimizely_client.CreateUserContext("user123", map[string]interface{}{"logged_in": true})
	decision := user.Decide("product_sort", []decide.OptimizelyDecideOptions{})

	// Did the decision fail with a critical error?
	if decision.VariationKey == "" {
		fmt.Printf("[decide] error: %v", decision.Reasons)
		return
	}

	// flag enabled state:
	enabled := decision.Enabled
	fmt.Println("Decision enabled: ", enabled)

	// variable value:
	var value1 string
	if err := decision.Variables.GetValue("sort_method", &value1); err != nil {
		panic(err)
	}
	// or:
	value2 := decision.Variables.ToMap()["sort_method"].(string)
	fmt.Println("Variable value: ", value2)

	// all variable values
	allVarValues := decision.Variables
	fmt.Println("All variable: ", allVarValues)

	// variation
	variationKey := decision.VariationKey
	fmt.Println("Variation Key: ", variationKey)

	// user for which the decision was made
	// userContext := decision.UserContext

	// flag decision reasons
	reasons := decision.Reasons
	fmt.Println("Decision reasons: ", 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

SDK v1.6.0 and higher

Description

Use the Decide All method to return a map of flag decisions for a user.

Parameters

The following table describes parameters for the Decide All method:

ParameterTypeDescription
optionsArrayArray of OptimizelyDecideOptions. See Decide Parameters.

Returns

The Decide All method returns a map of OptimizelyDecision objects. 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:

// make a decisions for all active (unarchived) flags for the user
decisions := user.DecideAll([]decide.OptimizelyDecideOptions{})
// or only for enabled flags
decisions = user.DecideAll([]decide.OptimizelyDecideOptions{decide.EnabledFlagsOnly})

flagKeys := []string{}
flagDecisions := []client.OptimizelyDecision{}
for k, v := range decisions {
  flagKeys = append(flagKeys, k)
  flagDecisions = append(flagDecisions, v)
}
decisionForFlag1 := decisions["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

SDK v1.6.0 and higher

Description

Get a map of flag decisions for an array of flag keys.

Parameters

The following table describes parameters for the Decide for Keys method:

ParameterTypeDescription
keysArrayArray of string flag keys.
optionsArrayArray of OptimizelyDecideOptions. 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 with the Decide method:

decisions := user.DecideForKeys([]string{"flag_1", "flag_2"}, []decide.OptimizelyDecideOptions{decide.EnabledFlagsOnly})

decisionForFlag1 := decisions["flag_1"]
decisionForFlag2 := 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 Go are available on GitHub.