Run A/B tests
How to set up a simple A/B or ON/OFF test in Optimizely Feature Experimentation.
If you are new to experimentation, you can get a lot done with a simple ON
or OFF
A/B test. This configuration has one flag with two variations:
-
One "flag_on" variation.
-
One "flag_off" variation.
Restrictions
If you intend on having multiple experiments and flag deliveries (targeted deliveries) in your flag, your experiment must always be the first rule in your ruleset for a flag.
Configuration overview
To configure a basic A/B test:
-
(Prerequisite) Create a flag.
-
(Prerequisite) Handle user IDs.
-
Create and configure an A/B Test rule in the Optimizely app.
-
Integrate the example
decide
code that the Optimizely Feature Experimentation app generates with your application. -
Test your experiment in a non-production environment. See QA and troubleshoot.
-
Discard any QA user events and enable your experiment in a production environment.
Create an experiment
With the Feature Experimentation UI
See Run A/B tests in Feature Experimentation in the user documentation.
With the REST API
Use the Update the Ruleset for a Flag in an Environment Environment](ref/update_ruleset) endpoint to add a new rule with type
set to a/b
.
Implement the experiment using the decide method
Flag is implemented in your code
If you have already implemented the flag using a Decide
method, you do not need to take further action (Optimizely Feature Experimentation SDKs are designed so you can reuse the exact flag implementation for different flag rules).
Flag is not implemented in your code
If the flag is not implemented yet, copy the sample flag integration code into your application code and edit it so that your feature code runs or does not run based on the output of the decision received from Optimizely.
Use the Decide
method to enable or disable the flag for a user:
// Decide if user sees a feature flag variation
user := optimizely.CreateUserContext("user123", map[string]interface{}{"logged_in": true})
decision := user.Decide("flag_1", nil)
enabled := decision.Enabled
// Decide if user sees a feature flag variation
var user = optimizely.CreateUserContext("user123", new UserAttributes { { "logged_in", true } });
var decision = user.Decide("flag_1");
var enabled = decision.Enabled;
// Decide if user sees a feature flag variation
var user = await flutterSDK.createUserContext(userId: "user123");
var decideResponse = await user.decide("product_sort");
var enabled = decision.enabled;
// Decide if user sees a feature flag variation
OptimizelyUserContext user = optimizely.createUserContext("user123", new HashMap<String, Object>() { { put("logged_in", true); } });
OptimizelyDecision decision = user.decide("flag_1");
Boolean enabled = decision.getEnabled();
// Decide if user sees a feature flag variation
const user = optimizely.createUserContext('user123', { logged_in: true });
const decision = user.decide('flag_1');
const enabled = decision.enabled;
// Decide if user sees a feature flag variation
$user = $optimizely->createUserContext('user123', ['logged_in' => true]);
$decision = $user->decide('flag_1');
$enabled = $decision->getEnabled();
# Decide if user sees a feature flag variation
user = optimizely.create_user_context("user123", {"logged_in": True})
decision = user.decide("flag_1")
enabled = decision.enabled
// Decide if user sees a feature flag variation
var decision = useDecision('flag_1', null, { overrideUserAttributes: { logged_in: true }});
var enabled = decision.enabled;
# Decide if user sees a feature flag variation
user = optimizely_client.create_user_context('user123', {'logged_in' => true})
decision = user.decide('flag_1')
decision.enabled
// Decide if user sees a feature flag variation
let user = optimizely.createUserContext(userId: "user123", attributes: ["logged_in":true])
let decision = user.decide(key: "flag_1")
let enabled = decision.enabled
For more detailed examples of each SDK, see:
- Android SDK example usage
- Go SDK example usage
- C# SDK example usage
- Flutter SDK example usage
- Java SDK example usage
- JavaScript SDK example usage – SDK versions 6.0.0 and later.
- Javascript (Browser) SDK example usage – SDK versions 5.3.5 and earlier.
- JavaScript (Node) SDK example usage – SDK versions 5.3.5 and earlier.
- PHP SDK example usage
- Python SDK example usage
- React SDK example usage
- React Native SDK example usage
- Ruby SDK example usage
- Swift SDK example usage
Adapt the integration code in your application. Show or hide the flag's functionality for a given user ID based on the boolean value your application receives.
The goal of the Decide
method is to separate the process of developing and releasing code from the decision to turn a flag on. The value this method returns is determined by your flag rules. For example, the method returns false if the current user is assigned to a control or "off" variation in an experiment.
Remember, a user evaluates each flag rule in an ordered ruleset before being bucketed into a given rule variation or not. See Interactions between flag rules for information.
Start your rule and flag (ruleset)
Note
For information between the old and new flag management UI in Optimizely Feature Experimentation, see New flag and rule lifecycle management FAQs.
After creating your A/B test and implementing the experiment using the decide method, you can start your test.
With the Feature Experimentation UI
See the Start your rule and flag (ruleset) in the user documentation for information on using the UI.
With the REST API
First, enable the flag by using the Enable the Ruleset for a Flag in an Environment endpoint. Then, launch experiment by enabling the rule by using the Update the Ruleset for a Flag in an Environment endpoint and setting the enabled
path to true
.
You can also see step seven (Launch experiment) in the Run experiment recipe. See the REST API cookbook for information on configuring the recipe and step by step instructions.
Test with flag variables
When you have run a basic "on/off" A/B test, you can increase the power of your experiments by adding remote feature configurations or flag variables.
Flag variations enable you to avoid hard-coding variables in your application. Instead of updating the variables by deploying, you can edit them remotely in the Optimizely Feature Experimentation app. For information about flag variations, see flag variations.
To set up an A/B test with multiple variations:
- Create and configure a basic A/B test. See previous steps.
- Create flag variations containing multiple variables.
- Integrate the example code with your application.
Updated 8 days ago