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

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Get Feature Variable

This topic describes the Get Feature Variable method which evaluates the specified feature variable of a specific variable type and returns its value.

Evaluates the specified feature variable of a specific variable type and returns its value.

This method is used to evaluate and return a feature variable. Multiple versions of this method are available and are named according to the data type they return:

This method takes into account the user attributes passed in, to determine if the user is part of the audience that qualifies for the experiment.

Boolean

Returns the value of the specified Boolean variable.

func (o *OptimizelyClient) GetFeatureVariableBoolean(featureKey, variableKey string, userContext entities.UserContext) (value bool, err error)

Double

Returns the value of the specified double variable.

func (o *OptimizelyClient) GetFeatureVariableDouble(featureKey, variableKey string, userContext entities.UserContext) (value float64, err error)

Integer

Returns the value of the specified integer variable.

func (o *OptimizelyClient) GetFeatureVariableInteger(featureKey, variableKey string, userContext entities.UserContext) (value int, err error)

String

Returns the value of the specified string variable.

func (o *OptimizelyClient) GetFeatureVariableString(featureKey, variableKey string, userContext entities.UserContext) (value string, err error)

JSON

Returns the value of the specified JSON variable.

func (o *OptimizelyClient) GetFeatureVariableJSON(featureKey, variableKey string, userContext entities.UserContext) (optlyJSON *optimizelyjson.OptimizelyJSON, err error)

Version

SDK v1.0

Description

Each of the Get Feature Variable methods follows the same logic as Is Feature Enabled:

  1. Evaluate any feature tests running for a user.
  2. Check the default configuration on a rollout.

The default value is returned if neither of these are applicable for the specified user, or if the user is in a variation where the feature is disabled.

🚧

Important

Unlike IsFeatureEnabled, the Get Feature Variable methods do not trigger an impression event. This means that if you are running a feature test, events will not be counted until you call Is Feature Enabled. If you do not call Is Feature Enabled, you will not see any visitors on your results page.

Parameters

The table below lists the required and optional parameters in Go.

ParameterTypeDescription
featureKey
required
stringThe feature key is defined from the Features dashboard; see Use feature flags.
variableKey
required
stringThe key that identifies the feature variable. For more information, see: Create feature variables.
userContext
required
entities.UserContextHolds information about the user, such as the userID and the user's attributes.

Returns

The value this method returns is determined by your feature flags. This example shows the specific value returned for Go:

APIReturn
GetFeatureVariableBoolean@return The value of the variable, or false with error if the feature key is invalid, the variable key is invalid, or there is a mismatch with the type of the variable.
GetFeatureVariableDouble@return The value of the variable, or 0.0 with error if the feature key is invalid, the variable key is invalid, or there is a mismatch with the type of the variable.
GetFeatureVariableInteger@return The value of the variable, or 0 with error if the feature key is invalid, the variable key is invalid, or there is a mismatch with the type of the variable.
GetFeatureVariableString@return The value of the variable, or empty-string with error if the feature key is invalid, the variable key is invalid, or there is a mismatch with the type of the variable.

Example

This section shows an example of how you can use the getFeatureVariableInteger method.

attributes := map[string]interface{}{
        "DEVICE": "iPhone",
        "hey":    2,
}

user := entities.UserContext{
        ID:         "userId",
        Attributes: attributes,
}

value, err := optlyClient.GetFeatureVariableInteger("feature_key", "variable_key", user)

Side effects

The table lists other Optimizely functionality that may be triggered by using this method.

FunctionalityDescription
NotificationsInvokes the DECISION [notification] is this notification is subscribed to.

The example code below shows how to add and remove a decision listener.

import (
	"fmt"

	"github.com/optimizely/go-sdk/pkg/client"
	"github.com/optimizely/go-sdk/pkg/notification"
)

// Callback for decision notification
	callback := func(notification notification.DecisionNotification) {

		// Access type on decisionObject to get type of decision
		fmt.Print(notification.Type)
		// Access decisionInfo on decisionObject which
		// will have form as per type of decision.
		fmt.Print(notification.DecisionInfo)
	}

	optimizelyClient, err := optimizelyFactory.Client()

	// Add callback for decision notification
	id, err := optimizelyClient.DecisionService.OnDecision(callback)

	// Remove callback for decision notification
	err = optimizelyClient.DecisionService.RemoveOnDecision(id)

Exceptions

None

Source files

The language/platform source files containing the implementation for Go is Go.