Sub topic 10
Analysing AI Referrals in the BigQuery Export
Assume that you know the GA4 interface, referrers, and UTM parameters. You also need access to a GA4 property linked to BigQuery and basic SQL knowledge. This chapter uses the GA4 daily export, where each row represents an event rather than a session.
Why use the BigQuery export?
GA4 reports are useful for exploring traffic, but they can hide details while applying attribution rules and reporting thresholds. The BigQuery export gives you event-level data that you can query repeatedly.
For a page view, the referring URL is usually stored in the event_params array under the key page_referrer. The event date is stored as event_date in YYYYMMDD format. The wildcard table events_* represents multiple daily export tables.
The export records visits only when your site receives and measures them. It does not record an AI assistant's private recommendation, an answer that contains no clicked link, or a crawler that never loads your GA4 tag.
Extracting AI referrers
The following query finds page views whose referrer contains a known assistant host. Replace the project, dataset, and property ID with your values. The date range in _TABLE_SUFFIX must match the daily tables you want to scan.
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;The inner query uses UNNEST to turn the repeated event_params array into searchable rows. The scalar subquery selects the string value for page_referrer. The outer query extracts the host and counts matching page views by day.
_TABLE_SUFFIX limits the scan to the selected daily tables. This reduces query cost and prevents older data from changing the result unexpectedly. Add new assistant domains as you find them in your own data.
> callout note > Do not treat the host list as universal. Assistant domains, redirect services, regional domains, and link wrappers can change. First inspect unclassified referrers, then update your matching rules.
Comparing AI traffic with UTM data
A referrer is not the only signal. Some links may include parameters such as utm_source=chatgpt or utm_medium=ai. GA4 stores these values in traffic-source fields, whose exact availability depends on the export schema and property configuration.
Compare the referrer and UTM values instead of assuming they agree. A link can have an AI UTM value but no browser referrer. Conversely, a browser referrer can identify an assistant even when no UTM parameters exist.
For reliable analysis, create a normalized assistant name. Map chat.openai.com and chatgpt.com to ChatGPT, for example. Keep the original host too, so you can audit the mapping later.
Common errors
- Counting events instead of sessions. A single session can generate many page views.
- Scanning every
events_*table. Always restrict_TABLE_SUFFIX. - Matching only exact URLs. Redirects and subdomains can alter the referrer.
- Counting crawler activity as human traffic. Crawlers often do not execute your GA4 tag.
- Assuming missing referrers mean missing interest. Privacy settings, app browsers, redirects, and security policies can remove referrer data.
To estimate sessions, use a session identifier such as ga_session_id from the event parameters and combine it with the user or pseudonymous identifier. Be explicit about the session definition before comparing BigQuery results with GA4 reports, because attribution and identity settings may produce different totals.
Exercise
Exercise
Why does the query extract pagereferrer from eventparams instead of selecting it as a normal column?
Show answer
GA4 stores many event-specific values, including pagereferrer, in the repeated eventparams array. The query must use UNNEST and select the parameter's string_value before it can filter or group the referrer.