Skip to main content
Make every link your AI Agent shares carry the visitor’s UTM parameters, so your analytics still knows which campaign brought them in.

Why you need this

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. Links your AI Agent shares in the chat don’t have them. A visitor who clicks one and signs up may not be credited to the ad that brought them in.

How it works

Before you start

You need a website with the Chatbase embed script already installed and working. New to Chatbase? Check out Your First AI Agent to get started with the embed 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. Your AI Agent also forgets its user attributes on every page load, so you need to send them again on each page. The fix is to save the parameters 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, before you call the Chatbase identify method:

How the code behaves

  • It only keeps the five standard UTM keys. Anything else in the address is ignored. This keeps random query parameters away from your AI Agent and keeps the attributes short.
  • It keeps the first UTMs it sees. If the visitor later clicks through from a different campaign in the same session, the original source is kept. To credit the most recent campaign instead, remove the if check so new UTMs replace the saved ones.
  • It uses sessionStorage. The parameters are cleared when the visitor closes the tab. Use localStorage instead if you want them to last across visits.

Step 2: Pass them to your AI Agent

Send the saved parameters to your AI Agent as user attributes. Your AI Agent reads user attributes while it replies, so it will know which campaign the visitor came from. Pick the tab that matches your visitors. If your website has both logged-in and logged-out visitors, you need both.
For visitors who aren’t logged in, pass the parameters inside user_metadata. You don’t need a token or a user ID.
To set this up before the Chatbase script loads, use window.chatbaseUserConfig instead:
User attributes are visible to your AI Agent. UTM parameters aren’t sensitive, so this is fine. Still, never put private information in these attributes. That belongs inside the signed token.

Step 3: Update your instructions

Your AI Agent now knows where the visitor came from, but it won’t do anything with that on its own. You need to tell it what to do in its instructions.

What your AI Agent sees

Each attribute appears as a line of text at the start of the conversation, like this:

Add the instructions

1

Open your instructions

In your Chatbase Dashboard, open your AI Agent and go to Build > Instructions.
2

Paste the text below

Add it to the end of your instructions, and replace example.com with your own domain.
3

Save

Click Save changes.
For more on writing instructions, see Build.
AI models usually follow clear instructions like these, but not every time, so your AI Agent may now and then share a link without the parameters. Test it on your own site before you depend on it.

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=chatbase_check.
2

Ask for a link

Open the chat and ask something that should get a link back, like “Where can I see your pricing?”
3

Check the link

The link in the reply should end with utm_source=test&utm_campaign=chatbase_check.
4

Try another page

Go to another page on your site and ask for another link. The same UTM parameters should still be there.
Test in the widget on your own website, not in the Playground. The Playground doesn’t run your site’s code, so it never receives the UTM parameters.

Troubleshooting

Good to know

The source cards shown under a reply link to your data sources as they are. Only links your AI Agent writes in the reply itself get the UTM parameters.
All user attributes together are cut off at 1,000 characters. The five UTM parameters are well under that. If you also pass many other attributes, the last ones may not reach your AI Agent.
There’s no {{utm_source}} placeholder syntax in instructions. Your AI Agent reads the attribute lines and adds the values itself, which is why the instructions above describe what the lines look like.
To tag every link as coming from the chat, like utm_source=chatbase, you don’t need Steps 1 and 2. Tell your AI Agent in its instructions to add those exact parameters to every link to your domain.

Next steps

Identity Verification

Identify logged-in users and pass more attributes to your AI Agent

Event Listeners

Learn to listen for and respond to chat events in real-time

Custom Initial Messages

Create dynamic, personalized initial messages for users