How to track AI referral traffic in GA4 with Google Tag Manager

AI traffic is messy. Someone can ask ChatGPT, Perplexity, Gemini, Claude, Copilot, or another AI tool for a recommendation, click a link, and land on your site. Sometimes GA4 shows a useful source. Sometimes the visit looks like referral traffic. Sometimes it falls into direct.

That makes reporting harder than it should be.

The Visible AI Referral / UTM Classifier is a Google Tag Manager variable template that helps with the part of AI traffic that leaves a usable signal.

It checks for AI-related signals in UTM sources, browser referrers, and optional custom host mappings. When it finds a match, it returns a clean source label such as chatgpt, perplexity, claude, gemini, copilot, deepseek, grok, notebooklm, or brave_ai.

If it does not find a supported AI source, it returns (none).

The scope is simple: classify visible AI referral and UTM traffic so GA4 has a cleaner field to report on.

You can download the template here: Visible AI Referral / UTM Classifier on GitHub

What this template is for

Use this when you want a cleaner way to separate known AI platform traffic in GA4.

For example, you may want to know whether people are reaching your site from ChatGPT, Perplexity, Gemini, Claude, Copilot, or another AI answer engine.

GA4 may not give you a neat reporting field for that by default. This template gives you one.

The setup works like this:

AI source signal → GTM variable → GA4 event parameter → GA4 custom dimension

Once it is working, you can report on a field like ai_source.

That gives you a cleaner way to answer questions like:

  • Are we getting traffic from AI platforms?
  • Which AI tools are sending traffic?
  • Are those visits landing on useful pages?
  • Are AI-referred visitors engaging?
  • Should we look closer at how our content appears in AI answers?

Treat the data as a visible signal. Some AI-related visits will still be unclassified if the browser or platform does not pass enough information.

What this template can miss

Some AI visits leave a trail. Others do not.

This template may miss:

  • direct visits with no source data
  • copied and pasted URLs
  • AI app traffic that strips the referrer
  • private browser handoffs
  • native mobile app clicks with no usable signal
  • visits where the AI platform does not pass a UTM or referrer

For example, if someone asks an AI tool for a recommendation, copies your URL, and pastes it into their browser, this template will not detect that path.

That is a current limitation of browser and analytics data. The useful move is to clean up the AI traffic you can see.

What you will set up

By the end of this setup, you will have:

  • imported the GTM variable template
  • created a variable that returns the detected AI source
  • passed that value into GA4 as ai_source
  • tested the setup with controlled URLs
  • checked the value in GTM Preview
  • confirmed the event parameter in GA4 DebugView
  • registered ai_source as a GA4 custom dimension
  • published the GTM container with clear version notes

Before you start

You need edit access to the right Google Tag Manager container.

You also need permission to import custom templates, create variables, update GA4 tags, and publish or submit GTM changes.

From the GitHub repository, use the template.tpl file.

Do not import README.md or metadata.yaml into GTM. Those files are for documentation and template metadata.

Step 1: import the template into GTM

Open the correct Google Tag Manager container.

Go to:

Templates → Variable templates → New

In the template editor:

  1. Click the three-dot menu in the top-right corner.
  2. Select Import.
  3. Upload template.tpl.
  4. Save the template.

After saving, you should see the custom variable template in GTM as:

Visible AI Referral / UTM Classifier Variable

Google has its own documentation on GTM custom templates if you want the platform-level reference:

Google Tag Manager custom templates documentation

Step 2: create the GTM variable

Now create the variable you will use in your GA4 tags.

Go to:

Variables → New

Name the variable:

AI Source – Visible Referral / UTM

Then choose the imported template:

Visible AI Referral / UTM Classifier Variable

Set the fallback value to:

(none)

Leave “Return matched host instead of normalized platform label” unchecked unless you specifically need the full host name.

For reporting, normalized labels are usually cleaner.

Use chatgpt instead of chatgpt.com.

The shorter label is easier to group in GA4 and keeps your reports cleaner.

Step 3: add custom AI host mappings if needed

You only need custom mappings if you want to classify an AI platform or internal AI tool that is not already covered.

HostLabel
example-ai.comexample_ai
internal-ai-tool.company.cominternal_ai_tool

Keep the labels simple and consistent.

Good labels:

  • example_ai
  • internal_ai_tool

Messy labels:

  • Example AI
  • example-ai
  • Example_AI

GA4 reporting gets ugly fast when naming is inconsistent. Keep it boring.

Step 4: send the value into GA4

The GTM variable does not send anything to GA4 on its own. You need to add it to your GA4 tag as an event parameter.

Use this setup:

Parameter nameValue
ai_source{{AI Source - Visible Referral / UTM}}

You can add this to your GA4 page view setup or to specific events, depending on how your container is built.

For most sites, I would start by sending it with page views. That gives you a clean read on landing traffic before deciding whether to pass it into other events.

Step 5: create a temporary debug trigger

Before publishing, test this without polluting normal tracking.

Create a page view trigger called:

Page View – AI Classifier Debug

Set it to fire on some page views.

Use this condition:

Page URL contains ai_debug=1

This lets you trigger a test event only when your URL includes ai_debug=1.

Example:

https://www.example.com/?utm_source=chatgpt&ai_debug=1

Step 6: create a temporary GA4 test event

Create a temporary GA4 event tag.

Name it:

GA4 – AI Classifier Test Event

Use this event name:

ai_classifier_test

Add these event parameters:

Parameter nameValue
ai_source{{AI Source - Visible Referral / UTM}}
page_location{{Page URL}}
page_referrer{{Referrer}}

Use the debug trigger you created:

Page View – AI Classifier Debug

This tag is just for QA. Remove it or pause it before the final publish unless you want to keep it for future testing.

Step 7: test with UTM values

Open GTM Preview mode and test URLs like these:

https://www.example.com/?utm_source=chatgpt&ai_debug=1
https://www.example.com/?utm_source=perplexity&ai_debug=1
https://www.example.com/?utm_source=claude&ai_debug=1
https://www.example.com/?utm_source=randomsource&ai_debug=1

Expected results:

Test URLExpected ai_source
?utm_source=chatgpt&ai_debug=1chatgpt
?utm_source=perplexity&ai_debug=1perplexity
?utm_source=claude&ai_debug=1claude
?utm_source=randomsource&ai_debug=1(none)

In GTM Preview:

  1. Click the page view event.
  2. Open the Variables tab.
  3. Find AI Source – Visible Referral / UTM.
  4. Confirm the value.

Test the negative case too. If random traffic gets classified as AI traffic, the setup needs more work.

Step 8: test ambiguous UTM values

This is where bad reporting usually starts.

Some teams use vague UTM values. For example:

  • ?utm_source=ai&ai_debug=1
  • ?utm_source=chat&ai_debug=1
  • ?utm_source=open&ai_debug=1
  • ?utm_source=copy&ai_debug=1
  • ?utm_source=notion&ai_debug=1

Test those before publishing.

A value like utm_source=ai might look obvious, but it could mean almost anything depending on how your team tags campaigns.

If one of these values creates a false match, tighten the setup before publishing.

Step 9: test referrer detection separately

UTM testing is easy because you control the URL.

Referrer testing is different. If you paste a URL straight into the browser, you usually will not get a useful referrer. To test referrer detection, you need to click from a page that passes one.

A simple test flow:

  1. Put a test link on a controlled page.
  2. Click that link into your test page.
  3. Open GTM Preview.
  4. Check the Referrer value.
  5. Confirm the AI source variable returns the expected label when the referrer host is supported.

Do not assume referrer detection works because UTM detection works. They are different checks.

Step 10: check GA4 DebugView

After GTM Preview looks right, open GA4 DebugView.

Look for the test event:

ai_classifier_test

Then check whether the ai_source parameter is coming through.

For a ChatGPT test, you should see:

ai_source = chatgpt

For a non-AI test, you should see:

ai_source = (none)

You want both results before moving on.

Step 11: register ai_source as a GA4 custom dimension

Once GA4 is receiving the parameter, register it as a custom dimension.

In GA4, go to:

Admin → Custom definitions → Create custom dimension

Use this setup:

FieldValue
Dimension nameAI Source
ScopeEvent
Event parameterai_source

Save it.

GA4 may take time before the new dimension shows in standard reports.

Google’s documentation on custom dimensions is here: GA4 custom dimensions and metrics documentation

Step 12: validate the reporting

Before you call this done, check the boring stuff. That is where tracking setups usually break.

CheckStatus
The template is imported into GTM
The GTM variable is created
The fallback value is (none)
The GA4 event parameter is named ai_source
Positive UTM tests return the right AI source
Non-AI UTM tests return (none)
Ambiguous UTM values have been tested
Referrer detection has been tested separately
GA4 DebugView receives ai_source
ai_source is registered as an event-scoped custom dimension
Temporary debug tags are removed or paused
The GTM container is published with clear notes

Step 13: publish the GTM container

Before publishing, remove or pause the temporary debug event tag unless you want to keep it for future QA.

Use a clear GTM version name:

Add Visible AI Referral / UTM Classifier

Use a clear version description:

Imported Visible AI Referral / UTM Classifier variable template and added ai_source event parameter for visible AI referral and UTM classification.

Then publish the container.

A practical note on AI traffic

AI referral tracking is still messy.

Some visits will be visible. Some will look like direct traffic. Some will get grouped under normal referral sources. Some will not carry enough signal to classify.

I would use the phrase “visible AI referral classification” when describing this setup. It is less flashy, but it is more accurate.

For GA4 reporting, that accuracy matters.

If you want to use the template, you can grab it here: Visible AI Referral / UTM Classifier on GitHub