Sub topic 11
Testing that your setup works
This chapter assumes you have already built the pieces from earlier chapters. These are the AI Assistants custom channel group, the optional ai_assistant_visit event, and, if you use it, the BigQuery export. You need Editor access to the GA4 property to read DebugView and the reports. You need a browser with developer tools. Knowing what a referrer and a UTM parameter are is enough background.
What you are testing
The setup has several layers, and each can fail without the others noticing. A visit can arrive with the right referrer and still be filed under Referral because of a rule-order mistake. The event can fire in your browser and never reach GA4. Test each layer on its own, so a failure points to one cause.
Work through the layers from left to right. If an early layer fails, later results mean nothing.
Step 1: test the rule offline
GA4 evaluates "matches regex" conditions against the whole field value. The pattern chatgpt\.com alone would fail on chatgpt.com / referral-style values in some conditions, which is why the earlier pattern is wrapped in .*( ... ).*. You can check a pattern before you touch the property. This Node script mimics full-match behaviour with anchors:
const rule = /^.*(chatgpt\.com|chat\.openai\.com|gemini\.google\.com|perplexity\.ai|copilot\.microsoft\.com|claude\.ai).*$/;
const shouldMatch = [
'chatgpt.com',
'chat.openai.com',
'gemini.google.com',
'perplexity.ai',
'www.perplexity.ai',
'copilot.microsoft.com',
'claude.ai'
];
const shouldNotMatch = [
'google',
'bing',
'news.ycombinator.com',
'chatgptXcom.example',
'(direct)'
];
let failures = 0;
for (const value of shouldMatch) {
if (!rule.test(value)) { console.log('MISSED ', value); failures++; }
}
for (const value of shouldNotMatch) {
if (rule.test(value)) { console.log('FALSE HIT', value); failures++; }
}
console.log(failures === 0 ? 'All cases pass' : failures + ' case(s) failed');Run it with node test-rule.js. The escaped dots matter. Without them, chatgptXcom.example would match. Keep this file and add a row whenever you add an assistant to the rule.
Note that a pattern like this also matches a hostname that merely contains an assistant's domain, for example notclaude.ai. If that is a real risk for your traffic, anchor the pattern more tightly.
Step 2: generate a test visit
You need a visit that arrives the way a real one does. There are three ways, with different strengths.
- A real click from an assistant. Ask an assistant with web access a question that your page answers, or paste your URL and ask it to summarise the page, then click the link. This tests the whole path, including the referrer the assistant actually sends. It is the only method that does. It can be slow, and the assistant may not cite you on demand.
- A UTM-tagged link. Open
https://example.com/landing?utm_source=chatgpt.com&utm_medium=referralin a fresh browser window. This tests source and medium matching in the channel rule. It does not test referrer handling, because no referrer is present. - A test page on another host. Create a page on a different hostname that links to your site. The referrer will be that host, so it tests referrer capture, but it will not match an assistant hostname unless you add a temporary test hostname to your rule.
If you use the temporary test hostname in option 3, remove it afterwards. Otherwise your own test traffic stays in the AI channel.
Use a private window with extensions disabled for the click. Some privacy extensions and browser settings strip referrers, which gives you a false failure.
Check the referrer the browser sends
Before you look at GA4, confirm what left the browser. Open developer tools, go to the Network tab, filter by collect, and load the landing page. Select the request to google-analytics.com/g/collect and read its query parameters. The dr parameter is the document referrer GA4 received. If dr is empty, the assistant (or the browser) sent no referrer, and no GA4 configuration can recover it. That is the Direct case from the earlier chapter, not a bug in your setup.
You can also see what your own code will read:
console.log('referrer:', document.referrer);
console.log('host:', document.referrer ? new URL(document.referrer).hostname : '(none)');Run this in the console on the landing page, or on a page that loaded from the click. Compare the host with the keys in your AI_HOSTS object.
Step 3: confirm the event in DebugView
DebugView shows events from a device in debug mode, with a few seconds of delay. For the custom event, add the debug_mode parameter while you test:
gtag('event', 'ai_assistant_visit', {
assistant_name: 'ChatGPT',
debug_mode: true
});If you send the event through Google Tag Manager, use Preview mode instead. Preview enables debug mode on its own, and it also shows which tag fired and what values the variables held.
In Admin, open DebugView and select your device from the list at the top. Then check:
- The
ai_assistant_visitevent appears once for the page load, not several times. - The
assistant_nameparameter has the expected value. - A
page_viewevent appears from the same visit, withpage_locationset to your landing page.
Remove debug_mode: true before you publish. Debug traffic is excluded from standard reports, so leaving it on makes real AI visits vanish from them.
An event that fires twice usually means the snippet is both hard-coded on the page and sent from a GTM tag. Keep one.
Step 4: check Realtime, then the channel reports
The Realtime report confirms that the visit reached the property within minutes. Look at the source cards and the event count list for ai_assistant_visit. Realtime does not show your custom channel group, so do not read a missing channel there as a failure.
Channel assignment is confirmed in the standard reports, which can lag by many hours:
- Open Reports, then Acquisition, then Traffic acquisition.
- Change the primary dimension to your custom channel group using the dimension picker above the table. The default channel group is selected first, so a common mistake is looking at the wrong grouping.
- Find the row named AI Assistants. Add a secondary dimension, Session source / medium, and confirm the test visit's source appears under that row.
- If a row says Referral or Direct instead, open the session source / medium of that row and compare it with your rule.
Custom channel groups are applied when the report is built, so a rule you edit later is applied to past data in reports as well. Event-level checks in DebugView do not have that delay.
Step 5: cross-check in BigQuery
If the BigQuery export is on, confirm that the raw rows agree with the interface. The daily export table for the day of your test appears the next day. This query lists a single day's page views whose referrer came from an AI host:
SELECT
event_timestamp,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location,
(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 = '20261129'
AND REGEXP_CONTAINS(
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer'),
r'(chatgpt\.com|chat\.openai\.com|gemini\.google\.com|perplexity\.ai|copilot\.microsoft\.com|claude\.ai)'
)
ORDER BY event_timestamp;Replace the project, dataset number, and date with your own. If the interface shows one AI session and BigQuery shows none, the table may not have been exported yet, or the visit arrived with a UTM source and no referrer. The query searches page_referrer only, so a UTM-only test will not appear in it.
Common failures and their causes
| Symptom | Likely cause | What to check |
|---|---|---|
| Test visit lands in Referral | The AI Assistants rule sits below the default Referral rule | Move the custom rule above Referral in the channel group |
| Test visit lands in Direct | No referrer was sent | Look at `dr` in the Network tab; try a private window |
| Event missing from DebugView | Debug mode off, wrong stream, or an ad blocker | Check the measurement ID and disable blockers |
| Event appears twice | Snippet and GTM tag both fire | Remove one of them |
| Event in DebugView, not in reports | `debug_mode` left on, or processing delay | Remove the flag and wait |
| Perplexity appears under two names | `perplexity.ai` and `www.perplexity.ai` are different hosts | Check both keys in `AI_HOSTS` |
| Rule never matches a source value | Regex lacks the `.*` wrapper or the dots are unescaped | Rerun the offline test |
Keep a regression check
Assistants change domains. ChatGPT is reachable at both chatgpt.com and chat.openai.com, and new products appear. A setup that passes today can silently miss new traffic. Once a month:
- Open the Traffic acquisition report with Session source / medium as the dimension, filtered to Referral, and scan for hostnames that look like assistants but are not in your rule.
- Run your offline test script after any change to the regex.
- Repeat one real click from an assistant and follow it through DebugView and the channel report.
Exercise
You click a link from an AI assistant to your site, DebugView shows ai_assistant_visit, and the Network tab shows an empty dr parameter. The session lands in Direct. Is your channel group rule at fault?
Show answer
No. The empty dr value means the browser sent no referrer, so GA4 had no source to classify and filed the session as Direct. The event still fired because your custom code only ran on pages where it found a referrer, so check whether it did in fact read one (for example from a UTM parameter). A rule can only match data that arrives, so the fix for missing referrers is tagged links or accepting the gap, not a rule change.
Exercise
Why does a UTM-tagged test link not prove that your referrer-based detection works?
Show answer
A UTM link sets the session source and medium directly, so it exercises the channel group rule on source values. It carries no referrer, so it never tests whether document.referrer or the page_referrer parameter is captured and matched. Only a real click from an assistant, or a link from a test page on another host, tests that path.