Sub topic 9
Tagging AI Visits with a Custom Event
GA4 can record an AI assistant as a referral when the browser sends a usable referrer. However, source and medium values may be missing or inconsistent. A custom event gives you a second signal: it records which known assistant sent the visitor.
This chapter assumes you understand GA4 reports, referrers, and UTM parameters. You also need Editor access to the GA4 property. The examples assume that the Google tag and gtag() are already installed.
What the custom event records
The event below is named ai_assistant_visit. It sends an assistant_name parameter, such as ChatGPT or Gemini.
The event does not change GA4 session attribution. It does not turn a Direct session into a Referral session. It creates an additional event that you can use in reports and comparisons.
const AI_HOSTS = {
"chatgpt.com": "ChatGPT",
"chat.openai.com": "ChatGPT",
"gemini.google.com": "Gemini",
"perplexity.ai": "Perplexity",
"www.perplexity.ai": "Perplexity",
"copilot.microsoft.com": "Copilot",
"claude.ai": "Claude"
};
let host = "";
try {
host = new URL(document.referrer).hostname.toLowerCase();
} catch (error) {
host = "";
}
const assistant = AI_HOSTS[host];
if (assistant && typeof gtag === "function") {
gtag("event", "ai_assistant_visit", {
assistant_name: assistant
});
}Place this code after the Google tag has loaded. A landing-page script is usually the best location because document.referrer normally contains the page that linked to the current page.
| Event field | Example value | Purpose |
|---|---|---|
| Event name | `ai_assistant_visit` | Identifies the custom event |
| `assistant_name` | `ChatGPT` | Identifies the assistant |
| Browser referrer | `https://chatgpt.com/...` | Source used for detection |
Add the event with Google Tag Manager
With Google Tag Manager (GTM), create a Custom HTML tag containing the detection code. Set the trigger to fire on page views. If your site is a single-page application, also trigger it when the initial history change is detected, but prevent duplicate events.
A simpler GTM setup can use a variable containing the referrer hostname and a custom event tag. The tag should fire only when the hostname matches your approved list. Keep the matching list narrow. A broad rule could classify unrelated domains incorrectly.
After the event arrives in GA4:
- Open Admin and select Custom definitions.
- Create a custom dimension.
- Set the dimension scope to Event.
- Use
assistant_nameas the event parameter. - Give the dimension a clear name, such as AI assistant.
Custom definitions apply to new data. They do not rewrite historical events.
Understand the measurement boundary
The script only detects a referrer visible to the browser. It cannot identify a visitor who copied a URL from an AI assistant, used a privacy tool that removed the referrer, or arrived through a redirect that discarded it.
It also detects human visits, not AI crawlers. Crawlers generally do not execute your website's GA4 tag in a normal browser session. Robots.txt logs, server logs, or search-provider reporting are separate data sources.
The relationship between the page load and the event looks like this:
Use the event in analysis
In an Exploration, add Event name as a filter for ai_assistant_visit. Add the AI assistant custom dimension as a row and compare it with landing page, engagement time, key events, or session source.
Compare event counts with sessions rather than treating them as identical. One session can generate more than one event if your implementation fires repeatedly. A missing event also does not prove that the visitor did not come from an AI assistant.
Exercise
If GA4 records an ai_assistant_visit event with assistant_name = Gemini, does this guarantee that the session's source is Gemini?
Show answer
No. The event is an independent signal. Session source depends on GA4's attribution rules and the referrer or UTM values available when the session began.