# Send chat activity to your analytics

> Forward chat opens, messages, form submissions and ratings to Google Analytics or any tracker.

help.ai already counts how visitors use the widget, under **Analytics** → **Deployments**. To see chat activity next to the rest of your site's data, such as in Google Analytics, forward the widget's events to your tracker.

This is the other direction from [Report your site's events to help.ai](https://docs.help.ai/customize-and-extend-the-widget/report-site-events-with-track), which sends your page's events to help.ai.

## Pick the events you need

| Event | Fires when | What you get |
| --- | --- | --- |
| `open`, `close` | The chat opens or closes | Nothing |
| `message` | The visitor sends a message, or a reply from the AI agent or your team finishes | `{ role, text }`, where `role` is `user`, `assistant` or `operator` |
| `formSubmit` | The visitor submits or skips a form | `{ formId, values, skipped }` |
| `messageFeedback` | The visitor rates a reply | `{ messageId, value }`: `up`, `down`, or `null` when they take a rating back |
| `contentOpen` | The visitor opens an article or post | `{ contentId, contentCollectionId }` |
| `contentSearch` | The visitor searches Help | `{ queryLength, hitCount }`, never the search text |
| `localeChange` | The visitor picks another language | The language code, such as `fr` |
| `error` | The widget can't load or send | The error |

`message.text` and a form's `values` are what the visitor typed. Keep them out of trackers that mustn't hold personal data: send the `role` or the `formId` instead.

## Send them to Google Analytics

With the **Quick install** script tag, put this after your Google tag. It works before and after the widget's script has loaded:

```html
<script>
  window.HelpAI = window.HelpAI || function () { (HelpAI.q = HelpAI.q || []).push(arguments); };

  HelpAI('hook', 'default', 'open', () => gtag('event', 'chat_open'));
  HelpAI('hook', 'default', 'message', (chat, event, data) => {
    if (data.role === 'user') gtag('event', 'chat_message_sent');
  });
  HelpAI('hook', 'default', 'formSubmit', (chat, event, data) => {
    gtag('event', 'chat_form', { form_id: data.formId, skipped: data.skipped });
  });
</script>
```

`HelpAI('hook', widget, event, callback)` calls you back with the widget, the event's name and its data. Use `'*'` as the widget to hear every widget on the page. `'*'` as the event hears every event, including many you won't want in reports, so name the ones you need.

## Listen from your own code

```js
const chat = await HelpAI.ready();
const stop = chat.on('messageFeedback', ({ value }) => {
  if (value) analytics.track('Chat reply rated', { value });
});
```

- `on()` returns a function that stops listening.
- With the NPM package, `init()` also takes `onOpen`, `onClose`, `onMessage` and `onError` callbacks.
- `HelpAI('hook', …)` doesn't hear a `<web-ai-chat>` element: call `on()` on the element instead.
- A callback that throws an error can't break the widget.

<Tip>
Every event and its data is in the [API reference](https://cdn.help.ai/sdk/js/elements/latest/docs/api-reference.md).
</Tip>
