Sub topic 14
Querying events with UNNEST on event_params
This chapter shows how to read event parameters from the GA4 BigQuery export. It assumes you know basic SQL (SELECT, WHERE, GROUP BY), understand GA4 events and event parameters, and have a BigQuery link that is already delivering events_YYYYMMDD tables. Dataset names below use my-project.analytics_123456789. Replace them with your own project and GA4 property ID.
Why event_params needs UNNEST
In the export, each row is one event. Fields such as event_name, event_date, and user_pseudo_id are ordinary columns. Event parameters are different. They live in a single column named event_params, which is a repeated record (an array of structs). Each element has a key and a value. The value is itself a struct with four typed fields:
| Field | Holds |
|---|---|
| `string_value` | Text, such as a page URL |
| `int_value` | Whole numbers, such as `ga_session_id` |
| `float_value` | Single-precision decimals (rarely populated) |
| `double_value` | Decimals, such as a custom `price` parameter |
A parameter is stored in exactly one of these fields. The others are NULL. If you read the wrong one, you get NULL and no error. This is the most common mistake in GA4 queries.
You cannot write event_params.page_location. You first have to open the array and pick the element whose key matches. UNNEST turns an array into rows so that you can filter them.
The scalar subquery pattern
The simplest approach is a scalar subquery in the SELECT list. It reads one parameter for each event row and does not change the number of rows.
SELECT
event_name,
(SELECT value.string_value
FROM UNNEST(event_params)
WHERE key = 'page_location') AS page_location
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX = '20250115'
LIMIT 10;For each event, the subquery unnests that event's own event_params and keeps the row where key = 'page_location'. The result is a single value, or NULL if the event has no such parameter. Because the subquery returns at most one row, BigQuery accepts it as a scalar. If a key could appear twice in the same event, the query would fail with an error about a scalar subquery producing more than one element. GA4 normally sends each key once per event.
The unnesting is correlated to the current row. It does not multiply rows, so a later COUNT(*) still counts events.
Ranking pages by views
You can use the same subquery in GROUP BY through its alias. This query ranks the most viewed pages for a month.
SELECT
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location,
COUNT(*) AS page_views
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20250101' AND '20250131'
AND event_name = 'page_view'
GROUP BY page_location
ORDER BY page_views DESC
LIMIT 20;Two details matter here:
page_locationholds the full URL, including query strings. Two visits to the same page with differentutm_orgclidvalues count as different locations. To group by path, wrap the value in a function such asREGEXP_EXTRACT(url, r'^https?://[^/]+([^?#]*)').- The filter on
event_nameis applied before grouping, so onlypage_viewrows are counted.
Counting sessions
The export has no session table and no session ID that is unique across users. GA4 stores a session identifier in the ga_session_id event parameter. It is a timestamp-like integer, so it is stored in int_value. Two different users can share the same value. A session is therefore identified by combining user_pseudo_id with ga_session_id.
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 subquery reads int_value, and CAST ... AS STRING lets CONCAT join it with the user ID. If an event has no ga_session_id, the cast gives NULL, and CONCAT with a NULL argument returns NULL. COUNT(DISTINCT ...) ignores NULL, so those events do not add a session.
A session that crosses midnight appears under two event_date values. It is counted once on each day. As a result, the sum of daily sessions can be slightly larger than the count of distinct sessions for the whole month.
Limiting data with _TABLE_SUFFIX
The pattern events_* is a wildcard table. It covers every table whose name starts with events_, including the events_intraday_ tables if streaming export is on. The pseudo-column _TABLE_SUFFIX holds the part of the table name matched by the *. Filtering on it limits which tables BigQuery reads, so it reduces the bytes scanned and the cost.
Keep these points in mind:
- A filter such as
_TABLE_SUFFIX BETWEEN '20250101' AND '20250131'compares strings. It works because the date format sorts correctly as text. - Filtering on
event_datealone does not prune tables. BigQuery still scans every matching table. - Intraday tables have suffixes that begin with
intraday_. These sort after the date suffixes, so a boundedBETWEENon dates excludes them. An open range such as>= '20250101'includes them, and you may count the same events twice if the daily table for that day also exists. - Selecting fewer columns also lowers cost. BigQuery bills for the columns it reads, and
SELECT *reads all of them, includingevent_params.
Reading several parameters
When you need many parameters, repeating the subquery gets long. A common step is to move the extraction into a common table expression (CTE), so the rest of the query uses plain columns.
WITH events AS (
SELECT
event_date,
event_name,
user_pseudo_id,
(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
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20250101' AND '20250131'
)
SELECT
page_location,
COUNT(DISTINCT CONCAT(user_pseudo_id, '-', CAST(ga_session_id AS STRING))) AS sessions,
SUM(engagement_time_msec) / 1000 AS engaged_seconds
FROM events
WHERE page_location IS NOT NULL
GROUP BY page_location
ORDER BY engaged_seconds DESC
LIMIT 20;BigQuery reads the underlying columns only once, so the CTE does not add cost. It only makes the query easier to read.
Handling mixed value types
Some parameters are stored in different fields depending on how they were sent. A custom parameter sent as the number 5 may arrive in int_value, while 5.5 arrives in double_value. To read such a parameter without guessing, use COALESCE.
SELECT
COALESCE(
(SELECT value.double_value FROM UNNEST(event_params) WHERE key = 'score'),
(SELECT CAST(value.int_value AS FLOAT64) FROM UNNEST(event_params) WHERE key = 'score')
) AS score
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX = '20250115'
AND event_name = 'level_end';To find out which field a parameter uses, list its key with all four value fields for a few rows and see which ones are not NULL.
UNNEST in the FROM clause
You can also unnest in FROM with a cross join. This produces one output row per parameter, which is useful for inspecting which parameters exist.
SELECT
ep.key,
COUNT(*) AS occurrences
FROM `my-project.analytics_123456789.events_*`,
UNNEST(event_params) AS ep
WHERE _TABLE_SUFFIX = '20250115'
AND event_name = 'page_view'
GROUP BY ep.key
ORDER BY occurrences DESC;Here the row count is multiplied by the number of parameters per event, so COUNT(*) counts parameters, not events. Use this form for discovery. Use the scalar subquery for metrics, where each event must stay as one row.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Column is all `NULL` | Wrong value field, or a misspelled key | Check all four value fields and the key's spelling |
| "Scalar subquery produces more than one element" | The key appears more than once in an event | Use `ARRAY_AGG(... LIMIT 1)` or inspect the data for duplicates |
| Counts are far too high | Cross join unnest multiplied the rows | Use scalar subqueries, or count distinct events |
| Unexpectedly large bytes billed | No `_TABLE_SUFFIX` filter, or `SELECT *` | Filter tables and select only needed columns |
| Doubled counts for recent days | Intraday and daily tables both matched | Bound the suffix range to dates |
Exercise
You run SELECT COUNT() FROM events_, UNNEST(eventparams) AS ep WHERE eventname = 'page_view' and expect the number of page views, but the result is about ten times larger. Why, and how would you fix it?
Show answer
The cross join with UNNEST produces one row for each parameter in each event, so each pageview is counted once per parameter. Remove the UNNEST from FROM and read the parameters you need with scalar subqueries, or count events directly with COUNT(*) on the events table without unnesting. Also add a TABLE_SUFFIX filter to limit the data scanned.
Exercise
Write the expression that reads the integer parameter gasessionid for each event, and explain why a query that reads value.string_value for it returns only NULL.
Show answer
(SELECT value.intvalue FROM UNNEST(eventparams) WHERE key = 'gasessionid'). GA4 stores gasessionid as an integer, so it is in intvalue, and stringvalue is NULL for that key.