Sub topic 13
Seeing the Model in the BigQuery Export
This chapter assumes you know what GA4 events, event parameters, user properties, and sessions are, and that you can read basic SQL. The BigQuery export shows the GA4 data model with nothing hidden. Every row is an event, and users and sessions are not stored as rows. You rebuild them from event fields.
What the export contains
When you link a GA4 property to a BigQuery project, GA4 writes event data into a dataset named analytics_<property_id>. Two kinds of tables appear:
events_YYYYMMDDis a daily table. It holds one row per event for that day. Google writes it after the day ends, and it can be updated later for late-arriving events.events_intraday_YYYYMMDDis a streaming table for the current day. It is only available with the streaming export, and it is replaced by the daily table.
You query all daily tables with a wildcard, events_*, and limit the date range with the _TABLE_SUFFIX pseudo column. Filtering on _TABLE_SUFFIX makes BigQuery scan only the tables you name, which controls cost.
One row is one event
The export mirrors the principle that everything is an event. A visitor who opens a page, scrolls, and clicks a link produces at least three rows: page_view, scroll, and click. Each row carries the same user and session identifiers, so you can group the rows back together.
The main top-level columns are:
| Column | Type | Meaning |
|---|---|---|
| `event_date` | STRING | Date of the event as `YYYYMMDD`, in the property's reporting time zone |
| `event_timestamp` | INTEGER | Event time in microseconds since the Unix epoch, in UTC |
| `event_name` | STRING | Name of the event, such as `purchase` |
| `event_params` | REPEATED RECORD | Key/value pairs describing the event |
| `user_pseudo_id` | STRING | The client-level identifier, derived from the client ID |
| `user_id` | STRING | The User-ID you set, if any |
| `user_properties` | REPEATED RECORD | Key/value pairs describing the user |
| `items` | REPEATED RECORD | The items array for ecommerce events |
| `traffic_source` | RECORD | The source, medium, and campaign that first acquired the user |
| `device`, `geo`, `platform` | RECORD or STRING | Context about the device, location, and stream type |
Notice that there is no session_id column and no sessions table. A session exists only as a convention: events share a ga_session_id parameter.
Parameters are nested, repeated records
event_params is an array of records. Each record has a key and a value. The value is itself a record with four typed fields: string_value, int_value, float_value, and double_value. Only one field is filled for each parameter, and which one depends on the type of the data you sent.
This design explains two common surprises:
- You cannot write
event_params.page_location. You must search the array for the key you want. - You must know which typed field holds the value.
ga_session_idis an integer, so it is inint_value.page_locationis text, so it is instring_value. Reading the wrong field returnsNULL.
The standard way to read one parameter is a scalar subquery over UNNEST:
SELECT
event_name,
(SELECT ep.value.string_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'page_location') AS page_location
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX = '20240115'
AND event_name = 'page_view'
LIMIT 100;Because the subquery returns one value per row, the output stays at one row per event. If you used a plain CROSS JOIN UNNEST(event_params) instead, each event would become many rows, one per parameter, and your counts would multiply.
The same pattern applies to user_properties, where each record also has a key and value, plus a set_timestamp_micros field. The items array works the same way, except that its fields are named columns such as item_id, item_name, price, and quantity.
How the model maps to the export
The diagram shows where each concept from the earlier chapters ends up in the export.
Rebuilding users
The export has no single user table. Two columns identify people:
user_pseudo_idis the identifier tied to the browser or app instance, which comes from the client ID. One person on a phone and a laptop has two values.user_idis the value you set through the User-ID feature. It isNULLon events from visitors who are not signed in, and it can also beNULLon events that happened before sign-in.
A count of distinct user_pseudo_id values approximates total users. To follow a signed-in person across devices, group by user_id where it exists, and fall back to user_pseudo_id otherwise:
SELECT
COALESCE(user_id, user_pseudo_id) AS person_key,
COUNT(*) AS events
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
GROUP BY person_key;This is a simplified identity rule. GA4 reports can apply a reporting identity setting (blended, observed, or device-based), so a query like this will not match every report exactly.
Active users are not a column either. The interface defines an active user as someone with an engaged session, or, for apps, someone who triggers the first_open or engagement-type activity. You rebuild that from the session-level engagement signals described below.
Rebuilding sessions
The session identifier is the pair user_pseudo_id and ga_session_id. The ga_session_id value is the timestamp, in seconds, when the session started. It is unique only within one user, so two users can share the same number. This is why the chapter's SQL example concatenates the two:
-- Count events, users, and sessions per day from the GA4 BigQuery export
SELECT
event_date,
COUNT(*) AS event_count,
COUNT(DISTINCT user_pseudo_id) AS users,
COUNT(DISTINCT CONCAT(
user_pseudo_id,
'.',
CAST((SELECT ep.value.int_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'ga_session_id') AS STRING)
)) AS sessions
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
GROUP BY event_date
ORDER BY event_date;Read the query in three steps:
- The scalar subquery pulls
ga_session_idout of theevent_paramsarray. CONCATjoins it touser_pseudo_id, giving a key that is unique across users.COUNT(DISTINCT ...)counts each key once, however many events share it.
Two points about the result. Events with no ga_session_id produce a NULL inside the concatenation. CONCAT returns NULL if any argument is NULL, and COUNT(DISTINCT) ignores NULL, so those events add nothing to the session count. Also, a session that crosses midnight appears under two event_date values, so summing daily sessions over a range overcounts. For a range total, compute the distinct count over the whole range rather than adding up daily numbers.
Rebuilding engaged sessions
A session is engaged when it lasts longer than ten seconds, has a key event, or has two or more page or screen views. In the export, two parameters help:
session_engagedis set to1on events in an engaged session (the value is stored as a string on some properties and an integer on others, so check your own data).engagement_time_msecis the time the page or screen was in the foreground, per event.
This query computes engaged sessions and an engagement rate for one month:
WITH session_events AS (
SELECT
CONCAT(
user_pseudo_id, '.',
CAST((SELECT ep.value.int_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'ga_session_id') AS STRING)
) AS session_key,
(SELECT ep.value.string_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'session_engaged') AS engaged_string,
(SELECT ep.value.int_value
FROM UNNEST(event_params) AS ep
WHERE ep.key = 'session_engaged') AS engaged_int
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
),
sessions AS (
SELECT
session_key,
MAX(IF(engaged_string = '1' OR engaged_int = 1, 1, 0)) AS is_engaged
FROM session_events
WHERE session_key IS NOT NULL
GROUP BY session_key
)
SELECT
COUNT(*) AS sessions,
SUM(is_engaged) AS engaged_sessions,
SAFE_DIVIDE(SUM(is_engaged), COUNT(*)) AS engagement_rate
FROM sessions;The MAX step collapses many event rows into one flag per session. A session counts as engaged if any of its events carries the engaged marker. Handling both the string and integer forms makes the query safe whichever way your property stores the value.
Rebuilding counts for key events and items
Key events are ordinary events that you marked in the interface. The export does not label them in the event rows, so you filter by event_name yourself. For example, COUNTIF(event_name = 'purchase') counts purchases. For items, you flatten the array with a join because each event holds several products:
SELECT
item.item_id,
SUM(item.quantity) AS units_sold
FROM `my-project.analytics_123456789.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
AND event_name = 'purchase'
GROUP BY item.item_id
ORDER BY units_sold DESC;Here the join is intended. You want one row per item, so the row count grows on purpose. Do not use this form to count events, since an event with three items appears three times.
Things that make BigQuery differ from reports
- Thresholds and modeling. Reports may hide small values or use consented-user modeling. The export holds only what was collected.
- Custom definitions. The export contains every parameter you send, whether or not you registered it as a custom dimension. Registration matters only for the interface.
- Time zones.
event_datefollows the property's reporting time zone.event_timestampis UTC. Mixing them gives off-by-one-day errors near midnight. - Late data. Events can arrive after their day closes. Recent daily tables can change, so wait a few days before treating a total as final.
- Export limits. Standard properties have a daily limit on exported events (one million at the time of writing). Check the current quota for your property.
Summary of the mapping
| GA4 concept | Where it lives in the export | How to get it |
|---|---|---|
| Event | One row | `COUNT(*)` |
| Event parameter | `event_params` | Scalar subquery on `key` |
| User | `user_pseudo_id`, `user_id` | `COUNT(DISTINCT ...)` |
| User property | `user_properties` | Scalar subquery on `key` |
| Session | `ga_session_id` parameter | Concatenate with `user_pseudo_id` |
| Engaged session | `session_engaged` parameter | `MAX` per session |
| Key event | `event_name` | Filter by name |
| Item | `items` | `UNNEST(items)` |
Exercise
A teammate counts sessions with COUNT(DISTINCT ga_session_id) taken from the event parameters, without including user_pseudo_id. The result is much lower than the GA4 report. Why, and how do you fix it?
Show answer
ga_session_id is the session start time in seconds, so it is only unique within one user. Two users who start a session in the same second share the same value, and COUNT(DISTINCT) merges them into one. Concatenate user_pseudo_id with ga_session_id and count distinct values of that combined key.
Exercise
You run SELECT COUNT(*) FROM events_*, UNNEST(event_params) for one day and get a number about twenty times larger than the number of events. What happened, and which pattern avoids it when you only need one parameter?
Show answer
The UNNEST in the FROM clause turns each event into one row per parameter, so every event is counted once for each parameter it carries. To read one parameter and keep one row per event, use a scalar subquery such as (SELECT ep.value.string_value FROM UNNEST(event_params) AS ep WHERE ep.key = 'page_location').