Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideProduct feedbackGitHubNuGetDev CommunitySubmit a ticketLog In

Behavior

Describes behavior API functions

You can use the behavior APIs if Optimizely 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 TypeChild AttributeDescription
behavior
string
N/ARequired.

Return value

Parameter and TypeChild AttributeDescription
BehaviorObjectN/A-
query
function[query]
Child attribute of type BehaviorObjectThis 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 TypeChild AttributeDescription
queryObject
object
N/AA behavioral query object. Required.

Return value

Parameter and TypeChild AttributeDescription
Object or Array or number or Boolean or stringN/AThe result of the behavioral query.

Example Call

behavior.query("queryObject");

Examples

Refer to Query behaviorial objects for a step-by-step guide on how to build a JSON object to create a behavioral query in Optimizely Web Personalization.

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"
  }]
});