State
Returns a module exposing several functions to access information about the current visitor's state.
Syntax
state = window["optimizely"].get("state");
Parameters
Parameter and Type | Child Attribute | Description |
---|---|---|
state | The argument indicating to get the state object. Required. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
StateObject | The object contains functions to get more information about the current state of Optimizely. | |
getActiveExperimentIds | Child attribute of type StateObject | Get an array of experiment IDs that are currently active on the page for the visitor. |
getCampaignStates | Child attribute of type StateObject | Get all the campaigns and their current states. |
getExperimentStates | Child attribute of type StateObject | Get all the experiments and their current states. |
getPageStates | Child attribute of type StateObject | Get all the pages and their current states. |
getRedirectInfo | Child attribute of type StateObject | 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. |
getVariationMap | Child attribute of type StateObject | Get a mapping from each experiment the visitor has ever seen to its current corresponding variation. |
getDecisionString | Child attribute of type StateObject | Get a single string describing the campaign/experiment decision for a given campaign ID. |
getDecisionObject | Child attribute of type StateObject | Get an object with strings that describe the campaign / experiment decision for a given campaign ID. |
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
Parameter and Type | Child Attribute | Description |
---|---|---|
filter | An object with fields to filter on. | |
isActive | Child attribute of | Indicates if the campaign is active. |
Return value
Parameter and Type | Child Attribute | |
---|---|---|
object[CampaignStateObject] | Returns an object. The object keys are campaign IDs. The values describe the state of every campaign. | |
id | Child attribute of type CampaignStateObject | The ID of the campaign. |
campaignName | Child attribute of type CampaignStateObject | The name of the campaign. |
experiment | Child attribute of type CampaignStateObject | An object with the name and the ID of the experiment the visitor is bucketed in. |
id | Child attribute of experiment type NameIdObject | The ID of the object. |
name | Child attribute of experiment type NameIdObject | The name of the object. |
variation | Child attribute of type CampaignStateObject | An object with the name and the ID of the variation the visitor is bucketed in. |
id | Child attribute of variation type NameIdObject | The ID of the object. |
name | Child attribute of variation type NameIdObject | The name of the object. |
isActive | Child attribute of type CampaignStateObject | Indicates if the campaign is currently active. |
visitorRedirected | Child attribute of type CampaignStateObject | Indicates if the visitor was redirected due to this campaign. |
isInCampaignHoldback | Child attribute of type CampaignStateObject | Indicates if the visitor is in the campaign holdback. |
audiences | Child attribute of type CampaignStateObject | Audiences the visitor was in at the moment the campaign activated. |
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
Parameter and Type | Child Attribute | Description |
---|---|---|
filter | An object with fields to filter on. | |
isActive | Child attribute of | Indicates if the experiment is active. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
object[ExperimentStateObject] | Returns an object. The object keys are experiment IDs. The values describe the state of every experiment. | |
id | Child attribute of type ExperimentStateObject | The ID of the experiment. |
experimentName | Child attribute of type ExperimentStateObject | The name of the experiment. |
audiences | Child attribute of type ExperimentStateObject | Audiences the visitor was in when the experiment was activated. |
id | Child attribute of audiences type NameIdObject | The ID of the object. Required. |
name | Child attribute of audiences type NameIdObject | The name of the object. Required. |
variation | Child attribute of type ExperimentStateObject | An object with the name and the ID of the variation the visitor is bucketed in, or null if the visitor was not bucketed. |
id | Child attribute of variation type NameIdObject | The ID of the object. Required. |
name | Child attribute of variation type NameIdObject | The name of the object. Required. |
isActive | Child attribute of type ExperimentStateObject | Indicates if the experiment is currently active. |
isInExperimentHoldback | Child attribute of type ExperimentStateObject | Indicates if the visitor is in the holdback (i.e., is excluded due traffic allocation). |
visitorRedirected | Child attribute of type ExperimentStateObject | Indicates if the visitor was redirected due to this experiment. |
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
Parameter and Type | Child Attribute | Description |
---|---|---|
filter | The argument indicating to get data information. | |
isActive | Child attribute of type PageFilterObject | Indicates if the page is active. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
object[PageStateObject] | Returns an object. The object keys are page IDs. The values describe the state of every page. | |
id | Child attribute of type PageStateObject | The ID of the page. |
name | Child attribute of type PageStateObject | The name of the page. |
apiName | Child attribute of type PageStateObject | The apiName of the page. |
staticConditions | Child attribute of type PageStateObject | The category of the page. |
category | Child attribute of type PageStateObject | The URL conditions for this page object. |
tags | Child attribute of type PageStateObject | The tags associated to this page. |
type | Child attribute of type TagObject | A static value that indicates that you are using the "tag" function. Required. |
tags | Child attribute of type TagObject | A single-level JSON object with metadata. This is equivalent to page tags but can be used when pages are already being activated using URL targeting. |
| Child attribute of type PageTag | This object can contain project- or environment-specific properties that are not predefined. |
isActive | Child attribute of type PageStateObject | Indicates if the page is currently active. |
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
Parameter and Type | Child Attribute | |
---|---|---|
object[VariationIdName] | An object where the keys are the experiment IDs of every experiment the visitor has ever seen and the values are objects with the name, ID, and index of the variation a visitor saw for that experiment. | |
id | Child attribute of type VariationIdName | The ID of the variation. Required. |
name | Child attribute of type VariationIdName | The name of the variation. Required. |
index | Child attribute of type VariationIdName | The index of the variation in its experiment's list of variations. The first item in the list has an index of |
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] | 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
Parameter and Type | Child Attribute | Description |
---|---|---|
null or RedirectInfoObject | The object contains all the details about a redirect experiment that ran on the previous page. | |
experimentId | Child attribute of type RedirectInfoObject | The ID of the redirect experiment. Required. |
variationId | Child attribute of type RedirectInfoObject | The ID of the redirect variation. Required. |
referrer | Child attribute of type RedirectInfoObject | The value of the original referrer, before the redirect occurred. Required. |
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
Parameters and Type | Child Attribute | Description |
---|---|---|
config | An object with configuration fields for the function. Required. | |
campaignId | Child attribute of type DecisionConfigObject | Indicates the campaign to get the decision string for. Required. |
shouldCleanString | Child attribute of type DecisionConfigObject | If true, any character that matches the regex |
maxLength | Child attribute of type DecisionConfigObject | 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. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
string | 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. More details on expected string output. |
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
Parameter and Type | Child Attribute | Description |
---|---|---|
config | An object with configuration fields for the function. Required. | |
campaignId | Child attribute of type DecisionConfigObject | Indicates the campaign to get the decision string for. Required. |
shouldCleanString | Child attribute of type DecisionConfigObject | If true, any character that matches the regex |
maxLength | Child attribute of type DecisionConfigObject | 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. |
Return value
Parameter and Type | Child attribute | Description |
---|---|---|
GetDecisionObjectReturn | 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. | |
campaign | Child attribute of type GetDecisionObjectReturn | A string representing the decision's campaign name and ID. Not included if campaign is an AB test. |
experiment | Child attribute of type GetDecisionObjectReturn | A string representing the decision's experiment name and ID. |
variation | Child attribute of type GetDecisionObjectReturn | A string representing the decision's variation name and ID. |
holdback | Child attribute of type GetDecisionObjectReturn | Whether visitor was placed in the campaign holdback. Not included if campaign is an AB test. |
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.
Updated about 3 years ago