GuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Get Feature Variable

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.

public func getFeatureVariableBoolean(featureKey: String,
                                     variableKey: String,
                                          userId: String,
                                      attributes: OptimizelyAttributes?=nil) throws -> Bool
- (NSNumber *)getFeatureVariableBooleanWithFeatureKey:(nullable NSString *)featureKey
                       variableKey:(nullable NSString *)variableKey
                            userId:(nullable NSString *)userId
                        attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes
                        error:(nullable NSError **)error;

Double

Returns the value of the specified double variable.

public func getFeatureVariableDouble(featureKey: String,
                                     variableKey: String,
                                          userId: String,
                                      attributes: OptimizelyAttributes?=nil) throws -> Double
- (NSNumber *)getFeatureVariableDoubleWithFeatureKey:(nullable NSString *)featureKey
                       variableKey:(nullable NSString *)variableKey
                            userId:(nullable NSString *)userId
                        attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes
                          error:(nullable NSError **)error;

Integer

Returns the value of the specified integer variable.

public func getFeatureVariableInteger(featureKey: String,
                                     variableKey: String,
                                          userId: String,
                                      attributes: OptimizelyAttributes?=nil) throws -> Integer
- (NSNumber *)getFeatureVariableIntegerWithFeatureKey:(nullable NSString *)featureKey
                     variableKey:(nullable NSString *)variableKey
                          userId:(nullable NSString *)userId
                      attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes
                        error:(nullable NSError **)error;

String

Returns the value of the specified string variable.

public func getFeatureVariableString(featureKey: String,
                                     variableKey: String,
                                          userId: String,
                                      attributes: OptimizelyAttributes?=nil) throws -> String
- (NSString *_Nullable)getFeatureVariableStringWithFeatureKey:(nullable NSString *)featureKey
                           variableKey:(nullable NSString *)variableKey
                                userId:(nullable NSString *)userId
                            attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes
                                 error:(nullable NSError **)error;

JSON

Returns the value of the specified JSON variable.

public func getFeatureVariableJSON(featureKey: String,
                                   variableKey: String,
                                   userId: String,
                                   attributes: OptimizelyAttributes? = nil) throws -> OptimizelyJSON

Version

SDK v3.0, v3.1

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 Is Feature Enabled, the Get Feature Variable methods do not trigger an impression event. This means that if you're running a feature test, events won't be counted until you call Is Feature Enabled. If you don't call Is Feature Enabled, you won't see any visitors on your results page.

Parameters

Required and optional parameters are listed below.

Parameter

Type

Description

featureKey
required

string

The feature key is defined from the Features dashboard.

variableKey
required

string

The key that identifies the feature variable.

userId
required

string

The user ID string uniquely identifies the participant in the experiment.

attributes
required

map

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

Returns

The value of the variable

Example

let attributes = [
  "device": "iPhone",
  "lifetime": 24738388,
  "is_logged_in": true,
]

let featureVariableValue = try? optimizelyClient?.getFeatureVariableDouble(featureKey:"my_feature_key", variableKey:"double_variable_key", userId:"user_123", attributes:attributes)
NSDictionary *attributes = @{
  @"device": @"iPhone",
  @"lifetime": @24738388,
  @"is_logged_in": @true
};

NSNumber *featureVariableValue = [optimizelyClient getFeatureVariableDouble:@"my_feature_key"
                                                                variableKey:@"double_variable_key"
                                                                     userId:@"user_123"
                                                                 attributes:attributes
                                  error:nil];

Side effects

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

Source files

The language/platform source files containing the implementation for Swift is OptimizelyClient.swift.


Did this page help you?