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.
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.
| Column | Meaning |
|---|---|
source / medium / campaign | First-touch source, medium and campaign name of the user. |
users | Distinct users active in the range with that first touch. |
Notes and cost
- Cost: STRUCT subfields are stored as separate columns, so reading three
traffic_sourcefields anduser_pseudo_idfor a month costs 80.7 MiB, without touchingevent_params. The wholetraffic_sourcerecord 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.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
