# 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. > [!NOTE] > > Your agent shares links exactly as they appear in your sources and instructions, and doesn't add UTM parameters to them. Tag the links in your sources if you want them to carry a fixed campaign tag. ## How it works | Step | What you do | Who | Where | | - | - | - | - | | [1. Save the UTMs](#step-1-save-the-utms) | Save the UTM parameters when a visitor arrives | Developer | Your website | | [2. Send chat events to your analytics](#step-2-send-chat-events-to-your-analytics) | Report chat events with the saved UTMs | Developer | Your website | ## Before you start > [!NOTE] > > You need a website with the QwryAI Chat bubble script already installed and working. New to QwryAI? Start with [Your First AI Agent](/docs/user-guides/quick-start/your-first-agent) to add the script first. ## 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](/docs/developer-guides/website-integration/chatbot-event-listeners) 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. > [!TIP] > > To count each conversation once, send `chat_message_sent` only the first time per visit, for example by saving a flag in `sessionStorage`. ## 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. > [!TIP] > > Test on your own website, not in the Playground. The Playground doesn't run your site's code. ## Troubleshooting > [accordion] 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. > [accordion] 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 > [card] Event Listeners | /docs/developer-guides/website-integration/chatbot-event-listeners | Listen for chat events and react to them in real time. > [card] Identity Verification | /docs/developer-guides/website-integration/identity-verification | Tell your agent which signed-in user it's talking to.