GA4 in BigQuery · Working with the export
Build GA4 sessions in BigQuery from ga_session_id
A GA4 session is identified by user_pseudo_id plus the ga_session_id event parameter. ga_session_id is the session start time in seconds, so it is not unique on its own. Group events by both to get one row per session with its start, length, events and page views.
The query
WITH events AS (
SELECT
user_pseudo_id,
event_timestamp,
event_name,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_number') AS ga_session_number
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
)
SELECT
CONCAT(user_pseudo_id, '.', CAST(ga_session_id AS STRING)) AS session_key,
user_pseudo_id,
ga_session_id,
MAX(ga_session_number) AS ga_session_number,
TIMESTAMP_MICROS(MIN(event_timestamp)) AS session_start,
TIMESTAMP_MICROS(MAX(event_timestamp)) AS last_event,
COUNT(*) AS events,
COUNTIF(event_name = 'page_view') AS page_views
FROM events
GROUP BY user_pseudo_id, ga_session_id;How it works
GA4 does not export a session table. Each event carries two integer parameters instead: ga_session_id and ga_session_number. Google documents the session ID as the timestamp in seconds when the session started, so two users who start a session in the same second share a ga_session_id. The session key is therefore the pair user_pseudo_id + ga_session_id, which the query concatenates into session_key. ga_session_number counts the user's sessions: 1 is the first.
Grouping by the pair turns events into sessions: first and last event time, event count, page views. This is the base for most other recipes: sessions, landing pages, engagement rate and session source / medium all start from it.
Sessions can cross midnight. The events after midnight sit in the next daily table, so a session near the end of your range is cut off, and a session that started the day before your range appears with a late session_start. If precise session boundaries matter, read one extra day on each side and then keep only sessions whose first event falls inside the range.
In the sample, every event carries ga_session_id and ga_session_number (4,295,584 of 4,295,584). In your own export, check for NULLs: events sent without a session, such as some Measurement Protocol hits, have none and would collapse into one fake session per user.
| Column | Meaning |
|---|---|
session_key | user_pseudo_id and ga_session_id joined with a dot. Unique per session. |
ga_session_number | Which session of this user it is: 1 for the first. |
session_start / last_event | First and last event time in UTC. Their difference is wall-clock length, which is not GA4 engagement time. |
page_views | page_view events in the session. |
Notes and cost
- Cost: reading
ga_session_idforces a scan ofevent_params.keyandint_valuefor every table in range. This week costs 103.5 MiB. If you build sessions every day, materialize them once (see daily summary table) instead of rebuilding them in every report. CONCATwith a NULL argument returns NULL, so events withoutga_session_idget a NULLsession_keyinstead of a wrong one. Filter them out or count them separately.- Use
user_idinstead ofuser_pseudo_idin the key only if every event has it. Mixing both splits one session into two when the user logs in halfway.
Variations
Sessions in a date range, including the ones that cross midnight
Reads one extra day on each side, then keeps sessions that started inside the range.
WITH events AS (
SELECT
user_pseudo_id,
event_timestamp,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201130' AND '20201208'
),
sessions AS (
SELECT
user_pseudo_id,
ga_session_id,
TIMESTAMP_MICROS(MIN(event_timestamp)) AS session_start,
TIMESTAMP_MICROS(MAX(event_timestamp)) AS last_event
FROM events
GROUP BY user_pseudo_id, ga_session_id
)
SELECT *
FROM sessions
WHERE DATE(session_start) BETWEEN '2020-12-01' AND '2020-12-07';FAQ
Is ga_session_id unique in GA4?
No. It is the session start time in seconds, so different users can share it. Combine it with user_pseudo_id (or user_id) to identify a session.
Where is the session ID in the GA4 BigQuery export?
In event_params, under the key ga_session_id, in value.int_value. Read it with (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id').
How do I count sessions from the session_start event?
COUNTIF(event_name = 'session_start') is cheaper because it skips event_params, but it misses sessions whose start fell outside your date range. Distinct session keys are the more reliable count (see sessions).
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
