Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySumbit a ticketLog In
GitHubNuGetDev CommunitySumbit a ticket
These docs are for v2.0. Click to read the latest docs for v3.0.

Define audiences and attributes

You can choose which types of users to include in an experiment or rollout using the Audiences feature in the Optimizely UI to create an audience. In that audience, you specify the attributes of users to include, such as their location, device type, or subscription plan, and then pass in these attributes when calling Activate or Is Feature Enabled. Optimizely uses the attributes to evaluate whether a user qualifies for an experiment, and also for segmentation on the Results page.

After creating an Audience, you can go to an A/B test, feature test, or feature rollout and choose which audience(s) to include. For more information, see the section on Targeting Your Visitors in our support documentation.

Create an attribute

Use attributes to build the audiences that see your experiments, and use the Attributes tab to manage all your user attributes. In Full Stack, you can have up to 100 attributes.

To create a new attribute:

  1. Navigate to Audiences > Attributes.
  2. Click New Custom Attribute.
  3. Enter a key for the attribute.
    Attribute keys must be unique within your project. For example, an attribute for visitors located in the United States might be called "US_VISITOR". You can pass attributes to the Full Stack SDK in your code, so always make sure to update your code with any changes made to your keys.
  4. Click Save.
1456

Create an audience

After you've created a few attributes, you can start building audiences. Audiences help you target your experiments to certain groups of users. You can reuse audiences that you've created across multiple experiments, but they’re limited to each Full Stack project.

📘

Note

You do not need to pass audiences through the SDK. Optimizely infers audiences based on the attributes passed in the SDK.

To create an audience:

  1. Navigate to Audiences > Saved.
  2. Click Create New Audience.
  3. Drag and drop the desired attributes into the Audience Conditions field.
    For example, to create an audience of visitors who are located in the US, based on specific location values, add the "LOCATION" attribute.
  4. Define the attributes.
    Add other attributes to help create your audience. They can be can be added as "and" or "or" conditions. Currently, only string attributes are available, and the audience builder includes only exact matching.
  5. Click Save Audience.
1456

Pass in attributes

The examples show how to pass in attributes to an SDK when calling either Activate or Is Feature Enabled.

📘

Note

Attributes are defined based on a string key and take a string value. Be sure that you convert all attributes to strings before passing them in.

import com.optimizely.ab.config.Variation;

Map<String, String> attributes = new HashMap<>();
attributes.put("DEVICE", "iPhone");
attributes.put("AD_SOURCE", "my_campaign");

// Conditionally activate a test for the provided user
Variation variation = optimizelyClient.activate(experimentKey, userId, attributes);
using OptimizelySDK;
using OptimizelySDK.Entity;

var optimizelyClient = new Optimizely(datafile);
var experimentKey = "app_redesign";
var userId = "userId";

UserAttributes attributes = new UserAttributes
{
  { "DEVICE", "iPhone" },
  { "AD_SOURCE", "my_campaign" }
};

// Conditionally activate a test for the provided user
Variation variation = optimizelyClient.Activate(experimentKey, userId, attributes);
import com.optimizely.ab.config.Variation;

Map<String, String> attributes = new HashMap<>();
attributes.put("DEVICE", "iPhone");
attributes.put("AD_SOURCE", "my_campaign");

// Conditionally activate a test for the provided user
Variation variation = optimizelyClient.activate(experimentKey, userId, attributes);
// Conditionally activate an experiment for the provided user
var variation = optimizelyClientInstance.activate(
  "my_experiment",
  userId,
  { plan_type: "silver" }
);

var isDiscountEnabled = optimizelyClientInstance.isFeatureEnabled(
  "product_discount",
  userId,
  { plan_type: "silver" }
);
// Conditionally activate an experiment for the provided user
var variation = optimizelyClient.activate("my_experiment", userId, {
  plan_type: "silver"
});

var isDiscountEnabled = optimizelyClient.isFeatureEnabled(
  "product_discount",
  userId,
  { plan_type: "silver" }
);
NSDictionary *attributes = @{@"device" : @"iPhone", @"ad_source" : @"my_campaign"};

OPTLYVariation *variation = [optimizely activate:@"my_experiment"
                                          userId:@"user123"
                                      attributes:attributes];
$experimentKey = 'my_experiment';
$userId = 'user123';
$attributes = [
    'device' => 'iphone',
    'ad_source' => 'my_campaign'
];

// Conditionally activate the experiment for the provided user
$variation = $optimizelyClient->activate($experimentKey, $userId, $attributes);
# Conditionally activate an experiment for the provided user
variation = optimizely_client.activate(
  'my_experiment',
  'user123',
  {'plan_type': 'silver'}
)

# Conditionally toggle a feature for the provider user
is_discount_enabled = optimizely_client.is_feature_enabled(
  'product_discount',
  'user_123',
  {'plan_type': 'silver'}
)
experiment_key = 'my_experiment'
user_id = 'user123'
attributes = {'DEVICE' => 'iPhone', 'AD_SOURCE' => 'my_campaign'}

# Conditionally activate a test for the provided user
variation = optimizely_client.activate(experiment_key, user_id, attributes)
let attributes = ["device" : "iPhone", "ad_source" : "my_campaign"]

let variation: OPTLYVariation? = optimizely?.activate("my_experiment", userId:"user123", attributes:attributes)

Pass in attributes for segmenting results

If you define attributes and pass them in the SDK, they'll be available as segmentation options from the drop-down menu at the top of the Results page. See also Analyze results: Segment results.

For example, you could use an is_logged_in attribute to measure how your experiment performed specifically for users who are logged in. This works most seamlessly if you also pass the user's attribute values when tracking events.