HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Create flag variations

This topic describes what flag variations are and how to create them in Optimizely Full Stack.

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. Optimizely dynamically assigns those variable values to your flag based on your flag rules. For more information about what flag variations are, see Flag variations. To create flag variations, follow these steps:

Set up flags in Optimizely

(Prerequisite) Create default variables

Before you create variations, you must create default flag variables. You can create variables of type Boolean, String, Integer, Double, or JSON. There is no character limit for a JSON variable value.

Take the following steps to create a flag variable with a default value in the Optimizely app:

  1. Navigate to Flags and select a flag.
  2. Navigate to Default Variables and click the new (+) icon.
  3. Select the type, then edit the Key and Default Value.
  4. Create as many variables as you need for that flag.
Click to enlarge imageClick to enlarge image

Click to enlarge image

Create flag variations:

After creating default variables, take the following steps to create flag variations:

  1. Navigate to Flags>Variations.

  2. Click the New (+) icon.

  3. Configure new values for the flag variables you created in the previous section.

Click to enlarge imageClick to enlarge image

Click to enlarge image

Use flag variations

To control the end-users' experiences, you deliver flag variations through flag rules. For more information, see:

Implement flag variations

In the Optimizely app, you see flag variations, which allow you to group and reuse variable values. In your code implementation, you could use the returned OptimizelyDecision's variation key to conditionally execute code, 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'd use flag variables. Note that this code is agnostic about what type of flag rule the user is in (delivery or experiment), 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: "userId");
var decision = user.Decide(key: "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>(jsonPath: "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)
# 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
    sortMethod = decision.variables["sort_method"]
    pageLimit = decision.variables["page_limit"]
end

# 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 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: Integer? = decision.variables.getValue(jsonPath: "page_limit")
    }


// Fetch products using the flag variables, using something like this pseudocode:
let 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?
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['sort_method'];
  pageLimit = decision.variables['page_limit'];
}

// Fetch products using the flag variables, using something like this pseudocode:
let 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)
$sort_method = 'alphabetical';
$page_limit = 10;
$userId = "user123";
// Is the flag enabled for the user?
$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?
var decision = useDecision('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['sort_method'];
  pageLimit = decision.variables['page_limit'];
}

// Fetch products using the flag variables, using something like this pseudocode:
let products = productProvider.get(sortMethod, pageLimit);

For more detailed examples, see:

Next steps

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


Did this page help you?