State
Returns a module exposing several functions to access information about the current visitor's state.
Get the Optimizely state
Syntax
const state = window["optimizely"].get("state");Parameters
Parameter and Type | Child attribute | Description |
|---|---|---|
| N/A | The argument indicating to get the state object. Required. |
Return value
| Parameter and Type | Child attribute | Description |
|---|---|---|
StateObject | N/A | An object with functions that return information about the current state of Optimizely Web Experimentation. |
getActiveExperimentIds (function) | Child attribute ofStateObject | Get an array of experiment IDs that are currently active on the page for the visitor. |
getCampaignStates (function) | Child attribute ofStateObject | Get all the campaigns and their current states. |
getExperimentStates (function) | Child attribute ofStateObject | Get all the experiments and their current states. |
getPageStates (function) | Child attribute ofStateObject | Get all the pages and their current states. |
getRedirectInfo (function) | Child attribute ofStateObject | If a redirect variation executed on the previous page, return details of that experiment. |
getVariationMap (function) | Child attribute ofStateObject | Get a mapping from each experiment the visitor has ever seen to its current corresponding variation. |
getDecisionString (function) | Child attribute ofStateObject | Get a single string describing the decision for a given campaign ID. |
getDecisionObject (function) | Child attribute ofStateObject | Get an object with strings that describe the decision for a given campaign ID. |
Example
const state = window["optimizely"].get("state");getCampaignStates()
Get all the campaigns and their current states.
Syntax
state.getCampaignStates(filter);Parameters
| Parameter and Type | Child attribute | Description |
|---|---|---|
filter (CampaignFilterObject | CampaignFilterFunction) | Optional filter to limit results. |
CampaignFilterObject fields
| Parameter and Type | Child attribute | Description |
|---|---|---|
isActive (boolean) | Child attribute ofCampaignFilterObject | Indicates if the campaign is active. |
Return value
| Parameter and Type | Child attribute | Description |
|---|---|---|
object | N/A | Keys are campaign IDs. Values describe each campaign's state. |
id (string) | Child attribute ofCampaignStateObject | Campaign ID. |
campaignName (string) | Child attribute ofCampaignStateObject | Campaign name. |
experiment (NameIdObject) | Child attribute ofCampaignStateObject | The experiment the visitor is bucketed in. |
id (string) | Child attribute of experimentNameIdObject | Object ID. |
name (string) | Child attribute of experimentNameIdObject | Object name. |
variation (NameIdObject) | Child attribute ofCampaignStateObject | The variation the visitor is bucketed in. |
id (string) | Child attribute of variationNameIdObject | Object ID. |
name (string) | Child attribute of variationNameIdObject | Object name. |
isActive (boolean) | Child attribute ofCampaignStateObject | Whether the campaign is currently active. |
visitorRedirected (boolean) | Child attribute ofCampaignStateObject | Whether the visitor was redirected due to this campaign. |
isInCampaignHoldback (boolean) | Child attribute ofCampaignStateObject | Whether the visitor is in the campaign holdback. |
audiences (array) | Child attribute ofCampaignStateObject | Audiences the visitor was in when 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. Use a filter object or function to limit the results.
Example: filter with a function
const 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 | CampaignFilterFunction) | N/A | Optional filter to limit results. |
CampaignFilterObject fields
| Parameter and Type | Child attribute | Description |
|---|---|---|
isActive (boolean) | Child attribute ofCampaignFilterObject | Indicates if the experiment is active. |
Return value
Parameter and Type | Child attribute | Description |
|---|---|---|
| N/A | Keys are experiment IDs. Values describe each experiments state. |
| Child attribute of | Experiment ID. |
| Child attribute of | Experiment name. |
| Child attribute of | Audiences the visitor was in when the experiment was activated. |
| Child attribute of audiences | Object ID. Required. |
| Child attribute of audiences | Object name. Required. |
|
| Child attribute of Variation bucketed into, or |
| Child attribute of variation | Object ID. Required. |
| Child attribute of variation | Object name. Required. |
| Child attribute of | Whether the experiment is currently active. |
| Child attribute of | Whether the visitor is excluded due to traffic allocation. |
| Child attribute of | Whether 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). Use a filter object or function to limit results.
Examples
Filter with a function
const state = window["optimizely"].get("state");// "Not variation #1"state.getExperimentStates(function (expState) { return !expState.variation || expState.variation.name !== "Variation #1";});Filter with an object
const 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 | Optional filter to limit results. |
isActive (boolean) | Child attribute ofPageFilterObject | Indicates if the page is active. |
Return value
| Parameter and Type | Child attribute | Description |
|---|---|---|
object | N/A | Keys are page IDs. Values describe each page's state. |
id (string) | Child attribute ofPageStateObject | Page ID. |
name (string) | Child attribute ofPageStateObject | Page name. |
apiName (string) | Child attribute ofPageStateObject | API name of the page. |
staticConditions (array) | Child attribute ofPageStateObject | URL conditions for the page object. |
category (string) | Child attribute ofPageStateObject | Page category. |
tags (array) | Child attribute ofPageStateObject | Tags associated with the page. |
type (string) | Child attribute ofTagObject | Static value indicating thetag function (required). |
tags (PageTag) | Child attribute ofTagObject | Single-level JSON object with metadata (for URL-targeted pages). |
* (*) | Child attribute ofPageTag | Arbitrary project- or environment-specific properties. |
isActive (boolean) | Child attribute ofPageStateObject | Whether 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. Use a filter object to limit results.
Example: filter with a function
const state = window["optimizely"].get("state");state.getCampaignStates(function (pageState) { return !!pageState.isActive;});📘
NoteThe previous example demonstrates function-style filtering. Swap in
getPageStatesif you prefer to filter page states specifically.
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 | N/A | Keys are experiment IDs; values are variation descriptors. |
id (string) | Child attribute ofVariationIdName | Variation ID. Required. |
name (string) | Child attribute ofVariationIdName | Variation name. Required. |
index (number) | Child attribute ofVariationIdName | Index of the variation in the experiment’s list (0-based). 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, the experiment is considered active and the visitor is bucketed into a variation, but no visual changes are 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<string> | 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 the visitor is bucketed into a variation, but no visual changes are applied. This situation is referred to asisInExperimentHoldback in thegetExperimentStates API:
state.getExperimentStates()[{experiment_id}].isInExperimentHoldbackgetRedirectInfo()
If a redirect variation was executed on the previous page, all the experiment details are accessible on the next page throughgetRedirectInfo().
Syntax
state.getRedirectInfo();Return value
| Parameter and Type | Child attribute | Description |
|---|---|---|
null | RedirectInfoObject | Details about a redirect experiment that ran on the previous page, ornull if none. |
experimentId (string) | Child attribute ofRedirectInfoObject | Redirect experiment ID. Required. |
variationId (string) | Child attribute ofRedirectInfoObject | Redirect variation ID. Required. |
referrer (string) | Child attribute ofRedirectInfoObject | Original referrer value before the redirect. Required. |
Example call
state.getRedirectInfo();Example return value
{ "experimentId": "7669390999", "variationId": "7662661235", "referrer": "https://www.google.com/"}Description
Use this when you need to correct referrer values in analytics after a redirect variation.
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 | Configuration for the function. Required. |
campaignId (string) | Child attribute ofDecisionConfigObject | Campaign ID to get the decision string for. Required. |
shouldCleanString (boolean) | Child attribute ofDecisionConfigObject | Iftrue, replace characters matching/[^a-zA-Z0-9\.\~\!\*\(\)\']+/g with_. Default:false. |
maxLength (number) | Child attribute ofDecisionConfigObject | Maximum length of the returned string. Names are truncated preferentially (campaign first) to meet the limit. Default:255. |
Return value
| Parameter and Type | Child attribute | Description |
|---|---|---|
string | N/A | For personalization decisions: concatenation of campaign, experiment, and variation names/IDs and holdback status. For A/B decisions: concatenation of experiment and variation names/IDs. |
Example call
state.getDecisionString({ "campaignId": "6676370413" });Example return value
"Example personalization campaign(6676370413):Example experiment(6666781105):Variation #1(6626731852):holdback"Description
Often used to send campaign decision data to third-party analytics. Pass a campaign ID. If the campaign is an A/B test, the API returns only the fields relevant to A/B tests. Your code does not need to branch on campaign type.
getDecisionObject()
Get an object with strings that describe the campaign or experiment decision for a given campaign ID.
Syntax
state.getDecisionObject(config);Parameters
| Parameter and Type | Child attribute | Description |
|---|---|---|
config (DecisionConfigObject) | N/A | Configuration for the function. Required. |
campaignId (string) | Child attribute ofDecisionConfigObject | Campaign ID to get the decision object for. Required. |
shouldCleanString (boolean) | Child attribute ofDecisionConfigObject | Iftrue, replace characters matching/[^a-zA-Z0-9\.\~\!\*\(\)\']+/g with_. Default:false. |
maxLength (number) | Child attribute ofDecisionConfigObject | Maximum length of returned string fields. Default:255. |
Return value
Parameter and Type | Child attribute | Description |
|---|---|---|
| N/A | For personalization decisions, contains fields for campaign, experiment, variation, and holdback status. For A/B decisions, contains fields for experiment and variation. |
|
| Child attribute of |
| Child attribute of | Decision’s experiment name and ID. |
| Child attribute of | Decision’s variation name and ID. |
|
| Child attribute of |
Example call
state.getDecisionObject({ "campaignId": "6676370413" });Example return value
{ "campaign": "Shopping Cart (6676370413)", "experiment": "New Visitors (3243212353)", "variation": "Variation #1 (2344343121)", "holdback": false}Description
Often used to send campaign decision data to third-party analytics. Pass a campaign ID. If the campaign is an A/B test, the API returns only A/B-relevant fields so your code does not need to branch on type.