Example usage

Once you've installed an SDK, import the Optimizely library into your code, get your Optimizely project's datafile, and instantiate a client. Then, you can use the client to evaluate feature flags, activate an A/B test, or feature test.

This example demonstrates the basic usage of each of these concepts. This example shows how to:

  1. Evaluate a feature with the key price_filter and check a configuration variable on it called min_price. The SDK evaluates your feature test and rollouts to determine whether the feature is enabled for a particular user, and which minimum price they should see if so.

  2. Run an A/B test called app_redesign. This experiment has two variations, control and treatment. It uses the activate method to assign the user to a variation, returning its key. As a side effect, the activate function also sends an impression event to Optimizely to record that the current user has been exposed to the experiment.

  3. Use event tracking to track an event called purchased. This conversion event measures the impact of an experiment. Using the track method, the purchase is automatically attributed back to the running A/B and feature tests we've activated, and the SDK sends a network request to Optimizely via the customizable event dispatcher so we can count it in your results page.

// Evaluate a feature flag and variable
let enabled = optimizelyClient.isFeatureEnabled(featureKey: "price_filter", 
                                                 userId: userId)
do {
    let min_price = try optimizelyClient.getFeatureVariableInteger(featureKey: "price_filter", 
                                                            variableKey: "min_price", 
                                                            userId: userId)
} catch {
  // Execute code for invalid variable value
}

// Activate an A/B test
do {
    let variation = try optimizelyClient.activate("app_redesign", userId:userId)
  if (variation.variationKey == "control") {
    // Execute code for variation A
  } else if (variation.variationKey == "treatment") {
    // Execute code for variation B
  }
} catch {
  // Execute code for users who don't qualify for the experiment
}

// Track an event
try? optimizelyClient.track("purchased", userId:userId)
// Evaluate a feature flag and a variable
BOOL enabled = [optimizelyClient isFeatureEnabled:@"price_filter" userId: userId attributes:nil];
NSNumber *min_price = [optimizelyClient getFeatureVariableInteger:@"price_filter"
                                                    variableKey:@"min_price"
                                                    userId:userId
                                                    attributes:nil
                                                                                error:nil];

// Activate an A/B test
NSString *variation = [optimizelyClient activate:@"app_redesign" 
                       userId:userId
                       error:nil];
if ([variation isEqualToString:@"control"]) {
    // Execute code for variation A
} else if ([variation isEqualToString:@"treatment"]) {
    // Execute code for variation B
} else {
    // Execute code for users who don't qualify for the experiment
}

// Track an event
[optimizelyClient trackWithEventKey:@"purchased"
                                    userId:userId
                                    attributes:nil
                                    eventTags:nil
                                    error:nil];

Did this page help you?