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 string | N/A | The argument indicating to get the state object. Required. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
StateObject | N/A | The object contains functions to get more information about the current state of Optimizely Web Experimentation. |
getActiveExperimentIds function[getActiveExperimentIds] | Child attribute of type StateObject | Get an array of experiment IDs that are currently active on the page for the visitor. |
getCampaignStates function[getCampaignStates] | Child attribute of type StateObject | Get all the campaigns and their current states. |
getExperimentStates function[getExperimentStates] | Child attribute of type StateObject | Get all the experiments and their current states. |
getPageStates function[getPageStates] | Child attribute of type StateObject | Get all the pages and their current states. |
getRedirectInfo function[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 function[getVariationMap] | Child attribute of type StateObject | Get a mapping from each experiment the visitor has ever seen to its current corresponding variation. |
getDecisionString function[getDecisionString] | Child attribute of type StateObject | Get a single string describing the campaign/experiment decision for a given campaign ID. |
getDecisionObject function[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 CampaignFilterObject or CampaignFilterFunction | N/A | An object with fields to filter on. |
isActive Boolean | Child attribute of type CampaignFilterObject | Indicates if the campaign is active. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
object[CampaignStateObject] | N/A | Returns an object. The object keys are campaign IDs. The values describe the state of every campaign. |
id string | Child attribute of type CampaignStateObject | The ID of the campaign. |
campaignName string | Child attribute of type CampaignStateObject | The name of the campaign. |
experiment NameIdObject | Child attribute of type CampaignStateObject | An object with the name and the ID of the experiment the visitor is bucketed in. |
id string | Child attribute of experiment type NameIdObject | The ID of the object. |
name string | Child attribute of experiment type NameIdObject | The name of the object. |
variation NameIdObject | Child attribute of type CampaignStateObject | An object with the name and the ID of the variation the visitor is bucketed in. |
id string | Child attribute of variation type NameIdObject | The ID of the object. |
name string | Child attribute of variation type NameIdObject | The name of the object. |
isActive string | Child attribute of type CampaignStateObject | Indicates if the campaign is currently active. |
visitorRedirected string | Child attribute of type CampaignStateObject | Indicates if the visitor was redirected due to this campaign. |
isInCampaignHoldback Boolean | Child attribute of type CampaignStateObject | Indicates if the visitor is in the campaign holdback. |
audiences array | 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 CampaignFilterObject or CampaignFilterFunction | N/A | An object with fields to filter on. |
isActive Boolean | Child attribute of type CampaignFilterObject | Indicates if the experiment is active. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
object[ExperimentStateObject] | N/A | Returns an object. The object keys are experiment IDs. The values describe the state of every experiment. |
id string | Child attribute of type ExperimentStateObject | The ID of the experiment. |
experimentName string | Child attribute of type ExperimentStateObject | The name of the experiment. |
audiences array[NameIdObject] | Child attribute of type ExperimentStateObject | Audiences the visitor was in when the experiment was activated. |
id string | Child attribute of audiences type NameIdObject | The ID of the object. Required. |
name string | Child attribute of audiences type NameIdObject | The name of the object. Required. |
variation NameIdObject | 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 string | Child attribute of variation type NameIdObject | The ID of the object. Required. |
name string | Child attribute of variation type NameIdObject | The name of the object. Required. |
isActive string | Child attribute of type ExperimentStateObject | Indicates if the experiment is currently active. |
isInExperimentHoldback Boolean | Child attribute of type ExperimentStateObject | Indicates if the visitor is in the holdback (i.e., is excluded due traffic allocation). |
visitorRedirected string | 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 PageFilterObject | N/A | The argument indicating to get data information. |
isActive boolean | Child attribute of type PageFilterObject | Indicates if the page is active. |
Return value
Parameter and Type | Child Attribute | Description |
---|---|---|
object[PageStateObject] | N/A | Returns an object. The object keys are page IDs. The values describe the state of every page. |
id string | Child attribute of type PageStateObject | The ID of the page. |
name string | Child attribute of type PageStateObject | The name of the page. |
apiName string | Child attribute of type PageStateObject | The apiName of the page. |
staticConditions array | Child attribute of type PageStateObject | The category of the page. |
category string | Child attribute of type PageStateObject | The URL conditions for this page object. |
tags array[TagObject] | Child attribute of type PageStateObject | The tags associated to this page. |
type string | Child attribute of type TagObject | A static value that indicates that you are using the "tag" function. Required. |
tags PageTag | 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 Boolean | 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 | Description |
---|---|---|
object[VariationIdName] | N/A | 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 string | Child attribute of type VariationIdName | The ID of the variation. Required. |
name string | Child attribute of type VariationIdName | The name of the variation. Required. |
index number | 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 0 . Required. |
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
Parameter and Type | Child Attribute | Description |
---|---|---|
null or RedirectInfoObject | N/A | The object contains all the details about a redirect experiment that ran on the previous page. |
experimentId string | Child attribute of type RedirectInfoObject | The ID of the redirect experiment. Required. |
variationId string | Child attribute of type RedirectInfoObject | The ID of the redirect variation. Required. |
referrer string | 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 DecisionConfigObject | N/A | An object with configuration fields for the function. Required. |
campaignId string | Child attribute of type DecisionConfigObject | Indicates the campaign to get the decision string for. Required. |
shouldCleanString Boolean | Child attribute of type DecisionConfigObject | If true, any character that matches the regex /[^a-zA-Z0-9\.\~\!\*\(\)\']+/g is replaced with _ . Default is false. |
maxLength number | 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 | 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. More details on naming conventions for third-party integrations. |
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 DecisionConfigObject | N/A | An object with configuration fields for the function. Required. |
campaignId string | Child attribute of type DecisionConfigObject | Indicates the campaign to get the decision string for. Required. |
shouldCleanString Boolean | Child attribute of type DecisionConfigObject | If true, any character that matches the regex /[^a-zA-Z0-9\.\~\!\*\(\)\']+/g is replaced with _ . Default is false. |
maxLength number | 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 | N/A | 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 string or undefined | Child attribute of type GetDecisionObjectReturn | A string representing the decision's campaign name and ID. Not included if campaign is an AB test. |
experiment string | Child attribute of type GetDecisionObjectReturn | A string representing the decision's experiment name and ID. |
variation string | Child attribute of type GetDecisionObjectReturn | A string representing the decision's variation name and ID. |
holdback Boolean or undefined | 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.