Returns a module exposing several functions to access information about the current visitor's state.
Syntax
state = window["optimizely"].get("state");
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
The argument indicating to get the state object. Required.
</td>
</tr>
Parameter and Type |
---|
**state** *string* |
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
The object contains functions to get more information about the current state of Optimizely Web Experimentation.
</td>
</tr>
<tr>
<td>
**getActiveExperimentIds**\
*functionzely"].get("state");
```*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get an array of experiment IDs that are currently active on the page for the visitor.
</td>
</tr>
<tr>
<td>
**getCampaignStates**\
*functionptimizely"].get("st*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get all the campaigns and their current states.
</td>
</tr>
<tr>
<td>
**getExperimentStates**\
*functionimizely"].get("state"*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get all the experiments and their current states.
</td>
</tr>
<tr>
<td>
**getPageStates**\
*functionw["optimizely"]*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get all the pages and their current states.
</td>
</tr>
<tr>
<td>
**getRedirectInfo**\
*function"optimizely"].get*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
If a redirect variation was executed on the previous page, all the details of that experiment are accessible on the next page through the getRedirectInfo() function.
</td>
</tr>
<tr>
<td>
**getVariationMap**\
*function"optimizely"].get*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get a mapping from each experiment the visitor has ever seen to its current corresponding variation.
</td>
</tr>
<tr>
<td>
**getDecisionString**\
*functionptimizely"].get("st*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get a single string describing the campaign/experiment decision for a given campaign ID.
</td>
</tr>
<tr>
<td>
**getDecisionObject**\
*functionptimizely"].get("st*
</td>
<td>
Child attribute of type StateObject
</td>
<td>
Get an object with strings that describe the campaign / experiment decision for a given campaign ID.
</td>
</tr>
Parameter and Type |
---|
*StateObject* |
Example Call
state = window["optimizely"].get("state");
Example Return Value
state.getActivationId();
state.getActiveExperimentIds();
state.getCampaignStates(filters);
state.getExperimentStates(filters);
state.getPageStates(filters);
state.getRedirectInfo();
state.getVariationMap();
state.getDecisionString();
state.getDecisionObject();
getCampaignStates()
Get all the campaigns and their current states.
Syntax
state.getCampaignStates(filter);
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
An object with fields to filter on.
</td>
</tr>
<tr>
<td>
**isActive**\
*Boolean*
</td>
<td>
Child attribute of\
type CampaignFilterObject
</td>
<td>
Indicates if the campaign is active.
</td>
</tr>
Parameter and Type |
---|
**filter** *CampaignFilterObject* or *CampaignFilterFunction* |
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
Parameter and Type |
---|
**objectax
|
Example Call
state.getCampaignStates({
"isActive": true
});
Example Return Value
{
"6676370413": {
"id": "6676370413",
"campaignName": "Example personalization campaign",
"experiment": {
"id": "6666781105",
"name": null
},
"variation": {
"id": "6626731852",
"name": "Variation #1"
},
"isActive": true,
"visitorRedirected": false,
"isInCampaignHoldback": false,
"audiences": [{
"id": "6672770135",
"name": "Chrome users"
}]
}
}
Description
Get all the campaigns and their current states. The filter argument allows you to filter the campaigns. It's possible to filter using an object or with a function.
Examples
Filter with a function
Filter the campaigns with a filter function.
Example code for Filter with a function
var state = window["optimizely"].get("state");
state.getCampaignStates(function(campaignState) {
return !!campaignState.isActive;
});
getExperimentStates()
Get all the experiments and their current states.
Syntax
state.getExperimentStates(filter);
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
An object with fields to filter on.
</td>
</tr>
<tr>
<td>
**isActive**\
*Boolean*
</td>
<td>
Child attribute of\
type CampaignFilterObject
</td>
<td>
Indicates if the experiment is active.
</td>
</tr>
Parameter and Type |
---|
**filter** *CampaignFilterObject* or *CampaignFilterFunction* |
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
Parameter and Type |
---|
*objecttax
|
Example Call
state.getExperimentStates({
"isActive": true
});
Example Return Value
{
"6666781105": {
"id": "6666781105",
"experimentName": null,
"variation": {
"id": "6626731852",
"name": "Variation #1"
},
"isActive": true,
"visitorRedirected": false,
"isInExperimentHoldback": false,
"audiences": [{
"id": "6672770135",
"name": "Chrome users"
}]
}
}
Description
Get A/B experiments and their current states (excludes Personalization campaigns). The filter argument allows you to filter the experiments. It's possible to filter using an object or with a function.
Examples
Filter with a function
Filter the experiments with a filter function.
Example code for Filter with a function
var state = window["optimizely"].get("state");
// "Not variation #1"
state.getExperimentStates(function(expState) {
return !expState.variation || expState.variation.name !== "Variation #1";
});
Filter with an object
Filter the experiments with a match object.
Example code for Filter with an object
var state = window["optimizely"].get("state");
state.getExperimentStates({
isActive: true
});
getPageStates()
Get all the pages and their current states.
Syntax
state.getPageStates(filter);
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
The argument indicating to get data information.
</td>
</tr>
<tr>
<td>
**isActive**\
*boolean*
</td>
<td>
Child attribute of type PageFilterObject
</td>
<td>
Indicates if the page is active.
</td>
</tr>
Parameter and Type |
---|
**filter** *PageFilterObject* |
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
Parameter and Type |
---|
*objecttax
|
Example Call
state.getPageStates({
"isActive": true
});
Example Return Value
{
"6678290257": {
"id": "6678290257",
"name": "Homepage",
"apiName": "homepage",
"category": "other",
"staticConditions": ["and", ["or", {
"type": "url",
"value": "https://developers.optimizely.com/",
"match": "simple"
}]],
"tags": [{
"category": "other",
"locator": ".push-quad > a:nth-of-type(1)",
"valueType": "string",
"locatorType": "css_selector",
"apiName": "view_docs_button"
}],
"isActive": true,
"metadata": {
"view_docs_button": "View Docs"
}
}
}
Description
Get all the pages and their current states. The filter argument allows you to filter the pages. It's possible to filter using an object or with a function.
Examples
Filter with a function
Filter the campaigns with a filter function.
Example code for Filter with a function
var state = window["optimizely"].get("state");
state.getCampaignStates(function(pageState) {
return !!pageState.isActive;
});
getVariationMap()
Get a mapping from each experiment the visitor has ever seen to its current corresponding variation.
Syntax
state.getVariationMap();
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
Parameter and Type |
---|
*objecttax
|
Example Call
state.getVariationMap();
Example Return Value
{
"6661191859": {
"id": "6670551924",
"name": "Variation #1",
"index": "1"
}
}
Description
Get the mapping of all experiments the visitor has ever seen to its current corresponding variation. If the visitor is excluded from an experiment due to traffic allocation, that experiment will appear here
and they will be bucketed into a variation, but no visual changes will be applied.
getActiveExperimentIds()
Get an array of experiment IDs that are currently active on the page for the visitor.
Syntax
state.getActiveExperimentIds();
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
array[integer] | N/A | An array of experiment IDs that are currently active on the page for the visitor. |
Example Call
state.getActiveExperimentIds();
Example Return Value
["6661191859", "6666781105", "7555673865"]
Description
Get an array of experiment IDs that are currently active on the page for the visitor. If the visitor is excluded from an experiment due to traffic allocation, the experiment is considered active and they will be bucketed into a variation, but no visual changes will be applied. This situation is referred to as isInExperimentHoldback
in the getExperimentStates
API:
state.getExperimentStates()[{experiment_id}].isInExperimentHoldback
getRedirectInfo()
If a redirect variation was executed on the previous page, all the details of that experiment are accessible on the next page through the getRedirectInfo()
function.
Syntax
state.getRedirectInfo();
Return value
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
The object contains all the details about a redirect experiment that ran on the previous page.
</td>
</tr>
<tr>
<td>
**experimentId**\
*string*
</td>
<td>
Child attribute of type RedirectInfoObject
</td>
<td>
The ID of the redirect experiment. Required.
</td>
</tr>
<tr>
<td>
**variationId**\
*string*
</td>
<td>
Child attribute of type RedirectInfoObject
</td>
<td>
The ID of the redirect variation. Required.
</td>
</tr>
<tr>
<td>
**referrer**\
*string*
</td>
<td>
Child attribute of type RedirectInfoObject
</td>
<td>
The value of the original referrer, before the redirect occurred. Required.
</td>
</tr>
Parameter and Type |
---|
*null* or *RedirectInfoObject* |
Example Call
state.getRedirectInfo();
Example Return Value
{
"experimentId": "7669390999",
"variationId": "7662661235",
"referrer": "https://www.google.com/"
}
Description
If a redirect variation was executed on the previous page, all the details of that experiment are accessible on the next page through the getRedirectInfo()
function. Most analytics integrations use this information to correct referrer values.
getDecisionString()
Get a single string describing the campaign/experiment decision for a given campaign ID.
Syntax
state.getDecisionString(config);
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
An object with configuration fields for the function. Required.
</td>
</tr>
<tr>
<td>
**campaignId**\
*string*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
Indicates the campaign to get the decision string for. Required.
</td>
</tr>
<tr>
<td>
**shouldCleanString**\
*Boolean*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
If true, any character that matches the regex `/[^a-zA-Z0-9\.\~\!\*\(\)\']+/g` is replaced with `_`. Default is false.
</td>
</tr>
<tr>
<td>
**maxLength**\
*number*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
The maximum length of the returned string. If the combined length of names and IDs is longer than the maxLength, the names will be individually truncated, with favor given to the campaign name. Default is 255 characters.
</td>
</tr>
Parameters and Type |
---|
**config** *DecisionConfigObject* |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
string | N/A | For personalization campaign decisions, the string returned is a concatenation of the decision campaign, experiment, and variation names and IDs, and holdback status. For A/B experiment decisions, the string returned is a concatenation of the decision experiment and variation names and IDs. |
Example Call
state.getDecisionString({
"campaignId": "6676370413"
});
Example Return Value
"Example personalization campaign(6676370413):Example experiment(6666781105):Variation #1(6626731852):holdback"
Description
Get a single string describing the campaign/experiment decision for a given campaign ID. This is often used to send campaign decision data to third-party analytics integrations. This function takes a campaign ID as a parameter; if the campaign in question is an AB test, make sure to still pass the campaign ID, and this API will return only the information that is relevant to an A/B test. This makes it so that code consuming this API does not have to conditionalize on whether the decision is for an AB test or a personalization campaign.
getDecisionObject()
Get an object with strings that describe the campaign/experiment decision for a given campaign ID.
Syntax
state.getDecisionObject(config);
Parameters
<th>
Child Attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
An object with configuration fields for the function. Required.
</td>
</tr>
<tr>
<td>
**campaignId**\
*string*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
Indicates the campaign to get the decision string for. Required.
</td>
</tr>
<tr>
<td>
**shouldCleanString**\
*Boolean*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
If true, any character that matches the regex `/[^a-zA-Z0-9\.\~\!\*\(\)\']+/g` is replaced with `_`. Default is false.
</td>
</tr>
<tr>
<td>
**maxLength**\
*number*
</td>
<td>
Child attribute of type DecisionConfigObject
</td>
<td>
The maximum length of the returned string. If the combined length of names and IDs is longer than the maxLength, the names will be individually truncated, with favor given to the campaign name. Default is 255 characters.
</td>
</tr>
Parameter and Type |
---|
**config** *DecisionConfigObject* |
Return value
<th>
Child attribute
</th>
<th>
Description
</th>
</tr>
<td>
N/A
</td>
<td>
For personalization campaign decisions, the object returned contains values for the decision campaign, experiment, and variation names and IDs, and holdback status. For A/B experiment decisions, the object returned contains values for the decision experiment and variation names and IDs.
</td>
</tr>
<tr>
<td>
**campaign**\
*string* or *undefined*
</td>
<td>
Child attribute of type GetDecisionObjectReturn
</td>
<td>
A string representing the decision's campaign name and ID. Not included if campaign is an AB test.
</td>
</tr>
<tr>
<td>
**experiment**\
*string*
</td>
<td>
Child attribute of type GetDecisionObjectReturn
</td>
<td>
A string representing the decision's experiment name and ID.
</td>
</tr>
<tr>
<td>
**variation**\
*string*
</td>
<td>
Child attribute of type GetDecisionObjectReturn
</td>
<td>
A string representing the decision's variation name and ID.
</td>
</tr>
<tr>
<td>
**holdback**\
*Boolean* or *undefined*
</td>
<td>
Child attribute of type GetDecisionObjectReturn
</td>
<td>
Whether visitor was placed in the campaign holdback. Not included if campaign is an AB test.
</td>
</tr>
Parameter and Type |
---|
*GetDecisionObjectReturn* |
Example Call
state.getDecisionObject({
"campaignId": "6676370413"
});
Example Return Value
{
"campaign": "Shopping Cart (6676370413)",
"experiment": "New Visitors (3243212353)",
"variation": "Variation #1 (2344343121)",
"holdback": false
}
Description
Get an object with strings that describe the campaign/experiment decision for a given campaign ID. This is often used to send campaign decision data to third-party analytics integrations. This function takes a campaign ID as a parameter; if the campaign in question is an AB test, make sure to still pass the campaign ID, and this API will return only the information that is relevant to an A/B test. This makes it so that code consuming this API does not have to conditionalize on whether the decision is for an AB test or a personalization campaign.