Request a demo

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.

Runs on Google's public sample bigquery-public-data.ga4_obfuscated_sample_ecommerce · Scans 103 MiB, under $0.01 on-demand · Checked in November 2026

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.

ColumnMeaning
session_keyuser_pseudo_id and ga_session_id joined with a dot. Unique per session.
ga_session_numberWhich session of this user it is: 1 for the first.
session_start / last_eventFirst and last event time in UTC. Their difference is wall-clock length, which is not GA4 engagement time.
page_viewspage_view events in the session.

Notes and cost

  • Cost: reading ga_session_id forces a scan of event_params.key and int_value for 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.
  • CONCAT with a NULL argument returns NULL, so events without ga_session_id get a NULL session_key instead of a wrong one. Filter them out or count them separately.
  • Use user_id instead of user_pseudo_id in 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).

All GA4 export queries · BigQuery cost queries · The GA4 sample dataset

Cut BigQuery spend without guessing

Masthead attributes compute and storage cost to every project, user, pipeline and table, finds unused and dead-end tables, and shows where editions or on-demand pricing is cheaper. Mnemonic cut its BigQuery compute cost by 20% within 24 hours of onboarding.