How to count sessions and source / medium from the GA4 BigQuery export
By Motabar Javaid · · 3 min read
Short answer: The GA4 BigQuery export has no sessions table. You build sessions yourself by combining user_pseudo_id with the ga_session_id event parameter. For a session's source and medium, use the collected_traffic_source fields on the session's first event, or the session_traffic_source_last_click record if your export includes it. Don't use the top level traffic_source field: it's the user's first ever source, not the session's.
Below is the SQL I start from on most client projects. Replace the project and dataset with your own.
How GA4 sessions work in BigQuery
Every event row has:
user_pseudo_id: the anonymous browser ID- An event parameter called
ga_session_id: the session's start timestamp
A session is the combination of the two. ga_session_id alone isn't unique, because two people can start a session in the same second.
Count sessions
SELECT
COUNT(DISTINCT CONCAT(
user_pseudo_id,
CAST((SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS STRING)
)) AS sessions
FROM `your-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20261001' AND '20261031';
The _TABLE_SUFFIX filter matters. Without it, BigQuery scans every day you've ever exported, and you pay for it.
Session source and medium
Here's a session table with source, medium and campaign taken from the first event in each session that carries campaign data:
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,
event_name,
collected_traffic_source.manual_source AS source,
collected_traffic_source.manual_medium AS medium,
collected_traffic_source.manual_campaign_name AS campaign,
collected_traffic_source.gclid AS gclid
FROM `your-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20261001' AND '20261031'
),
sessions AS (
SELECT
session_key,
MIN(event_timestamp) AS session_start,
ARRAY_AGG(
IF(source IS NOT NULL OR gclid IS NOT NULL, STRUCT(source, medium, campaign, gclid), NULL)
IGNORE NULLS ORDER BY event_timestamp LIMIT 1
)[SAFE_OFFSET(0)] AS first_touch,
COUNTIF(event_name = 'purchase') AS purchases
FROM events
GROUP BY session_key
)
SELECT
CASE
WHEN first_touch.gclid IS NOT NULL THEN 'google / cpc'
WHEN first_touch.source IS NULL THEN '(direct) / (none)'
ELSE CONCAT(first_touch.source, ' / ', IFNULL(first_touch.medium, '(none)'))
END AS source_medium,
COUNT(*) AS sessions,
SUM(purchases) AS purchases
FROM sessions
GROUP BY source_medium
ORDER BY sessions DESC;
Notes:
- Clicks with a
gclidand no UTMs are Google Ads auto tagging. The query labels themgoogle / cpc. - Sessions with no campaign data and no gclid come out as direct. GA4's interface applies extra logic here, like carrying over a previous non direct source, so your direct numbers will be higher in BigQuery than in the interface.
- Newer exports include a
session_traffic_source_last_clickrecord that mirrors how GA4 itself attributes the session. If your tables have it, it's the closest match to the interface.
The traffic_source mistake
Every event has a top level traffic_source record, and it looks like exactly what you want. It isn't. It holds the source that first acquired the user, and it never changes. Grouping sessions by it credits every visit a returning customer makes to whatever brought them in the first time. I see this mistake in most BigQuery dashboards I audit.
Why it won't match the GA4 interface exactly
- GA4's interface estimates users and sessions statistically, and BigQuery counts exactly.
- The interface includes modelled data from Consent Mode, and BigQuery doesn't.
- The interface uses its own attribution rules for direct traffic.
A few percent of difference is normal. My guide on why GA4 numbers differ covers this in more depth.
Make it reusable
Running this query against raw tables every time is slow and costly. Build it once as a daily session table, either with scheduled queries or a tool like dbt, then point dashboards at that. My guide on whether you need dbt for marketing analytics helps you decide.
If you're not set up yet, start with connecting GA4 to BigQuery. My open source GA4 BigQuery audit toolkit has more queries like these, and if you want it built for you, that's what my marketing analytics consulting does.
Frequently asked questions
How do I count sessions in the GA4 BigQuery export?
Count distinct combinations of user_pseudo_id and the ga_session_id event parameter. ga_session_id on its own isn't unique.
What is traffic_source in the GA4 BigQuery export?
It's the source, medium and campaign that first acquired the user. It never changes for that user, so don't use it for session level attribution.
Why are my BigQuery session counts different from GA4?
GA4's interface uses statistical estimates and includes modelled consent data, and it applies its own rules to direct traffic. Small differences are expected.