GA4 in BigQuery · Working with the export
Unnest GA4 event_params in BigQuery
Read a GA4 event parameter with a scalar subquery over UNNEST(event_params): (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location'). It keeps one row per event, returns NULL when the parameter is missing and lets you pull several parameters side by side.
The query
SELECT
event_date,
event_name,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location,
(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 = 'engagement_time_msec') AS engagement_time_msec,
-- `value` arrives as a double on some events and as an integer on others
(SELECT COALESCE(value.double_value, CAST(value.int_value AS FLOAT64))
FROM UNNEST(event_params) WHERE key = 'value') AS value
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
AND event_name IN ('page_view', 'purchase');How it works
event_params is an array of STRUCT<key STRING, value STRUCT<string_value, int_value, float_value, double_value>>. Each event has one array element per parameter. A scalar subquery, (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id'), looks inside the array of the current row and returns that parameter's value, or NULL if the event does not carry it. Because the subquery runs per row, the result keeps exactly one row per event, and you can extract as many parameters as you need next to each other.
The alternative, FROM events_*, UNNEST(event_params) AS ep, produces one row per parameter. That is the right shape for an inventory of keys (see export schema), but for reports it multiplies rows, and a WHERE ep.key = 'page_location' filter drops every event that lacks the parameter.
Pick the value slot that matches how the parameter is stored. Text parameters such as page_location, source and medium live in string_value; IDs and counters such as ga_session_id, ga_session_number and engagement_time_msec in int_value. Some parameters change type between events: in this sample value is a double on 3,410 events and an integer on 1,832, and session_engaged is mostly the string '1' but an integer on 313,391 events. For those, COALESCE across the slots, as the query does for value.
Cost: BigQuery stores event_params.key and each value slot as separate columns, and bills each one you touch in full for every table scanned, whatever keys you filter on. Over the whole sample, key alone is 658.6 MiB, key plus int_value 811.0 MiB, key plus string_value 1.17 GiB. Extracting a fourth parameter from a slot you already read costs nothing extra, so the levers are the date range and avoiding string_value when you only need integers. This query reads one week: 152.3 MiB.
| Column | Meaning |
|---|---|
page_location | Full page URL from the page_location string parameter. |
ga_session_id | Session ID from the integer slot. Unique only together with user_pseudo_id (see session ID). |
engagement_time_msec | Engagement time the event reports, in milliseconds. NULL on events that do not carry it. |
value | Event value, read from the double slot and falling back to the integer slot. |
Notes and cost
- If a key can appear twice in one event's array, the scalar subquery fails with scalar subquery produced more than one element. Add
LIMIT 1inside the subquery, or aggregate withMAX(value.string_value). - Filtering on
event_namedoes not reduce bytes: BigQuery still reads the wholeevent_paramscolumn for every scanned table. Only_TABLE_SUFFIXand the choice of columns change the bill. - Do not compare
value.string_value = '1'when the parameter is an integer. Check the type first with the inventory query, or normalize withCOALESCE(value.string_value, CAST(value.int_value AS STRING)). - Event parameters you register as custom dimensions in GA4 appear here under the parameter name you send, not the display name of the dimension.
Variations
Reusable helper functions
Temporary SQL functions with ANY TYPE keep long queries readable. They work in any query that uses the export, and LIMIT 1 protects against duplicate keys.
CREATE TEMP FUNCTION param_str(params ANY TYPE, k STRING) AS (
(SELECT value.string_value FROM UNNEST(params) WHERE key = k LIMIT 1)
);
CREATE TEMP FUNCTION param_int(params ANY TYPE, k STRING) AS (
(SELECT value.int_value FROM UNNEST(params) WHERE key = k LIMIT 1)
);
SELECT
event_name,
param_str(event_params, 'page_location') AS page_location,
param_int(event_params, 'ga_session_id') AS ga_session_id
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207';Any parameter as a string, whatever its type
Useful for exploration and for parameters whose type changes between events.
SELECT
event_name,
(SELECT COALESCE(value.string_value,
CAST(value.int_value AS STRING),
CAST(value.double_value AS STRING))
FROM UNNEST(event_params) WHERE key = 'session_engaged') AS session_engaged
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX = '20201201';One row per parameter, for a few keys
CROSS JOIN UNNEST with a key filter. Events without these keys disappear, which is fine for counting values and wrong for per-event reports.
SELECT
ep.key,
ep.value.string_value AS value,
COUNT(*) AS events
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(event_params) AS ep
WHERE _TABLE_SUFFIX = '20201201'
AND ep.key IN ('source', 'medium')
GROUP BY ep.key, value
ORDER BY ep.key, events DESC;FAQ
How do I get a single event parameter in GA4 BigQuery?
Use a scalar subquery: (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location. Use value.int_value for integer parameters such as ga_session_id.
Why does my event parameter come back NULL?
Either the event does not carry it, or you read the wrong slot. A number sent as a string sits in string_value, and some parameters use different slots on different events. Check with the inventory in the export schema recipe.
Is CROSS JOIN UNNEST(event_params) more expensive than a subquery?
The bytes are the same: both read the event_params columns you reference. The difference is the row count. The cross join produces one row per parameter, which makes later joins and aggregations heavier and easy to double count.
Can I unnest items the same way?
Yes. items is also a repeated record, but you usually want one row per item, so FROM events_*, UNNEST(items) AS item is the normal pattern there (see items revenue).
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
