Skip to content
QwryAI
Documentation
Pages

Website Integration

UTM Tracking

Credit chats with your agent to the campaigns that brought visitors to your site

Visitors from ads and email campaigns arrive with UTM parameters in the address, like utm_source=google. Your analytics reads them to credit the right campaign. This guide shows how to tell your analytics when those visitors chat with your agent, so chats count toward the campaign too.

How it works

StepWhat you doWhoWhere
1. Save the UTMsSave the UTM parameters when a visitor arrivesDeveloperYour website
2. Send chat events to your analyticsReport chat events with the saved UTMsDeveloperYour website

Before you start

Step 1: Save the UTMs

UTM parameters are only in the address of the first page a visitor lands on. They're gone once the visitor moves to another page, so save them in the browser on the first visit and read them back on every page.

Add the code

Add this to your site so it runs on every page:

JavaScript
function getUtmParams() {
  const keys = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
  const url = new URLSearchParams(window.location.search);
  const saved = JSON.parse(sessionStorage.getItem("qwryai_utm") || "{}");

  // Only save UTMs if none were saved earlier this visit
  if (Object.keys(saved).length === 0) {
    for (const key of keys) {
      if (url.get(key)) saved[key] = url.get(key);
    }
    sessionStorage.setItem("qwryai_utm", JSON.stringify(saved));
  }

  return saved;
}

How the code behaves

  • It keeps only the five standard UTM keys. Anything else in the address is ignored.
  • It keeps the first UTMs it sees. If the visitor later arrives from a different campaign in the same session, the first source is kept. To credit the latest campaign instead, remove the if check.
  • It uses sessionStorage. The parameters are cleared when the visitor closes the tab. Use localStorage if you want them to last across visits.

Step 2: Send chat events to your analytics

The Chat bubble tells your page when the chat opens and when the visitor sends a message. Listen for those events and send them to your analytics with the saved UTMs.

window.chatbot is ready once the widget script has loaded, so wait for it before you add listeners:

JavaScript
function whenChatReady(callback) {
  if (window.chatbot) return callback(window.chatbot);
  setTimeout(() => whenChatReady(callback), 300);
}

whenChatReady((chat) => {
  chat.on("open", () => {
    gtag("event", "chat_opened", getUtmParams());
  });

  chat.on("messageSent", () => {
    gtag("event", "chat_message_sent", getUtmParams());
  });
});

This example uses Google Analytics' gtag. Use your own analytics tool's call in its place.

Test it

  1. Visit your site with test UTMs

    Open your website with test parameters in the address, for example https://example.com/?utm_source=test&utm_campaign=qwryai_check.

  2. Chat with your agent

    Open the chat and send a message.

  3. Check your analytics

    In your analytics tool's real-time view, look for chat_opened and chat_message_sent with utm_source=test and utm_campaign=qwryai_check.

  4. Try another page

    Go to another page on your site, send another message, and check that the same UTM parameters arrive.

Troubleshooting

Events arrive without UTM parameters
  • Open your browser's developer console and run sessionStorage.getItem("qwryai_utm"). If it shows null or "{}", the parameters weren't saved. Make sure the code from Step 1 runs on the page you landed on.
  • Check that the code from Step 2 runs on every page, not just the first one.
No events arrive at all
  • In the console, check that window.chatbot exists once the Chat bubble has appeared.
  • Check the event names: open and messageSent, with that spelling and case.

Next steps

Explore the documentation