---
title: "Introduction to the klaviyo object"
description: "Learn more about the new klaviyo JavaScript object, which offers full support for callbacks and promises."
url: "https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object"
category: "Build with Klaviyo"
lastUpdated: "2026-11-10"
related: ["https://developers.klaviyo.com/en/docs/llms.txt"]
---

# Introduction to the klaviyo object

> This is the Markdown version of https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object

Learn more about the new klaviyo JavaScript object, which offers full support for callbacks and promises.

> [!NOTE]
> Check out our video on how to <a href="https://www.youtube.com/watch?v=O2oHTGpBdlQ&list=PLHkNfHgtxcUanrkMnKPdkRzuWU7MGv_xM&index=3" target="_blank">use Klaviyo's JavaScript for onsite tracking</a>.

## What is the `klaviyo` object?

The new _klaviyo_ object replaces the legacy \__learnq_ and _klOnsite_ objects. These JavaScript objects offer a shorthand way to interact with our APIs and send events into Klaviyo with [event tracking](https://developers.klaviyo.com/en/docs/custom_event_tracking.md). The _klaviyo_ object offers robust support for asynchronous JavaScript implementations with callbacks and promises. It also supports existing _klOnsite_ functionality, such as opening sign up forms with custom triggers and executing end-user provided callbacks, which provide better control over when forms are displayed.

klaviyo.js, also known as [Klaviyo’s “Active on Site” JavaScript](https://developers.klaviyo.com/en/docs/guide_to_integrating_a_platform_without_a_pre_built_klaviyo_integration.md), automatically supports the _klaviyo_ object. If you have enabled an integration with your Klaviyo account or installed klaviyo.js manually, you will be able to initiate _klaviyo_ to listen for relevant calls.

### How to load the `klaviyo` object

To use the _klaviyo_ object immediately on page load, we recommend manually installing the snippet on your site. This snippet would exist in addition to [Klaviyo's onsite script](https://developers.klaviyo.com/en/docs/javascript_api.md). The _klaviyo_ object only needs to be loaded once per page.

To load the _klaviyo_ object:

```javascript
!function(){if(!window.klaviyo){window._klOnsite=window._klOnsite||[];try{window.klaviyo=new Proxy({},{get:function(n,i){return"push"===i?function(){var n;(n=window._klOnsite).push.apply(n,arguments)}:function(){for(var n=arguments.length,o=new Array(n),w=0;w<n;w++)o[w]=arguments[w];var t="function"==typeof o[o.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push([i].concat(o,[function(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo||[],window.klaviyo.push=function(){var n;(n=window._klOnsite).push.apply(n,arguments)}}}}();
```

### Callback support

_Klaviyo_ offers full support for callbacks, giving you more control over the order in which your functions are invoked. Callbacks will be called with a return value.

For example, to identify a cookied user without a callback using _klaviyo_:

`klaviyo.identify({});`

With the _klaviyo_ object, you can now pass in a callback to the identify call. This allows the customer to be identified as well as track any onsite event without having to reload the page (ex: viewed product):

```javascript
function myCallback() {
  var item = {
    "ProductName": item.ProductName,
    "ProductID": item.ProductID,
    "SKU": item.SKU,
    "Categories": item.Categories,
    "ImageURL": item.ImageURL,
    "URL": item.URL,
    "Brand": item.Brand,
    "Price": item.Price,
    "CompareAtPrice": item.CompareAtPrice
  };
  klaviyo.track("Viewed Product", item);
}

klaviyo.identify({
  'email' : 'thomas.jefferson@example.com',
  'first_name' : 'Thomas',
  'last_name' : 'Jefferson'
}, myCallback);
```

### Promise support

The _klaviyo_ object also supports promises, which offer a cleaner approach to resolving asynchronous requests. This option eliminates the potential complexity of nested callbacks and can simplify your code. Promises will be resolved with a return value.

For example, to return a promise when an identify call has been completed:

```javascript
klaviyo.identify({}).then(() => console.log('Identify has been completed'));
```

## Supported methods

The _klaviyo_ object supports the following methods. Optional parameters are represented by a `?`.

- openForm
  - **Parameters** - `formId: string`, `callback?: Function`
  - **Return type** - none
- identify
  - **Parameters** - `properties: Object`, `callback?: Function`
  - **Return type** - object
- track
  - **Parameters** - `event: string`, `properties?: Object`, `callback?: Function`
  - **Return type **- boolean
- trackViewedItem
  - **Parameters** - `item: Object`, `callback?: Function`
  - **Return type** - none
- account
  - **Parameters** - `account_id?: string`, `callback?: Function`
    If an `account_id` string is provided, it will set the account id.
  - **Return type** - string
- cookieDomain
  - **Parameters** - `cookieDomain?: string`, `callback?: Function`
    If a `cookieDomain` string is provided, it will set the cookie domain.
  - **Return type** - string
- isIdentified
  - **Parameters** - `callback?: Function`
  - **Return type** - boolean

#### Return values:

`klaviyo.push([‘method’, ...params, callback])` returns `None`, and instead executes a callback with a return type.

`klaviyo.method(...params, callback)` returns a Promise that resolves to a return type.

**Examples:**

- `klaviyo.push([‘identify’, {}, (result) => console.log(“Identify result “ + result))` returns `None`

- `klaviyo.identify({})` returns `Promise<Object>`

- `klaviyo.identify({}, (result) => console.log(“Identify result “ + result))` returns `Promise<Object>`

## Update `_learnq` code to use `klaviyo`

If you have implemented custom code using the \__learnq_ object, we recommend that you update your code to use _klaviyo_.

You can quickly update instances of the legacy `learnq.push` to use the new `klaviyo.push.`

For example:
`_learnq.push([‘identify’,{}]); `
becomes
`klaviyo.push(['identify', {}]);`

### Can I still use `_learnq` and `klOnsite`?

We will continue to support the functionality of legacy objects such as \__learnq_ and _klOnsite_, so that sites using them will not break and you will have time to adopt the new _klaviyo_ object.

However, we highly recommend moving to _klaviyo_ as soon as possible to utilize its additional features and broad support for callbacks, promises, and functions.
