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

Set Forced Variation

This topic describes the Set Forced Variation method, which forces a user into a variation for a given experiment for the lifetime of the Optimizely client.

Forces a user into a variation for a given experiment for the lifetime of the Optimizely client.

The purpose of this method is to force a user into a specific variation or personalized experience for a given experiment. The forced variation value doesn't persist across application launches.

Version

SDK v3.0, v3.1

Description

Forces a user into a variation for a given experiment for the lifetime of the Optimizely client. Any future calls to Activate, Is Feature Enabled, Get Feature Variable, and Track for the given user ID returns the forced variation.

Forced bucketing variations take precedence over whitelisted variations, variations saved in a User Profile Service (if one exists), and the normal bucketed variation. Impression and conversion events are still tracked when forced bucketing is enabled.

Variations are overwritten with each set method call. To clear the forced variations so that the normal bucketing flow can occur, pass null as the variation key parameter. To get the variation that has been forced, use Get Forced Variation.

This call will fail and return false if the experiment key is not in the project file or if the variation key is not in the experiment.

You can also use Set Forced Variation for feature tests.

Parameters

This table lists the required and optional parameters for the JavaScript SDK.

ParameterTypeDescription
experimentKey
required
stringThe key of the experiment to set with the forced variation.
userId
required
stringThe ID of the user to force into the variation.
variationKey
optional
stringThe key of the forced variation. Set the value to null to clear the existing experiment-to-variation mapping.

Returns

@return boolean true if the user was successfully forced into a variation, false if the experimentKey isn't in the project file or the variationKey isn't in the experiment.

Example

optimizelyClient.setForcedVariation('my_experiment_key', 'user_123', 'some_variation_key')

Side effects

In the receiving client instance, sets the forced variation for the specified user in the specified experiment. This forced variation is used instead of the variation that Optimizely would normally determine for that user and experiment.

Source files

The language/platform source files containing the implementation for JavaScript are available on GitHub.