Request a demo

GA4 in BigQuery · Users, sessions and traffic

First user source, medium and campaign in GA4 BigQuery

The traffic_source record in the GA4 export is first-touch acquisition: the source, medium and campaign that first brought the user, the same on every later event. Count distinct users by traffic_source.source and medium; it reads only those subfields, so it is cheap.

Runs on Google's public sample bigquery-public-data.ga4_obfuscated_sample_ecommerce · Scans 80.7 MiB, under $0.01 on-demand · Checked in November 2026

The query

SELECT
  traffic_source.source,
  traffic_source.medium,
  traffic_source.name              AS campaign,
  COUNT(DISTINCT user_pseudo_id)   AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY source, medium, campaign
ORDER BY users DESC
LIMIT 20;

How it works

traffic_source has three fields: source, medium and name (the campaign). Google's schema describes them as the network, medium and campaign that first acquired the user, and they do not change with later campaign interactions. That makes traffic_source the export's version of GA4's First user source, First user medium and First user campaign dimensions.

Use it for acquisition questions: which channels bring new customers, what lifetime revenue a channel's users produce. Do not use it for session or event attribution. A user acquired by organic search who later clicks an email still shows google / organic on every event. For session-level attribution, see session source / medium.

The query counts all users active in December by their first source, including users acquired months earlier. To count only users acquired in the period, restrict to first_visit events (first variation).

traffic_source is not populated in intraday tables; read it from the daily tables.

ColumnMeaning
source / medium / campaignFirst-touch source, medium and campaign name of the user.
usersDistinct users active in the range with that first touch.

Notes and cost

  • Cost: STRUCT subfields are stored as separate columns, so reading three traffic_source fields and user_pseudo_id for a month costs 80.7 MiB, without touching event_params. The whole traffic_source record across the sample is 129.7 MiB.
  • Because the values repeat on every event of a user, you can also read them from one event per user (such as first_visit) without changing the answer.

Variations

New users by first source

Only users whose first_visit happened in the range.

SELECT
  traffic_source.source,
  traffic_source.medium,
  COUNT(DISTINCT user_pseudo_id) AS new_users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
  AND event_name = 'first_visit'
GROUP BY source, medium
ORDER BY new_users DESC
LIMIT 20;

Revenue by first user medium

SELECT
  traffic_source.medium,
  COUNT(DISTINCT user_pseudo_id)                         AS purchasers,
  ROUND(SUM(ecommerce.purchase_revenue_in_usd), 2)       AS revenue_usd
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
  AND event_name = 'purchase'
GROUP BY medium
ORDER BY revenue_usd DESC;

FAQ

Is traffic_source in GA4 BigQuery session or user level?

User level, first touch. It records what first acquired the user and stays the same on all of their later events.

Why is traffic_source empty in my intraday table?

Google does not populate traffic_source in intraday tables. It appears in the daily events_YYYYMMDD table.

All GA4 export queries · BigQuery cost queries · The GA4 sample dataset

Cut BigQuery spend without guessing

Masthead attributes compute and storage cost to every project, user, pipeline and table, finds unused and dead-end tables, and shows where editions or on-demand pricing is cheaper. Mnemonic cut its BigQuery compute cost by 20% within 24 hours of onboarding.