Example usage
This topic includes sample code for evaluating a feature, running an A/B test and tracking an event in each supported Full Stack SDK language.
Once you have 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. Each concept is described in detail in this guide, and you can find each method's arguments and return values in the API Reference.
This example shows how to:
-
Evaluate a feature with the key
price_filter
and check a configuration variable on it calledmin_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. -
Run an A/B test called
app_redesign
. This experiment has two variations,control
andtreatment
. It uses theactivate
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. -
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 a variable
Boolean enabled = optimizelyClient.isFeatureEnabled("price_filter", userId);
Integer minPrice = optimizelyClient.getFeatureVariableInteger("price_filter", "min_price", userId);
// Activate an A/B test
Variation variation = optimizelyClient.activate("app_redesign", userId);
if (variation != null) {
if (variation.is("control")) {
// Execute code for variation A
} else if (variation.is("treatment")) {
// Execute code for variation B
}
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
optimizelyClient.track("purchased", userId);
//Import Optimizely SDK
using OptimizelySDK;
// Instantiate an Optimizely client
var optimizelyClient = new Optimizely(datafile);
// Evaluate a feature flag and a variable
bool isFeatureEnabled = optimizelyClient.IsFeatureEnabled("price_filter", userId);
int? min_price = optimizelyClient.GetFeatureVariableInteger("price_filter", "min_price", userId);
// Activate an A/B test
var variation = optimizelyClient.Activate("app_redesign", userId);
if (variation != null && !string.IsNullOrEmpty(variation.Key))
{
if (variation.Key == "control")
{
// Execute code for variation A
}
else if (variation.Key == "treatment")
{
// Execute code for variation B
}
}
else
{
// Execute code for your users who don’t qualify for the experiment
}
// Track an event
optimizelyClient.Track("purchased", userId);
import com.optimizely.ab.Optimizely;
import com.optimizely.ab.config.Variation;
// Instantiate an Optimizely client
Optimizely optimizelyClient = Optimizely.builder(datafile, eventHandler).build();
// Evaluate a feature flag and a variable
Boolean enabled = optimizelyClient.isFeatureEnabled("price_filter", userId);
Integer min_price = optimizelyClient.getFeatureVariableInteger("price_filter", "min_price", userId);
// Activate an A/B test
Variation variation = optimizelyClient.activate("app_redesign", userId);
if (variation != null) {
if (variation.is("control")) {
// Execute code for variation A
} else if (variation.is("treatment")) {
// Execute code for variation B
}
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
optimizelyClient.track("purchased", userId);
var optimizely = require('@optimizely/optimizely-sdk');
// Instantiate an Optimizely client
var optimizelyClientInstance = optimizely.createInstance({ datafile: datafile });
// Evaluate a feature flag and a variable
var enabled = optimizelyClientInstance.isFeatureEnabled('price_filter', userId);
var min_price = optimizelyClientInstance.getFeatureVariableInteger('price_filter', 'min_price', userId);
// Activate an A/B test
var variation = optimizelyClientInstance.activate('app_redesign', userId);
if (variation === 'control') {
// Execute code for variation A
} else if (variation === 'treatment') {
// Execute code for variation B
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
optimizelyClientInstance.track('purchased', userId);
// Evaluate a feature flag and a variable
bool enabled = [client isFeatureEnabled:@"price_filter" userId: userId];
int min_price = [[client getFeatureVariableInteger:@"price_filter" userId: userId] integerValue];
// Activate an A/B test
OPTLYVariation *variation = [client activate:@"app_redesign" userId:userId];
if ([variation.variationKey isEqualToString:@"control"]) {
// Execute code for variation A
} else if ([variation.variationKey isEqualToString:@"treatment"]) {
// Execute code for variation B
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
[client track:@"purchased" userId:userId];
use Optimizely\Optimizely;
// Instantiate an Optimizely client
$optimizelyClient = new Optimizely($datafile);
// Evaluate a feature flag and a variable
$enabled = $optimizelyClient->isFeatureEnabled('price_filter', $userId);
$min_price = $optimizelyClient->getFeatureVariableInteger('price_filter', 'min_price', $userId);
// Activate an A/B test
$variation = $optimizelyClient->activate('app_redesign', $userId);
if ($variation == 'control') {
// Execute code for variation A
} else if ($variation == 'treatment') {
// Execute code for variation B
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
$optimizelyClient->track('purchased', $userId);
from optimizely import optimizely
# Instantiate an Optimizely client
optimizely_client = optimizely.Optimizely(datafile)
# Evaluate a feature flag and variable
enabled = optimizely_client.is_feature_enabled('price_filter', user_id)
min_price = optimizely_client.get_feature_variable_integer('price_filter', 'min_price', user_id)
# Activate an A/B test
variation = optimizely_client.activate('app_redesign', user_id)
if variation == 'control':
pass
# Execute code for variation A
elif variation == 'treatment':
pass
# Execute code for variation B
else:
pass
# Execute code for users who don't qualify for the experiment
# Track an event
optimizely_client.track('purchased', user_id)
require "optimizely"
# Initialize an Optimizely client
optimizely_client = Optimizely::Project.new(datafile)
# Evaluate a feature flag and a variable
enabled = optimizely_client.is_feature_enabled('price_filter', user_id)
min_price = optimizely_client.get_feature_variable_integer('price_filter', 'min_price', user_id)
# Activate an A/B test
variation = optimizely_client.activate('app_redesign', user_id)
if variation == 'control'
# Execute code for variation "control"
elsif variation == 'treatment'
# Execute code for variation "treatment"
else
# Execute code for users who don't qualify for the experiment
end
# Track an event
optimizely_client.track('purchased', user_id)
// Evaluate a feature flag and variable
let enabled = optimizelyClient?.isFeatureEnabled("price_filter", userId: userId)
let min_price = optimizelyClient?.getFeatureVariableInteger("price_filter", "min_price", userId: userId)?.intValue
// Activate an A/B test
let variation = optimizelyClient?.activate("app_redesign", userId:userId)
if let variation = variation {
if (variation.variationKey == "control") {
// Execute code for variation A
} else if (variation.variationKey == "treatment") {
// Execute code for variation B
}
} else {
// Execute code for users who don't qualify for the experiment
}
// Track an event
optimizelyClient?.track("purchased", userId:userId)
Updated almost 3 years ago