Describes behavior API functions in Optimizely Personalization.
You can use the behavior APIs if Optimizely Web Personalization is enabled on your account.
If not, window.optimizely.get('behavior') returns undefined.
Behavior
Syntax
behavior = window["optimizely"].get("behavior");
Parameters
| Parameter and type | Child attribute | Description |
|---|---|---|
| behavior string | N/A | Required. |
Return value
| Parameter and type | Child attribute | Description |
|---|---|---|
| BehaviorObject | N/A | N/A |
| 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 | N/A | A behavioral query object. Required. |
Return value
| Parameter and type | Child attribute | Description |
|---|---|---|
| Object or Array or number or Boolean or string | N/A | The result of the behavioral query. |
Example call
behavior.query("queryObject");Examples
Refer to Query behavioral objects for a step-by-step guide on how to build a JSON object to create a behavioral query in Optimizely 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"
}]
});