Get Variation
Returns a variation where the visitor will be bucketed, without triggering an impression.
Version
SDK v3.0 and higher
Description
Takes the same arguments and returns the same values as Activate, but without sending an impression network request. The behavior of the two methods is identical otherwise.
Use Get Variation if Activate has been called and the current variation assignment is needed for a given experiment and user. This method bypasses redundant network requests to Optimizely.
See Implement impressions for guidance on when to use each method.
Parameters
This table lists the required and optional parameters for the C# SDK.
Parameter | Type | Description |
---|---|---|
experimentey | string | The key of the experiment. |
userID | string | The ID of the user. |
attributes | map | A map of custom key-value string pairs specifying attributes for the user that are used for audience targeting and results segmentation. Non-string values are only supported in the 3.0 SDK and above. |
The specific parameter names for each supported language are as follows:
experimentKey
userId
attributes
experimentKey
userId
userAttributes
experimentKey
userId
attributes
experimentKey
userId
attributes
experimentKey
userId
attributes
experimentKey
userId
attributes
experimentKey
userId
attributes
experiment_key
user_id
attributes
experiment_key
user_id
attributes
experimentKey
userId
attributes
Returns
The table below lists the specific information returned for each supported language.
Language | Return |
---|---|
Android | @return the variation for the provided experiment key, user id, and attributes |
C# | null|Variation Representing variation |
Java | @return the variation for the provided experiment key, user id, and attributes |
JavaScript (browser) | @return {string|null} variation key |
JavaScript (Node) | @return {string|null} variation key |
Objective-C | @return The variation into which the user is bucketed. This value can be nil. |
PHP | @return null|string Representing the variation key |
Python | Returns: Variation key representing the variation in which the user will be bucketed. None if user is not in experiment or if experiment is not running. |
Ruby | @return [variation key] where visitor will be bucketed. |
Swift | @return The variation into which the user was bucketed. This value can be nil. |
Example
import com.optimizely.ab.config.Variation;
Map<String, Object> attributes = new HashMap<>();
attributes.put("device", "iPhone");
attributes.put("lifetime", 24738388);
attributes.put("is_logged_in", true);
Variation variation = optimizelyClient.getVariation("my_experiment_key", "user_123", attributes);
using OptimizelySDK.Entity;
var attributes = new UserAttributes {
{ "device", "iPhone" },
{ "lifetime", 24738388 },
{ "is_logged_in", true },
};
var variation = optimizelyClient.GetVariation("my_experiment_key", "user_123", attributes);
import com.optimizely.ab.config.Variation;
Map<String, Object> attributes = new HashMap<>();
attributes.put("device", "iPhone");
attributes.put("lifetime", 24738388);
attributes.put("is_logged_in", true);
Variation variation = optimizelyClient.getVariation("my_experiment_key", "user_123", attributes);
var attributes = {
device: 'iPhone',
lifetime: 24738388,
is_logged_in: true,
};
var variationKey = optimizelyClient.getVariation('my_experiment_key', 'user_123', attributes);
var attributes = {
device: 'iPhone',
lifetime: 24738388,
is_logged_in: true,
};
var variationKey = optimizelyClient.getVariation('my_experiment_key', 'user_123', attributes);
NSDictionary *attributes = @{
@"device": @"iPhone",
@"lifetime": @24738388,
@"is_logged_in": @true
};
NSString *variationKey = [optimizely getVariationKeyWithExperimentKey: @"my_experiment_key"
userId:@"user_123"
attributes:attributes
error:nil];
$attributes = [
'device' => 'iPhone',
'lifetime' => 24738388,
'is_logged_in' => true
];
$variationKey = $optimizelyClient->getVariation('my_experiment_key', 'user_123', $attributes);
attributes = {
'device': 'iPhone',
'lifetime': 24738388,
'is_logged_in': True,
}
variation_key = optimizely_client.get_variation('my_experiment_key', 'user_123', attributes)
attributes = {
'device' => 'iPhone',
'lifetime' => 24738388,
'is_logged_in' => true,
}
variation_key = optimizely_client.get_variation('my_experiment_key', 'user_123', attributes)
let attributes = [
"device": "iPhone",
"lifetime": 24738388,
"is_logged_in": true,
]
let variationKey = try? optimizely.getVariationKey(experimentKey: "my_experiment_key",
userId: "user_123",
attributes: attributes)
Notes
Activate versus Get Variation
Use Activate or Is Feature Enabled when the visitor actually sees the experiment. Use Get Variation when you need to know which bucket a visitor is in without showing the visitor the experiment.
For example, use Get Variation in the following circumstances:
-
To pre-bucket users (for example, in order to render a page correctly) before actually exposing them to the experiment. This way, you avoid triggering redundant impressions that might skew experiment results.
For example, suppose you want your web server to show a visitor variation_1 but don't want the visitor to count until they open a flag that isn't visible when the variation loads, like a modal. In this case, use Get Variation in the backend to specify that your web server should respond with variation_1, and use Activate or Get Feature Enabled in the front end when the visitor sees the experiment. -
To align your third-party analytics and your Optimizely results.
For example, suppose you use client-side third-party analytics. Use Get Variation to retrieve the variation, and even show it to the visitor, but only call Activate when the analytics call goes out. This way, your Optimizely results and your 3rd-party analytics stay aligned. -
To avoid redundant network requests. For example, if you called Activate or is Feature Enabled already, you can use Get Variation if you need the current variation assignment for a given experiment and user.
Important
Conversion events can only be attributed to experiments with previously tracked impressions. Impressions are tracked by Activate, not by Get Variation. As a general rule, Optimizely impressions are required for experiment results.
Source files
The language/platform source files containing the implementation for C# is Optimizely.cs.
Updated almost 2 years ago