Track AI Traffic in GA4 and BigQuery with Server-Side Data

Track AI traffic in GA4 and BigQuery: classify AI referrals server-side, keep raw source fields, and query AI sessions and revenue with SQL you can reuse.

Hardal
5 min read
Agentic Commerce

To track AI traffic in GA4 and BigQuery, capture the raw referrer and landing URL on the server, classify AI sources with a rule table you control, send the classification to GA4 as event parameters, and store raw fields in BigQuery. GA4's AI Assistant channel is a start, but it depends on the referrer.

Stape's recent guide covers wiring its AI detection headers into server GTM. This post focuses on the warehouse side: the table design, the classification rule, and SQL you can run against both the GA4 export and your own server-side event stream.

What GA4 does on its own

The GA4 default channel group puts a session in AI Assistant when its medium is exactly ai-assistant. GA4 sets that medium, and sets the campaign to (ai-assistant), when the referrer matches its list of AI assistants. AI Overviews and AI Mode are excluded and count as Organic Search.

That is fine for a dashboard. It is weak for analysis, for three reasons. You cannot see or edit the list of assistants. A visit that arrives without a referrer lands in Direct. And if your rule needs to change, you cannot reprocess old GA4 sessions. That is why the raw fields belong in your warehouse.

Step 1: Capture raw fields on the server

On the first request of a session, record these values before any classification:

FieldExampleWhy keep it
referrer_rawhttps://chatgpt.com/The original signal, before any tool rewrites it
landing_url_rawhttps://example.com/pricing?utm_source=chatgpt.comUTMs added by assistants, plus click IDs
user_agentthe full stringSeparates browsers, fetchers and crawlers
agent_verifiedtrue or falseResult of signature or IP verification at the edge
session_id, device_idyour first-party IDsJoins sessions to orders

A server-side collector on your own domain sees all of these. Requests from crawlers and fetchers belong in a separate bot table, not in session data.

Step 2: Classify with a rule table, not a regex in a tag

Keep AI sources in a table such as ai_sources(host, utm_source, assistant, active_from). Typical rows include chatgpt.com, perplexity.ai, gemini.google.com, copilot.microsoft.com and claude.ai. Apply the rule in this order:

  1. If the landing URL has a utm_source that matches the table, use it.
  2. Else, if the referrer host matches the table, use it.
  3. Else, leave the session unclassified for AI and use normal channel rules.

Write the result to two fields, ai_traffic (true or false) and ai_assistant (the assistant name). When a new assistant appears, add a row and rerun the rule over history in the warehouse.

Step 3: Send the classification to GA4

Pass ai_traffic and ai_assistant as event parameters on your GA4 events and register them as event-scoped custom dimensions. If you forward events to GA4 server-side through the Measurement Protocol, include the same client_id and session_id the browser uses so events join the right session.

GA4 custom channel groups are built from source, medium and campaign dimensions, not from your custom dimensions. If you want AI traffic in a channel group, build it from the referrer based source values, and use the custom dimensions in explorations and in BigQuery.

Step 4: Query the GA4 export in BigQuery

The GA4 export stores the referrer in the page_referrer event parameter and manual UTMs in collected_traffic_source. This query counts AI referred sessions by assistant and landing page from session_start events. Replace the project and dataset names with your own.

WITH starts AS (
  SELECT
    PARSE_DATE('%Y%m%d', event_date) AS day,
    user_pseudo_id,
    (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS landing_url,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer') AS referrer,
    collected_traffic_source.manual_source AS utm_source
  FROM `your-project.analytics_123456789.events_*`
  WHERE event_name = 'session_start'
    AND _TABLE_SUFFIX BETWEEN '20261101' AND '20261122'
)
SELECT
  day,
  COALESCE(s1.assistant, s2.assistant) AS assistant,
  REGEXP_EXTRACT(landing_url, r'^https?://[^/]+(/[^?#]*)') AS landing_path,
  COUNT(DISTINCT CONCAT(user_pseudo_id, CAST(session_id AS STRING))) AS sessions
FROM starts
LEFT JOIN `your-project.marketing.ai_sources` s1
  ON LOWER(starts.utm_source) = s1.utm_source
LEFT JOIN `your-project.marketing.ai_sources` s2
  ON NET.HOST(starts.referrer) = s2.host
WHERE COALESCE(s1.assistant, s2.assistant) IS NOT NULL
GROUP BY day, assistant, landing_path
ORDER BY day, sessions DESC;

Treat the result as browser-side AI traffic. It only includes sessions where the GA4 tag loaded and consent allowed collection. Run the same classification over your server-side session table and compare the two counts by day. The difference shows how much AI traffic your browser tag misses, which is a useful number on its own.

Step 5: Join AI sessions to revenue from your own events

For revenue, do not rely on the GA4 purchase event alone. Join AI sessions to backend orders in your server-side event table using your first-party IDs. If your server-side stream, for example server-side streaming to BigQuery, writes sessions and orders with the same first-party device_id, the join is direct:

SELECT
  s.ai_assistant,
  COUNT(DISTINCT s.session_id) AS ai_sessions,
  COUNT(DISTINCT o.order_id) AS orders,
  SUM(o.net_revenue) AS net_revenue
FROM `your-project.hardal.sessions` s
LEFT JOIN `your-project.hardal.orders` o
  ON o.device_id = s.device_id
 AND o.created_at BETWEEN s.started_at AND TIMESTAMP_ADD(s.started_at, INTERVAL 7 DAY)
WHERE s.ai_traffic = TRUE
GROUP BY s.ai_assistant;

The table names and the seven day window are examples. Pick a window that matches your sales cycle, and write it down next to the report.

Step 6: Carry the source into your CRM

For lead generation, store ai_assistant and the landing page on the lead at form submission, from the server-side session, not from a hidden field filled by a browser script that may not run. When the lead becomes an opportunity, the CRM can report pipeline by AI source without a second attribution system.

Common mistakes

  • Sending crawlers and fetchers to GA4, which inflates sessions and drags down conversion rates.
  • Hard coding an AI regex in a tag, so history cannot be reclassified.
  • Comparing GA4 AI sessions with warehouse AI sessions without matching consent, date range and time zone.
  • Reporting AI revenue from the browser purchase tag only.

Hardal is the first-party data and agentic analytics platform. We collect sessions, AI agent requests and orders server-side, then stream them to BigQuery with raw source fields intact. See how to import Hardal data to BigQuery, check Hardal AI Visibility for the bot side, or book a demo.

FAQs
4 Questions

Frequently Asked Questions

How to classify AI referral traffic, send it to GA4 and BigQuery, and query it reliably.

How do I see ChatGPT traffic in GA4?
Check the AI Assistant channel in the default channel group, which covers sessions whose referrer matches GA4's list of AI assistants. Add a custom channel group or an exploration on session source for assistants that are missing, and remember that visits without a referrer land in Direct.
Where is the referrer in the GA4 BigQuery export?
The page_referrer event parameter holds the referring URL on page_view events. Manual UTM values are in the collected_traffic_source record, for example collected_traffic_source.manual_source and manual_medium.
Why should AI traffic be classified server-side?
A server-side collector sees the raw referrer, the full landing URL and request headers before any tag runs. It can verify signed agents, separate bots from people, and write raw fields to your warehouse, so classification rules can change later without losing history.
Should AI crawlers be sent to GA4?
No. Crawlers and live fetchers are not visitors. Keep them out of GA4 and ad platforms, and store them in a separate table for visibility reporting.