Request a demo

GA4 in BigQuery · Users, sessions and traffic

Session source / medium in GA4 BigQuery

Older GA4 exports, including the public sample, carry source, medium and campaign as event parameters. Take the first non-null source and medium in each session; sessions with none are direct. Newer exports have session_traffic_source_last_click, which is closer to GA4's session attribution.

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

The query

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,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'source') AS source,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'medium') AS medium
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
),
sessions AS (
  SELECT
    user_pseudo_id,
    ga_session_id,
    ARRAY_AGG(IF(source IS NULL, NULL, STRUCT(source, medium)) IGNORE NULLS
              ORDER BY event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS first_source
  FROM events
  GROUP BY user_pseudo_id, ga_session_id
)
SELECT
  COALESCE(first_source.source, '(direct)') AS source,
  COALESCE(first_source.medium, '(none)')   AS medium,
  COUNT(*)                                  AS sessions
FROM sessions
GROUP BY source, medium
ORDER BY sessions DESC
LIMIT 20;

How it works

GA4 reports attribute each session to a source and medium. How you get that from the export depends on its age. This sample, like exports from before Google added the newer columns, stores the values collected with events as source, medium and campaign event parameters. The query takes the first non-null source and medium in each session and calls sessions without any (direct) / (none).

Current exports have two better options. session_traffic_source_last_click holds the session's last-click campaign, grouped by platform (manual_campaign, google_ads_campaign, cross_channel_campaign and others), so session_traffic_source_last_click.manual_campaign.source and .medium are ready-made per session. collected_traffic_source holds the UTM values and click IDs collected with each event, the modern version of what this query reads from event_params. Neither exists in the sample, so the SQL here sticks to event parameters.

Do not expect an exact match with the interface. Google states that GA4's session attribution model is not in the export and cannot be calculated with full accuracy from it: GA4 can, for example, carry a previous non-direct source into a direct session. The query shows what was collected, which is often what you need for campaign debugging.

The sample contains placeholder values such as <Other> and (data deleted) in source and medium. They come from obfuscation; in your own export these rows hold real values.

ColumnMeaning
source / mediumFirst source and medium collected in the session, or (direct) / (none) when none was.
sessionsSessions with that source and medium.

Notes and cost

  • Cost: source and medium sit in event_params.value.string_value, the most expensive part of the export to read, so one week costs 154.6 MiB. The date range is the main lever.
  • traffic_source (without the session prefix) is the user's first acquisition source, not the session's. Use it for acquisition reports (see traffic source), not for sessions.
  • In newer exports, collected_traffic_source.gclid marks Google Ads clicks even when UTM parameters are missing. Treat sessions with a gclid as google / cpc if your reports need that.

Variations

Add the campaign

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,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'source')   AS source,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'medium')   AS medium,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'campaign') AS campaign
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
),
sessions AS (
  SELECT
    ARRAY_AGG(IF(source IS NULL, NULL, STRUCT(source, medium, campaign)) IGNORE NULLS
              ORDER BY event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS s
  FROM events
  GROUP BY user_pseudo_id, ga_session_id
)
SELECT
  COALESCE(s.source, '(direct)')        AS source,
  COALESCE(s.medium, '(none)')          AS medium,
  COALESCE(s.campaign, '(not set)')     AS campaign,
  COUNT(*)                              AS sessions
FROM sessions
GROUP BY source, medium, campaign
ORDER BY sessions DESC
LIMIT 20;

FAQ

Where is session source/medium in the GA4 BigQuery export?

In current exports, in session_traffic_source_last_click, for example session_traffic_source_last_click.manual_campaign.source. Older exports only have source and medium event parameters, which you aggregate per session as in this query.

Why doesn't my BigQuery source/medium match GA4 reports?

GA4's session attribution model is not exported and cannot be reproduced exactly. Reports may also carry a previous campaign into a direct session, group rare rows into (other), and apply thresholds.

What is the difference between traffic_source and session source?

traffic_source is the first source that acquired the user and never changes. Session source describes how each visit started.

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.