> For the complete documentation index, see [llms.txt](https://docs.intelligems.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.intelligems.io/developer-resources/javascript-api.md).

# Javascript API

For ease of use, Intelligems adds an object to the global window scope when the page loads. It primarily acts as a read interface for configurations and session-specific A/B tests.

## Overview

The Intelligems JS API lives on the window object: `window.igData`.

The 3 main attributes attached to the `igData` object are:

1. [User Object](/developer-resources/javascript-api/user-object.md) (`igData.user`) - Used for retrieving user, test group, and experiment info.
2. [Price Object](/developer-resources/javascript-api/price-object.md) (`igData.price`) - Used for complicated or custom price testing integrations.
3. [Campaigns Object](/developer-resources/javascript-api/campaigns-object.md) (`igData.campaigns`) - Used for customizing your onsite campaign experience

Note: `igData.offers` is an alias of `igData.campaigns` and exposes the same functions.

## Load Timing

As soon as Intelligems is fully loaded on the site, we will set the `igData` object on the window. As soon as this happens, we will fire a window event: `ig:ready`

### Event-based Ready Detection

Here's an example of how you might perform an action when Intelligems is loaded using events:

```javascript
window.addEventListener('ig:ready', () => {
 const experiments = window.igData?.user.getExperiments();
 if (experiments.length) console.log("I have live experiments");
})
```

### Callback-based Ready Detection

For a more convenient approach, you can use the `window.onIgReady` callback system. This property supports both single functions and arrays of functions:

```javascript
// Single callback function
window.onIgReady = () => {
  const experiments = window.igData?.user.getExperiments();
  if (experiments.length) console.log("I have live experiments");
};

// Array of callback functions
window.onIgReady = [
  () => console.log("First callback executed"),
  () => console.log("Second callback executed"),
  () => {
    const experiments = window.igData?.user.getExperiments();
    console.log(`Found ${experiments.length} experiments`);
  }
];
```

**Key Features:**

* **Immediate execution**: If you set `window.onIgReady` after Intelligems is already loaded, the callbacks will execute immediately
* **Error handling**: Each callback is executed safely with error handling to prevent one failing callback from breaking others
* **Flexible**: Supports both single functions and arrays of functions
* **Compatible**: Works alongside the existing `ig:ready` event system


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.intelligems.io/developer-resources/javascript-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
