Sub topic 16
Analyzing acquisition in BigQuery
This chapter assumes you have used the User acquisition and Traffic acquisition reports. You should know the difference between first-user and session scope, and you should be able to read basic SQL. Here you rebuild those reports from raw event data in BigQuery. This lets you answer questions the standard interface can't.
What the export contains
When you link a GA4 property to BigQuery, Google writes one row per event into a dataset named analytics_<property_id>. Each day gets its own table, events_YYYYMMDD. A streaming export can add events_intraday_YYYYMMDD tables for the current day.
The export has no ready-made "Traffic acquisition" table. You assemble acquisition data from three places in each event row:
| Where it lives | Scope | What it holds |
|---|---|---|
| `traffic_source.source`, `.medium`, `.name` | User (first touch) | The source, medium, and campaign that first acquired this user |
| `collected_traffic_source` | Event | The raw UTM values and click IDs (such as `gclid`) collected with the event |
| `session_traffic_source_last_click` | Session | The attributed session source, medium, campaign, and default channel group |
Newer exports include the session-scoped record. Exports from before mid-2024 do not have it, so check your schema before you rely on it. Where it is missing, you must rebuild session attribution from event parameters and page_referrer.
User-scoped acquisition
The traffic_source record is set once, when GA4 first sees a user, and it doesn't change afterward. That makes it the right source for first-user questions.
SELECT
traffic_source.source AS first_user_source,
traffic_source.medium AS first_user_medium,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
GROUP BY first_user_source, first_user_medium
ORDER BY users DESC
LIMIT 20;Three details matter here.
user_pseudo_idis the identifier from the browser or app instance. One person on two devices counts as two users.- The
_TABLE_SUFFIXfilter limits which daily tables BigQuery scans. Without it you pay to read every day in the dataset. TheBETWEENon date strings also skips the intraday tables, because their suffix starts withintraday_. - A user who first arrived before you linked BigQuery appears with the source of their first event in the export, or with no source. The export has no history from before the link.
Session-scoped acquisition
GA4 has no session_id column. A session is the pair of user_pseudo_id and the ga_session_id event parameter, which holds the session's start time in seconds. Concatenate the two to get a session key, then group events by that key.
Event parameters are stored as a repeated record, so you read them with a subquery over UNNEST(event_params).
WITH events AS (
SELECT
CONCAT(
user_pseudo_id, '.',
CAST((SELECT value.int_value FROM UNNEST(event_params)
WHERE key = 'ga_session_id') AS STRING)
) AS session_key,
event_timestamp,
session_traffic_source_last_click.cross_channel_campaign.default_channel_group AS channel_group,
session_traffic_source_last_click.cross_channel_campaign.source AS source,
session_traffic_source_last_click.cross_channel_campaign.medium AS medium,
IF((SELECT value.string_value FROM UNNEST(event_params)
WHERE key = 'session_engaged') = '1', 1, 0) AS engaged_flag
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
),
sessions AS (
SELECT
session_key,
ARRAY_AGG(channel_group IGNORE NULLS ORDER BY event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS channel_group,
ARRAY_AGG(source IGNORE NULLS ORDER BY event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS source,
ARRAY_AGG(medium IGNORE NULLS ORDER BY event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS medium,
MAX(engaged_flag) AS engaged
FROM events
WHERE session_key IS NOT NULL
GROUP BY session_key
)
SELECT
channel_group,
CONCAT(source, ' / ', medium) AS session_source_medium,
COUNT(*) AS sessions,
SUM(engaged) AS engaged_sessions,
ROUND(SAFE_DIVIDE(SUM(engaged), COUNT(*)), 3) AS engagement_rate
FROM sessions
GROUP BY channel_group, session_source_medium
ORDER BY sessions DESC
LIMIT 20;This query mirrors the Data API request from the earlier chapter: sessionDefaultChannelGroup, sessionSourceMedium, sessions, and engagedSessions. The CONCAT returns NULL when either part is NULL, so sessions with no source drop out of the session_source_medium label. Check for NULL rows if your totals look low.
The session_key IS NOT NULL filter removes events that carry no ga_session_id. These are usually events from outside a normal session, such as some server-side or measurement-protocol hits.
Why BigQuery numbers differ from the reports
Expect small gaps between your queries and the GA4 interface. The usual causes:
- Approximation. The interface estimates distinct user counts with an approximate algorithm.
COUNT(DISTINCT ...)in BigQuery is exact. - Modeled data. If consent mode is on, the interface can include modeled sessions and users. The export holds only observed events.
- Time zones.
event_dateis in the property's reporting time zone, whileevent_timestampis in UTC microseconds. If you cut by timestamp, shift to the property time zone before you compare days. - Late data. Events can arrive after a daily table is first written, and the table gets updated. Re-run recent days before you trust them.
- Export limits. Standard properties have a daily cap on exported events. Days over the cap can have missing data.
- Attribution model. The reports apply the property's attribution settings. Your own grouping applies only what you code.
Rebuilding a default channel group
The default_channel_group field in the session record gives you GA4's own classification. If your export lacks it, or you want custom groups, classify rows yourself with CASE. This version covers a few common rules and is not the full definition.
SELECT
CASE
WHEN source IS NULL AND medium IS NULL THEN 'Direct'
WHEN medium = 'email' THEN 'Email'
WHEN medium IN ('cpc', 'ppc', 'paidsearch') THEN 'Paid Search'
WHEN medium = 'organic' THEN 'Organic Search'
WHEN medium = 'referral' THEN 'Referral'
ELSE 'Unassigned'
END AS my_channel_group,
COUNT(*) AS sessions
FROM (
SELECT
session_traffic_source_last_click.manual_campaign.source AS source,
session_traffic_source_last_click.manual_campaign.medium AS medium
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
AND event_name = 'session_start'
)
GROUP BY my_channel_group
ORDER BY sessions DESC;This groups only sessions whose UTM tags GA4 recorded, because manual_campaign holds tagged traffic. Untagged organic search and referrals come from other records or from page_referrer. The result is a custom rule set, so treat it like a custom channel group in the interface and document it.
Common errors
- Mixing scopes. Joining
traffic_source(first user) to session counts gives a result that matches neither report. Pick one scope per query. - Counting events as sessions.
COUNT(*)on the events table counts events. Count distinct session keys. - Forgetting
UNNEST. Readingevent_params.keydirectly fails. Always unwrap it with a subquery. - Wide date ranges. A year-long scan against
events_*can cost far more than the question deserves. Test on one day first. - Reading
(not set)as an error. In the export it can mean the field was not collected for that event, not that your tagging is broken.
Exercise
A colleague counts sessions in BigQuery with SELECT COUNT(*) FROM events_* filtered to January and reports a number three times larger than the Traffic acquisition report. Name the mistake and write the corrected counting expression.
Show answer
COUNT(*) counts event rows, not sessions. A session contains many events. Count distinct session keys instead: COUNT(DISTINCT CONCAT(user_pseudo_id, '.', CAST((SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS STRING))). Small remaining differences from the report can come from consent-mode modeling and time zones.
Exercise
Why does the user-scoped query use traffic_source while the session-scoped query uses session_traffic_source_last_click, and what happens if you swap them?
Show answer
traffic_source stores the first-touch source for the user and never changes, so it answers "how did this person first arrive". session_traffic_source_last_click stores the source attributed to each session. If you swap them, the user query would show session sources and count users in several channels, and the session query would assign every session to the user's first source. Neither would match its report.