> Site index: https://usehardal.com/llms.txt
> Every content route also serves markdown at <url>.md

# 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.

Source: https://usehardal.com/blog/ai-traffic-tracking-ga4-bigquery-warehouse
Published: 2026-11-23T14:00:00
Updated: 2026-11-23T14:00:00
Author: Hardal
Category: 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](https://support.google.com/analytics/answer/9756891?hl=en) 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:

| Field | Example | Why keep it |
| --- | --- | --- |
| `referrer_raw` | `https://chatgpt.com/` | The original signal, before any tool rewrites it |
| `landing_url_raw` | `https://example.com/pricing?utm_source=chatgpt.com` | UTMs added by assistants, plus click IDs |
| `user_agent` | the full string | Separates browsers, fetchers and crawlers |
| `agent_verified` | `true` or `false` | Result of signature or IP verification at the edge |
| `session_id`, `device_id` | your first-party IDs | Joins 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](https://developers.google.com/analytics/devguides/collection/protocol/ga4), 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.

```sql
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](https://usehardal.com/server-side-streaming-to-bigquery), writes sessions and orders with the same first-party `device_id`, the join is direct:

```sql
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](https://usehardal.com/import-hardal-first-party-data-to-bigquery), check [Hardal AI Visibility](https://usehardal.com/ai-visibility) for the bot side, or [book a demo](https://usehardal.com/contact/demo).
