Sub topic 14
Going Further: Building Path Counts in BigQuery
This chapter assumes you know the GA4 interface, event parameters, and the difference between users, sessions, and events. It also assumes that your GA4 property is linked to BigQuery and that you can run a standard SQL query there.
GA4 Path exploration is useful for interactive analysis, but it does not expose every detail of a user journey. The BigQuery export gives you the raw event rows, so you can define your own path rules, date ranges, filters, and metrics.
How the BigQuery export represents a path
GA4 exports one row for each event. Important columns include:
user_pseudo_id: the analytics identifier for a device or browser.event_name: the name of the event, such aspage_vieworpurchase.event_timestamp: the event time in microseconds.event_params: repeated key-value data attached to the event.ga_session_id: a parameter that identifies the session.
A path is not stored as one ready-made column. You create it by ordering events within a session and pairing each event with the next event.
Count adjacent event transitions
The following query counts how often one event is immediately followed by another during a session. Replace the project, dataset, and table names with your values.
WITH ordered_events AS (
SELECT
user_pseudo_id,
(
SELECT value.int_value
FROM UNNEST(event_params)
WHERE key = 'ga_session_id'
) AS session_id,
event_name,
event_timestamp,
event_bundle_sequence_id
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20260101' AND '20260131'
),
session_events AS (
SELECT
user_pseudo_id,
session_id,
event_name,
event_timestamp,
event_bundle_sequence_id,
LEAD(event_name) OVER (
PARTITION BY user_pseudo_id, session_id
ORDER BY event_timestamp, event_bundle_sequence_id
) AS next_event
FROM ordered_events
WHERE session_id IS NOT NULL
)
SELECT
event_name AS from_event,
next_event AS to_event,
COUNT(*) AS transition_occurrences,
COUNT(DISTINCT STRUCT(user_pseudo_id, session_id))
AS sessions_with_transition
FROM session_events
WHERE next_event IS NOT NULL
GROUP BY from_event, to_event
ORDER BY transition_occurrences DESC
LIMIT 20;LEAD reads the value from the next row in an ordered window. The query therefore creates pairs such as page_view → view_item and view_item → add_to_cart.
The two count columns answer different questions:
transition_occurrencescounts every matching adjacent pair, including repeated pairs in one session.sessions_with_transitioncounts each user-session combination once for that pair.
The second metric is often easier to compare with a path diagram. However, it is still an approximation because the GA4 interface may apply different counting, filtering, or identity rules.
Use pages instead of event names
To build a page path, extract a page parameter from event_params. For page_view events, the relevant parameter is commonly page_location or page_title. Replace event_name in the query with a conditional expression such as:
CASE
WHEN event_name = 'page_view' THEN (
SELECT value.string_value
FROM UNNEST(event_params)
WHERE key = 'page_location'
)
ELSE event_name
END AS nodeUsing full URLs can create too many nodes because query strings produce separate values. Removing tracking parameters or grouping URLs by page path usually gives a more useful result.
>callout note >BigQuery counts only the data that has been exported. Recently collected events may not be available yet, and daily export tables can change after late events are processed.
Know when BigQuery is the better choice
Use BigQuery when you need:
- More than the visible path depth or node choices in Explore.
- Custom rules, such as ignoring
scrollevents. - Exact date partition control.
- User-level or session-level deduplication.
- Paths that cross several event types with custom labels.
- A repeatable query for reporting or experimentation.
BigQuery also introduces responsibilities. You must handle event ordering, missing session IDs, duplicate-looking events, time zones, late arrivals, and consent-related gaps. A query can produce precise counts for the exported data while still failing to represent every real user action.
Exercise
Why does the query use LEAD after partitioning by userpseudoid and session_id?
Show answer
LEAD retrieves the next event within each ordered session, allowing the query to form adjacent transitions without pairing events from different users or sessions.