Sub topic 14
Querying Events with `UNNEST` on `event_params`
Assume you know the GA4 interface, events, event parameters, and basic SQL. This chapter uses BigQuery Standard SQL.
The GA4 BigQuery export stores one event per row. Fixed event fields, such as event_name, event_date, and user_pseudo_id, appear as regular columns. Custom and built-in event parameters appear in event_params, which is a repeated field: an array of key-value records.
Because event_params contains multiple records in each event row, query it with UNNEST. UNNEST turns the array into rows that SQL can inspect.
Extracting one parameter
A scalar subquery is a compact way to extract one parameter while keeping one result row per event:
SELECT
event_date,
event_name,
user_pseudo_id,
(SELECT value.string_value
FROM UNNEST(event_params)
WHERE key = 'page_location') AS page_location
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20250101' AND '20250131'
AND event_name = 'page_view'
LIMIT 100;The inner query searches the current event's event_params array. The key column identifies the parameter. Each parameter value has typed fields, including string_value, int_value, float_value, and double_value. Read the field that matches the parameter's stored type.
The outer query uses wildcard tables. The * matches daily tables such as events_20250101. _TABLE_SUFFIX limits the tables scanned. This is both clearer and usually less expensive than querying every available daily table.
Counting sessions
GA4 does not export a single session ID column. A common session key combines user_pseudo_id with the ga_session_id parameter:
SELECT
event_date,
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 `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20250101' AND '20250131'
GROUP BY event_date
ORDER BY event_date;The parameter is an integer, so this query reads value.int_value and casts it to text before combining it with the user identifier. If a parameter is missing, the subquery returns NULL. That can produce an incomplete key, so inspect missing values before relying on a metric.
| Parameter type | Field to read |
|---|---|
| Text | `value.string_value` |
| Whole number | `value.int_value` |
| Decimal number | `value.double_value` or `value.float_value` |
Common errors
- Filtering on
event_params.keywithout usingUNNEST. - Reading
string_valuewhen the parameter usesint_value. - Omitting
_TABLE_SUFFIXand scanning unnecessary daily tables. - Treating a missing parameter as zero.
- Using
COUNT(*)after an unrestrictedUNNEST, which can count parameter records instead of events.
Exercise
Which expression extracts the text value of the page_location parameter from the current event?
Show answer
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location')