---
title: Behavior
slug: web-experimentation/api/wRTJ-behavior
docTags: 
createdAt: 2026-08-05T20:56:17.000Z
---

You can use the `behavior` APIs if [Optimizely Web Personalization](https://www.optimizely.com/personalization/) is enabled on your account.

If not, `window.optimizely.get('behavior')` returns `undefined`.

## Behavior

### Syntax

```javascript
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

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

### Example return value

```javascript
behavior.query(queryObject);
```

## query()

This API function runs a behavioral query over the [events](docId\:CZ4hQpTY0yu14DgjzGKQF) 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

```javascript
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

```javascript
behavior.query("queryObject");
```

## Examples

Refer to [Query behavioral objects](docId:_8v0KtSilg8gpYtlCXe29) 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

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