You can use the behavior APIs if Optimizely X Web Personalization is enabled on your account.

If not, window.optimizely.get('behavior') will return undefined.

Syntax

behavior = window["optimizely"].get("behavior");
Parameters

Parameter and Type

Child Attribute

Description

behavior
string

Required.

Return value

Parameter and Type

Child Attribute

Description

BehaviorObject

query
function[query]

Child attribute of type BehaviorObject

This API function runs a behavioral query over the events that have taken place on this device.

Currently, the API only considers events that took place before the most recent activation of the Optimizely snippet. You must reload the page if you want to query over events that took place on the current page load.

Example Call

behavior = window["optimizely"].get("behavior");

Example Return Value

behavior.query(queryObject);

query()

This API function runs a behavioral query over the events that have taken place on this device.

Currently, the API only considers events that took place before the most recent activation of the Optimizely snippet. You must reload the page if you want to query over events that took place on the current page load.

Syntax

behavior.query(queryObject);
Parameters

Parameter and Type

Child Attribute

Description

queryObject
object

A behavioral query object. Required.

Return value

Parameter and Type

Child Attribute

Description

Object or Array or number or Boolean or string

The result of the behavioral query.

Example Call

behavior.query("queryObject");

Examples

Page names

Obtain a list of page names, sorted by viewing frequency.

Example code for page names
var behavior = window.optimizely.get('behavior');
var pageNames = behavior.query({
  "version": "0.2",
  "filter": [{
    "field": ["type"],
    "value": "pageview"
  }],
  "pick": {
    "field": ["name"]
  },
  "sort": [{
    "field": ["frequency"],
    "direction": "descending"
  }]
});

Did this page help you?