Skip to content

Send chat activity to your analytics

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

Last updated

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, which sends your page's events to help.ai.

Pick the events you need#

EventFires whenWhat you get
open, closeThe chat opens or closesNothing
messageThe visitor sends a message, or a reply from the AI agent or your team finishes{ role, text }, where role is user, assistant or operator
formSubmitThe visitor submits or skips a form{ formId, values, skipped }
messageFeedbackThe visitor rates a reply{ messageId, value }: up, down, or null when they take a rating back
contentOpenThe visitor opens an article or post{ contentId, contentCollectionId }
contentSearchThe visitor searches Help{ queryLength, hitCount }, never the search text
localeChangeThe visitor picks another languageThe language code, such as fr
errorThe widget can't load or sendThe 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.

Every event and its data is in the API reference (opens another site).

Was this helpful?

Still need help?

Ask the community