GA4 in BigQuery · Users, sessions and traffic
Count GA4 sessions in BigQuery
Count GA4 sessions as distinct user_pseudo_id plus ga_session_id, the way GA4 does. Counting session_start events is cheaper but misses sessions that began before the date range. Compute the count over the whole period, because sessions crossing midnight appear on two days.
The query
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
COUNT(DISTINCT CONCAT(user_pseudo_id, '.', CAST(
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS STRING))) AS sessions,
COUNTIF(event_name = 'session_start') AS session_start_events,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
GROUP BY day
ORDER BY day;How it works
Google's guide to matching the interface counts sessions as unique combinations of user_pseudo_id (or user_id) and the ga_session_id parameter. The query builds that key per event and counts distinct keys per day. It also counts session_start events, a cheaper proxy, so you can see how close the two are on your data.
The two differ for two reasons. A session that started before midnight continues into the next day without a new session_start, so the next day has more sessions than session_start events. And a session_start can arrive without other events, or be lost, depending on the implementation.
Sessions are not additive across days. Summing the daily rows double counts every session that crosses midnight; Google's guide warns about exactly this. For a weekly or monthly total, run one distinct count over the whole range (first variation).
GA4 reports estimate sessions with HyperLogLog++ at precision 12; Google quotes about ±1.63% at a 95% confidence interval. Exact counts from BigQuery will differ slightly (see GA4 vs BigQuery differences).
| Column | Meaning |
|---|---|
sessions | Distinct user_pseudo_id + ga_session_id with an event that day. |
session_start_events | session_start events that day. Cheaper, and usually a little lower. |
users | Distinct users that day, for sessions per user. |
Notes and cost
- Cost: the session key needs
event_params.keyandint_value: 104.3 MiB for one week. Counting onlysession_startevents readsevent_nameand costs 9.8 MiB (second variation). Pick the precise count for reports, the cheap one for trend checks. - If you need sessions in many reports, build a session table once a day (see daily summary table) and query that instead of the raw events.
- Events without
ga_session_idproduce a NULL key, whichCOUNT(DISTINCT)ignores.
Variations
Sessions for the whole period
One distinct count across the week. Lower than the sum of the daily rows.
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 `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207';Cheap trend: session_start events per day
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
COUNTIF(event_name = 'session_start') AS session_starts
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
GROUP BY day
ORDER BY day;FAQ
How do I count sessions in GA4 BigQuery?
COUNT(DISTINCT CONCAT(user_pseudo_id, CAST(ga_session_id AS STRING))), where ga_session_id comes from event_params. ga_session_id alone is not unique across users.
Why are my BigQuery sessions different from GA4?
GA4 estimates sessions with HyperLogLog++, may apply thresholds and modeling, and reports in the property time zone. Summing daily session counts in BigQuery also double counts sessions that cross midnight.
Can I count sessions with session_start?
Yes, as a cheap approximation: it reads only event_name. It undercounts sessions that started before your date range and depends on the event being collected.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
