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
| Step | What you do | Who | Where |
|---|---|---|---|
| 1. Save the UTMs | Save the UTM parameters when a visitor arrives | Developer | Your website |
| 2. Send chat events to your analytics | Report chat events with the saved UTMs | Developer | Your 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:
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
ifcheck. - It uses
sessionStorage. The parameters are cleared when the visitor closes the tab. UselocalStorageif 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:
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
- 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. - Chat with your agent
Open the chat and send a message.
- Check your analytics
In your analytics tool's real-time view, look for
chat_openedandchat_message_sentwithutm_source=testandutm_campaign=qwryai_check. - 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 showsnullor"{}", 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.chatbotexists once the Chat bubble has appeared. - Check the event names:
openandmessageSent, with that spelling and case.