Sub topic 3
Tagging AI visits with a custom event
This chapter assumes you know the GA4 interface, can open Realtime and Explore, and understand what a referrer is. You need Editor access to the property, and Administrator access if you configure data streams or custom definitions.
Tagging AI visits with a custom event
A custom event records an AI visit even when the channel group misclassifies it. The snippet below checks the page referrer on load and sends ai_assistant_visit when the host matches a known assistant. It needs the gtag library to be loaded first.
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;
} catch (e) {}
const assistant = AI_HOSTS[host];
if (assistant) {
gtag('event', 'ai_assistant_visit', { assistant_name: assistant });
}Analysing AI referrals in the BigQuery export
If you link GA4 to BigQuery, the daily events_* tables hold every event. The query below counts page views per day and referrer host for a chosen date range.
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
REGEXP_EXTRACT(page_referrer, r'^https?://([^/]+)') AS referrer_host,
COUNT(*) AS page_views
FROM (
SELECT
event_date,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer') AS page_referrer
FROM `my_project.analytics_123456789.events_*`
WHERE event_name = 'page_view'
AND _TABLE_SUFFIX BETWEEN '20261101' AND '20261130'
)
WHERE REGEXP_CONTAINS(page_referrer, r'(chatgpt\.com|chat\.openai\.com|gemini\.google\.com|perplexity\.ai|copilot\.microsoft\.com|claude\.ai)')
GROUP BY day, referrer_host
ORDER BY day, page_views DESC;Testing that your setup works
- Open GA4 DebugView, or enable debug mode with the GA Debugger extension.
- Click a link to your site from a test page on an AI assistant domain, or from a page you host on one of the listed hosts.
- Confirm that
ai_assistant_visitappears in DebugView with the expectedassistant_name. - Check Realtime to confirm the session is assigned to your AI Assistants channel.
AI crawlers versus visitors who click a link
Crawlers that fetch pages for AI training or search indexes usually do not run JavaScript. The gtag script never runs for them, so GA4 does not record those requests. A crawler-only hit leaves no session, no event, and no referrer.
Limits of the data and how to read the numbers
- Users who arrive from apps or copy and paste a link often send no referrer, so their visits appear as Direct.
- Consent banners, ad blockers, and privacy browsers can stop the script from sending events.
- Referrer policies can strip or shorten the referrer, so host matching misses some visits.
- Treat the counts as a lower bound, and compare trends over time rather than exact totals.
Exercise
Exercise
Your event fires for ChatGPT visits, but Explore shows no assistant_name values. Name the most likely cause and the fix.
Show answer
The assistant_name parameter has not been registered as an event-scoped custom dimension. Create the custom dimension in GA4 Admin, then wait for new data to arrive, since dimensions apply only to data collected after creation.