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 TermsGitHubNuGetDev CommunitySubmit a ticketLog In

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.


Starting in v3.3.0 of the SDK you can use the generic feature variable accessor. This can return an integer, double, boolean, or string depending on the variable's defined type.

def get_feature_variable(feature_flag_key, variable_key, user_id, attributes = nil)


Returns the value of the specified Boolean variable.

def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attributes = nil)


Returns the value of the specified double variable.

def get_feature_variable_double(feature_flag_key, variable_key, user_id, attributes = nil)


Returns the value of the specified integer variable.

def get_feature_variable_integer(feature_flag_key, variable_key, user_id, attributes = nil)


Returns the value of the specified string variable.

def get_feature_variable_string(feature_flag_key, variable_key, user_id, attributes = nil)


Returns the value of the specified JSON variable.

def get_feature_variable_json(feature_flag_key, variable_key, user_id, attributes = nil)


SDK v3.0, v3.1, v3.2, v3.3


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.



Unlike Is Feature Enabled, 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.


Required and optional parameters in Ruby are listed below.

stringThe feature key is defined from the Features dashboard; see Use feature flags.
stringThe key that identifies the feature variable. For more information, see: Create feature variables.
stringThe user ID string uniquely identifies the participant in the experiment. For more information, see: Handle user IDs.
mapA map of custom key-value string pairs specifying attributes for the user that are used for audience targeting and results segmentation. Non-string values are only supported in the 3.0 SDK and above.


@return [] The value of the variable, or nil if the feature key is invalid, the variable key is invalid, or there is a mismatch with the type of the variable.


feature_variable_value = optimizely_client.get_feature_variable_double('my_feature_key', 'double_variable_key', 'user_123', attributes)

# or in v3.3+ you can use the untyped variable accessor, which will return a double if the variable you defined in the Optimizely Dashboard is of type double
feature_variable_value = optimizely_client.get_feature_variable('my_feature_key', 'double_variable_key', 'user_123', attributes)

See also

Is Feature Enabled

Side effects

In SDKs v3.1 and later: Invokes the DECISION Notification listeners if this listener is enabled.

Source files

The language/platform source files containing the implementation for Ruby is optimizely.rb.