Disclaimer: This website requires JavaScript to function properly. Some features may not work as expected. Please enable JavaScript in your browser settings for the best experience.

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 GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Use forced bucketing

This topic describes how to use forced bucketing to put a user into a certain variation in Optimizely Full Stack.

Forced bucketing lets you programmatically put a user into a certain variation from within your code.

🚧

Important

The QA approaches here are used to put yourself into a specific variation of an Optimizely Full Stack experiment.

Forced bucketing allows you to force a user into a variation by calling the Set Forced Variation method of the SDK. This allows you to change variation assignments in real-time, which is ideal for QAing experiments or sharing links to a variation with a colleague.

For example, if your test is running on a web server, you may want to use a query parameter to force a variation and facilitate testing of your experiment. To do this, you can detect a parameter in the URL, such as ?force-opty-<experiment_key>=<variation_key> or ?force-opty-my_optimizely_experiment=variation. Parse this parameter and use the Set Forced Variation method to force that specific variation for the user prior to activating the experiment. After that functionality is set up, forcing yourself into each variation will be as simple as adding the parameter to your URL.

Unlike whitelisting, forced bucketing takes place immediately (meaning, without waiting for a datafile update) and has no limit on the number of users that can be forced. It is important to note that the forcedVariations field in the datafile is only related to whitelisted variations and not to variations set by this API.

import com.optimizely.ab.android.sdk.OptimizelyClient;
import com.optimizely.ab.config.Variation;

String experimentKey = "my_experiment";
String userId = "user123";
String forcedVariationKey = "treatment";

// Set a forced variation
optimizelyClient.setForcedVariation(experimentKey, userId, forcedVariationKey);

// Get a forced variation
Variation variation = optimizelyClient.getForcedVariation(experimentKey, userId);

// Clear a forced variation - set it to null!
optimizelyClient.setForcedVariation(experimentKey, userId, null);
using OptimizelySDK;

var optimizelyClient = new Optimizely(datafile);

var experimentKey = "myExperiment";
var userId = "user123";
var forcedVariationKey = "treatment";

// Set a forced variation
optimizelyClient.SetForcedVariation(experimentKey, userId, forcedVariationKey);

// Get a forced variation
var variation = optimizelyClient.GetForcedVariation(experimentKey, userId);

// Clear a forced variation
optimizelyClient.SetForcedVariation(experimentKey, userId, null);
import com.optimizely.ab.Optimizely;
import com.optimizely.ab.config.Variation;

String experimentKey = "my_experiment";
String userId = "user123";
String forcedVariationKey = "treatment";

// Set a forced variation
optimizelyClient.setForcedVariation(experimentKey, userId, forcedVariationKey);

// Get a forced variation
Variation variation = optimizelyClient.getForcedVariation(experimentKey, userId);

// Clear a forced variation - set it to null!
optimizelyClient.setForcedVariation(experimentKey, userId, null);
var experimentKey = "myExperiment"
var userId = "user123"
var forcedVariationKey = "treatment"

// set a forced variation
optimizelyClientInstance.setForcedVariation(
  experimentKey,
  userId,
  forcedVariationKey
);

// get a forced variation
var variationKey = optimizelyClientInstance.getForcedVariation(
  experimentKey,
  userId
);

// clear a forced variation
optimizelyClientInstance.setForcedVariation(
  experimentKey,
  userId,
  null
);
var experimentKey = "my_experiment";
var forcedUserId = "forcedUserId123";
var forcedVariationKey = "treatment";

// Set a forced variation
optimizelyClient.setForcedVariation(
  experimentKey,
  forcedUserId,
  forcedVariationKey
);

// Get a forced variation
console.log(
  `Forced variation: ${optimizelyClient.getForcedVariation(
    experimentKey,
    forcedUserId
  )}`
);

// Clear a forced variation
optimizelyClient.setForcedVariation(experimentKey, forcedUserId, null);
#import "OptimizelySDKiOS.h"

NSString *experimentKey = @"my_experiment";
NSString *userId = @"user123";
NSString *forcedVariationKey = @"treatment";

// Set a forced variation
[optimizelyClient setForcedVariation:experimentKey userId:userId variationKey:forcedVariationKey];

// Get a forced variation
OPTLYVariation *variation = [optimizelyClient getForcedVariation:experimentKey userId:userId];

// Clear a forced variation - set it to null!
[optimizelyClient setForcedVariation:experimentKey userId:userId variationKey:nil];
$experimentKey = "my_experiment";
$userId = "user123";
$forcedVariationKey = "treatment";

// Set a forced variation
$optimizelyClient->setForcedVariation($experimentKey, $userId, $forcedVariationKey);

// Get a forced variation
$variation = $optimizelyClient->getForcedVariation($experimentKey, $userId);

// Clear a forced variation
$optimizelyClient->setForcedVariation($experimentKey, $userId, null);
experiment_key = "feature_test"
user_id = 'user123'
forced_variation = "variation_2"

# Set a forced variation
is_variation_forced = optimizely_client.set_forced_variation(experiment_key,
                                                             user_id,
                                                             forced_variation)

# Get a forced variation
variation = optimizely_client.get_forced_variation(experiment_key, user_id)

# Clear a forced variation
clear_variation = optimizely_client.set_forced_variation(experiment_key, user_id, None)
experiment_key = 'my_experiment'
user_id = 'user123'
forced_variation_key = 'treatment'

# Set a forced variation
optimizely_client.set_forced_variation(experiment_key, user_id, forced_variation_key);

# Get a forced variation
variation_key = optimizely_client.get_forced_variation(experiment_key, user_id);

# Clear a forced variation - set it to null!
optimizely_client.set_forced_variation(experiment_key, user_id, null);
import OptimizelySDKiOS

let experimentKey = "my_experiment"
let userId = "user123"
let forcedVariationKey = "treatment"

// Set a forced variation
optimizelyClient?.setForcedVariation(experimentKey, userId: userId, variationKey: forcedVariationKey)

// Get a forced variation
let variation = optimizelyClient?.getForcedVariation(experimentKey, userId: userId)

// Clear a forced variation - set it to null!
optimizelyClient?.setForcedVariation(experimentKey, userId: userId, variationKey: nil)

📘

Note

Visitors that are forced into a variation with this API will accrue impressions on their subsequent Activate, isFeatureEnabled or Decide calls.