> For the complete documentation index, see [llms.txt](https://docs.iadvize.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iadvize.dev/technologies/web-and-mobile-sdk/javascript-callbacks/guides.md).

# Guides

## Safely adds events in `window.iAdvizeCallbacks` object <a href="#safely-adds-events-in-window.iadvizecallbacks-object" id="safely-adds-events-in-window.iadvizecallbacks-object"></a>

If you need to add iAdvize callbacks in different places in your code, you should always declare your callbacks like the following example to avoid overwriting a previous callback definition:

```javascript
window.iAdvizeCallbacks = {
  ...window.iAdvizeCallbacks,
  
  onChatStarted() {
    // Do something
  },
  
  onChatEnded() {
    // Do something
  }
  
};

// Later in your code:
window.iAdvizeCallbacks = {
  ...window.iAdvizeCallbacks,
  
  onMessageReceived(context) {
    // Do something
  }
  
};
```

With this approach, iAdvize will see 3 callbacks: `onChatStarted`, `onChatEnded` et `onMessageReceived`.

## Safely define a function for an event <a href="#safely-define-a-function-for-an-event" id="safely-define-a-function-for-an-event"></a>

If your code is splitted in multiple files or module, you may need to define several functions for the same event.

Here is a solution to avoid overwriting a previous function:

```javascript
var tempOnChatStarted = window.iAdvizeCallbacks.onChatStarted || () => {};

window.iAdvizeCallbacks = {
  ...window.iAdvizeCallbacks,
  
  onChatStarted(context) {

    // Call the possible already defined callback
    tempOnChatStarted(context);

    // Your new custom code here
    // ...
    
  }
  
};
```

## Track some Google Analytics events <a href="#track-some-google-analytics-events" id="track-some-google-analytics-events"></a>

```javascript
window.iAdvizeCallbacks = {

  ...window.iAdvizeCallbacks,
  
  // Chat conversation starts
  onChatStarted(context) {
    _gaq.push(['_trackEvent', 'iAdvize', 'Chat Start', context.startedBy]);
  },
  
  // Chat conversation ends
  onChatEnded(context) {
    _gaq.push(['_trackEvent', 'iAdvize', 'Chat End', context.startedBy]);
  },
  
  // Call conversation starts
  onCallStarted() {
    _gaq.push(['_trackEvent', 'iAdvize', 'Call Start']);
  },
  
  // Call conversation ends
  onCallEnded() {
    _gaq.push(['_trackEvent', 'iAdvize', 'Call End']);
  }

};
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.iadvize.dev/technologies/web-and-mobile-sdk/javascript-callbacks/guides.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
