Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Create flag variations

Describes flag variations and how to create them in Optimizely Feature Experimentation.

A flag variation is a reusable group of flag variable values (also called remote feature configuration or remote variables) that let you avoid hard coding values in your code. Instead of waiting for a deployment, you can group them in a flag variation, then update them remotely in the Optimizely application.

Optimizely Feature Experimentation dynamically assigns those variable values to your flag based on your flag rules. For more information, see Flag variations.

Set up flag variations

(Prerequisite) Create flag variables

Flag variables must be created before assigning them to a variation.

Create flag variations:

After creating variables, to create flag variations:

  1. Navigate to Flags > Variations.

  2. Click the add variation button.

  3. Configure new values for the flag variables you created.

add variation to a flag

Click to enlarge the image

📘

Note

When a new flag is created, the default variations of On and Off are automatically generated.

You cannot delete the Off variation.

Implement flag variations

You deliver flag variations through flag rules to control the end-user's experiences. Flag rules can be a targeted delivery, A/B test, or Multi-armed bandit optimization.

In the Optimizely Experimentation application, you see flag variations, which allow you to group and reuse variable values.

In your code implementation, you can use the returned OptimizelyDecision's variation key to execute code conditionally. But, to take full advantage of variables, we recommend getting the values of the variation's variables directly.

The following code sample shows how you would use flag variables.

📘

Note

This code is indifferent about what type of flag rule the user is in (targeted delivery, A/B test, or Multi-armed bandit), so you can re-use the same code to run different rules.

// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagation.

//Define flag variable defaults (if the flag is disabled, you fall back to these values)
sortMethod := "alphabetical"
pageLimit := 10

// Is the flag enabled for the user?
user := optimizely.CreateUserContext(userId, nil)
decision := user.Decide("product_sort", nil)
enabled := decision.Enabled

// In the online Optimizely app, go define flag variations using
// sort_method and page_limit variables.
// For example, variation_1 might sort by popularity and limit results to 15 pages.

if enabled {
	// get flag variable values depending on the variation the user bucketed into
	err1 := decision.Variables.GetValue("sort_method", &sortMethod)
	err2 := decision.Variables.GetValue("page_limit", &pageLimit)
}

// Fetch products using the flag variables, using something like this pseudocode:
products := productProvider.get(sortMethod, pageLimit)
/** 
 * Fetch enabled state for the "product_sort" flag.
 * Then use flag variations to expose different sorting methods and pagation. 
 */

// Define flag variable defaults (if the flag is disabled, you fall back to these values)
var sortMethod = "alphabetical";
var pageLimit = 10;

// Is the flag enabled for the user?
var user = optimizely.CreateUserContext("userId");
var decision = user.Decide("product_sort");
var enabled = decision.Enabled;

/**
 * In the online Optimizely app, go define flag variations using
 * sort_method and page_limit variables. 
 * For example, variation_1 might sort by popularity and limit results to 15 pages.
 */

if (enabled)
{
  // get flag variable values depending on the variation the user bucketed into
  sortMethod = decision.Variables.GetValue<string>("sort_method");

  pageLimit = decision.Variables.GetValue<int>("page_limit");
}


// Fetch products using the flag variables, using something like this pseudocode:
var products = productProvider.Get(sortMethod, pageLimit);
/**
 * Fetch enabled state for the "product_sort" flag.
 * Then use flag variations to expose different sorting methods and pagation.
 */

// Define flag variable defaults (if the flag is disabled, you fall back to these values)
String sortMethod = "alphabetical";
int pageLimit = 10;

// Is the flag enabled for the user?
OptimizelyUserContext user = optimizely.createUserContext("userId");
OptimizelyDecision decision = user.decide("product_sort");
Boolean enabled = decision.getEnabled();

/**
 * In the online Optimizely app, go define flag variations using
 * sort_method and page_limit variables.
 * For example, variation_1 might sort by popularity and limit results to 15 pages.
 */
if (enabled)
{
  // get flag variable values depending on the variation the user bucketed into
  try {
    sortMethod = decision.getVariables().getValue("sort_method", String.class);
    pageLimit = decision.getVariables().getValue("page_limit", Integer.class);

  } catch (JsonParseException e) {
    e.printStackTrace();
  }
}


// Fetch products using the flag variables, using something like this pseudocode:
List products = productProvider.get(sortMethod, pageLimit);
"""
    Fetch enabled state for the "product_sort" flag.
    Then use flag variations to expose different sorting methods and pagination.
"""

# Define flag variable defaults (if the flag is disabled, you fall back to these values)

sort_method = "alphabetical"
page_limit = 10

# Is the flag enabled for the user?
user = optimizely.create_user_context("user123")
decision = user.decide("product_sort")
enabled = decision.enabled

"""
    In the online Optimizely app, go define flag variations using
    sort_method and page_limit variables.
    For example, variation_1 might sort by popularity and limit results to 15 pages.
"""

if enabled:
    # get flag variable values depending on the variation the user bucketed into
    sort_method = decision.variables["sort_method"]
    page_limit = decision.variables["page_limit"]

# Fetch products using the flag variables, using something like this pseudocode:
products = ProductProvider.get(sort_method, page_limit)
# frozen_string_literal: true

# Fetch enabled state for the "product_sort" flag.
# Then use flag variations to expose different sorting methods and pagination.

# Define flag variable defaults (if the flag is disabled, you fall back to these values)
sort_method = 'alphabetical'
page_limit = 10

# Is the flag enabled for the user?
user = optimizely.create_user_context(userId)
decision = user.decide('product_sort')
enabled = decision.enabled

# In the online Optimizely app, go define flag variations using
# sort_method and page_limit variables.
# For example, variation_1 might sort by popularity and limit results to 15 pages.

if enabled
  # get flag variable values depending on the variation the user bucketed into
  sort_method = decision.variables['sort_method']
  page_limit = decision.variables['page_limit']
end

# Fetch products using the flag variables, using something like this pseudocode:
products = productProvider.get(sort_method, page_limit)
/**
 * Fetch enabled state for the "product_sort" flag.
 * Then use flag variations to expose different sorting methods and pagation. 
*/


//Define flag variable defaults (if the flag is disabled, you fall back to these values)
var sort_method = "alphabetical"
var page_limit = 10
 
// Is the flag enabled for the user?
let user = optimizely.createUserContext(userId: userId)
let decision = user.decide(key: "product_sort")
let enabled = decision.enabled 

                                                        
/**
  * In the online Optimizely app, go define flag variations using
  * sort_method and page_limit variables. 
  * For example, variation_1 might sort by popularity and limit results to 15 pages.
 */
  
if enabled {
  // get flag variable values depending on the variation the user bucketed into
  let sortMethod: String? = decision.variables.getValue(jsonPath: "sort_method")
  let pageLimit: Int? = decision.variables.getValue(jsonPath: "page_limit")
	}


// Fetch products using the flag variables, using something like this pseudocode:
let products = productProvider.get(sort_method, page_limit)
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination. 

// Define flag variable defaults (if the flag is disabled, you fall back to these values)
let sortMethod = 'alphabetical';
let pageLimit = 10;

// Is the flag enabled for the user?
const userId = 'user123'
const user = optimizely.createUserContext(userId);
const decision = user.decide('product_sort');
const enabled = decision.enabled;

//In the online Optimizely app, go define flag variations using
// sort_method and page_limit variables. 
// For example, variation_1 might sort by popularity and limit results to 15 pages.

if (enabled) {
  // get flag variable values depending on the variation the user bucketed into
  sortMethod = decision.variables['sort_method'];
  pageLimit = decision.variables['page_limit'];
}

// Fetch products using the flag variables, using something like this pseudocode:
const products = productProvider.get(sortMethod, pageLimit);
/*
 * Fetch enabled state for the "product_sort" flag.
 * Then use flag variations to expose different sorting methods and pagination.
*/

// Define flag variable defaults (if the flag is disabled, you fall back to these values)
$sortMethod = 'alphabetical';
$pageLimit = 10;

// Is the flag enabled for the user?
$userId = "user123";
$user = $optimizely->createUserContext($userId);
$decision = $user->decide('product_sort');
$enabled = $decision->getEnabled();

/**
 * In the online Optimizely app, go define flag variations using
 * sort_method and page_limit variables.
 * For example, variation_1 might sort by popularity and limit results to 15 pages.
 */

if ($enabled) {
    // get flag variable values depending on the variation the user bucketed into
    $sortMethod = (string) $decision->getVariables()['sort_method'];
    $pageLimit = (int) $decision->getVariables()['page_limit'];
}

// Fetch products using the flag variables, using something like this pseudocode:
$products = $productProvider->get($sortMethod, $pageLimit);
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination. 

// Define flag variable defaults (if the flag is disabled, you fall back to these values)
let sort_method = 'alphabetical';
let page_limit = 10;
 
// Is the flag enabled for the user?
const decision = useDecision('product_sort');
const enabled = decision.enabled;
                                                        
//In the online Optimizely app, go define flag variations using
// sort_method and page_limit variables. 
// For example, variation_1 might sort by popularity and limit results to 15 pages.
  
if (enabled) {
  // get flag variable values depending on the variation the user bucketed into
  sortMethod = decision.variables['sort_method'];
  pageLimit = decision.variables['page_limit'];
}

// Fetch products using the flag variables, using something like this pseudocode:
const products = productProvider.get(sortMethod, pageLimit);
// Fetch enabled state for the "product_sort" flag.
// Then use flag variations to expose different sorting methods and pagination.

// Define flag variable defaults (if the flag is disabled, you fall back to these values)

var sort_method = "alphabetical";
var page_limit = 10;

// Is the flag enabled for the user?
var user = await flutterSDK.createUserContext(userId: "user123");
var decisionResponse = await user!.decide(sort_method);
var decision = decisionResponse.decision;
var enabled = decision!.enabled;

// In the online Optimizely app, go define flag variations using sort_method and page_limit variables. 
// For example, variation_1 might sort by popularity and limit results to 15 pages.

if (enabled) {
    // get flag variable values depending on the variation the user bucketed into
    var sort_method = decision.variables["sort_method"];
    var page_limit = decision.variables["page_limit"];
  }

// Fetch products using the flag variables, using something like this pseudocode:
var products = ProductProvider.get(sort_method, page_limit);

For more detailed examples, see:


Next

Use flag variations to build flag delivery rules or experiment rules: