Checking Variants Without Triggering Exposures
When you call treatment(), the SDK records an exposure, telling ABsmartly that this
user saw the experiment. That's usually what you want. But sometimes you need to know
which variant a user is in without counting it as an exposure.
The peek() method does exactly this. It returns the same variant number as
treatment(), but silently, without recording anything.
When to use peek
Here are some real scenarios where peeking makes sense:
- Pre-loading assets. You need to know the variant ahead of time to prefetch images, scripts, or data. The actual exposure should be recorded later when the user sees the content.
- Server-side routing with client-side exposure. Your server decides which page template to render based on the variant, but the exposure should only fire on the client where the user actually sees it.
- Debug logging. You want to log which variant a user is in for debugging, but you don't want that logging to affect the experiment by creating exposures.
- Admin panels. An internal dashboard shows the current state of experiments for a user. Looking at this state shouldn't count as an exposure.
If you peek but never call treatment() for the same experiment, no exposure will be
recorded. That user won't appear in your experiment results at all. Make sure that
somewhere in your code, treatment() gets called for users who actually see the
experiment.
You can also peek using the useTreatment hook by passing true as the second
argument:
import { useTreatment } from "@absmartly/react-sdk";
const { variant } = useTreatment("experiment_name", true);
This returns the variant without recording an exposure, just like context.peek().
Peeking at treatments
The peek method returns the variant number just like treatment(), but without
recording an exposure. The method name varies by SDK:
- Javascript, React, Vue:
peek() - Python, Ruby:
peek_treatment() - PHP, Swift, Java, Flutter/Dart:
peekTreatment() - Go, .NET:
PeekTreatment()
- Javascript
- Python3
- React
- Swift
- Vue2
- Vue3
- Java
- Go
- .NET
- PHP
- Ruby
- Flutter/Dart
if (context.peek("exp_test_experiment") == 0) {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
res = context.peek_treatment("exp_test_experiment")
if res == 0:
# user is in control group (variant 0)
else:
# user is in treatment group
const { context } = useABSmartly();
useEffect(() => {
context
.ready()
.then(() => {
if (context.peek("exp_test_experiment") === 0) {
// User is in control group (variant 0)
} else {
// User is in treatment group
}
})
.catch((error) => console.error(error));
}, [context]);
let treatment = context.peekTreatment(experimentName: "exp_test_experiment")
if treatment == 0 {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
if (this.$absmartly.peek("exp_test_experiment") == 0) {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
if (this.$absmartly.peek("exp_test_experiment") == 0) {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
if (context.peekTreatment("exp_test_experiment") == 0) {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
var res, _ = context.PeekTreatment("exp_test_experiment")
if res == 0 {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
if (context.PeekTreatment("exp_test_experiment") == 0) {
// user is in control group (variant 0)
} else {
// user is in treatment group
}
$treatment = $context->peekTreatment('exp_test_experiment');
if ($treatment === 0) {
// user is in control group (variant 0)
}
else {
// user is in treatment group
}
treatment = context.peek_treatment('exp_test_experiment')
var value = await ctx.peekTreatment("experimentName");
Peeking at variables
The peek variable method returns the variable value just like variableValue(),
but without recording an exposure. It's called peekVariableValue() in most SDKs
and peek_variable_value() in Python and Ruby.
- Javascript
- Python3
- React
- Swift
- Vue2
- Vue3
- Java
- Go
- .NET
- PHP
- Ruby
- Flutter/Dart
const buttonColor = context.peekVariableValue("button.color", "red");
button_color = context.peek_variable("button.color", "red")
const { context } = useABSmartly();
const buttonColor = context.peekVariableValue("button.color", "red");
let color = context.peekVariableValue("colorGComponent", defaultValue: 255)
this.variableValue = this.$absmartly.peekVariableValue("button.color", "red");
this.variableValue = this.$absmartly.peekVariableValue("button.color", "red");
final Object variable = context.peekVariable("button.color");
var variable = context.PeekVariable("button.color")
var buttonColor = context.PeekVariableValue("button.color");
$buttonColor = $context->peekVariableValue('button.color', 'red');
treatment = context.peek_variable_value('button.color', default_button_color_value)
final dynamic variable = context.peekVariableValue("button.color", defaultButtonColorValue);