Sub topic 14
Rebuilding paths from BigQuery export and the Data API
This chapter assumes you can read basic SQL (joins, GROUP BY, UNNEST). It also assumes you have used the Conversion paths and Model comparison reports, and that your GA4 property is linked to BigQuery. You will rebuild a simplified version of a conversion path from raw events, then pull a session-scoped summary from the Data API. Both give you a way to check the interface reports.
What you can and cannot rebuild
The Conversion paths report shows paths after GA4 applies its attribution logic, lookback window and data thresholds. The raw export contains none of that. It holds one row per event, with traffic source fields attached.
If you query it yourself, you get an ordered list of the sessions a user had before a key event, with the source and medium each session arrived from. That is a useful approximation. It is not a copy of the report. Expect differences for these reasons:
- GA4 credits only touchpoints inside the lookback window. Your query includes every session in the date range unless you filter.
- The report applies the attribution model you selected. Your path list is unweighted.
- The report works at the level of channels. Your query works at source and medium, and you must map those to channel groups yourself.
- The report can hide or aggregate rows under data thresholds. BigQuery does not.
- The export identifies users by
user_pseudo_id(the client ID). The report may use User-ID or Google signals if you enabled them, so the same person can look like one user in the report and two in BigQuery.
The shape of the work
Where the source and medium live
In the export, each event row has a collected_traffic_source record with fields such as manual_source, manual_medium and manual_campaign_name. These hold the values GA4 collected from UTM parameters or auto-tagging. They describe how the user arrived at that session. The export also has traffic_source, which is a user-level field showing the first acquisition source and does not change per session. For path work, use the event-level collected_traffic_source.
A session has no row of its own. You identify it by combining user_pseudo_id with the ga_session_id event parameter, which is stored as an integer in the event_params array.
Traffic source fields are not guaranteed to be filled on every event of a session. A simple ANY_VALUE over the session can return a null even when another event holds a real value. The query below takes the earliest non-null value instead.
A path query
This query finds each user's first purchase in the date range, keeps their sessions up to that moment, and builds a path string. Replace project.dataset with your export dataset.
-- Source/medium path to each user's first purchase, from the GA4 BigQuery export.
WITH first_purchase AS (
SELECT
user_pseudo_id,
MIN(event_timestamp) AS purchase_ts
FROM `project.dataset.events_*`
WHERE event_name = 'purchase'
AND _TABLE_SUFFIX BETWEEN '20251201' AND '20260131'
GROUP BY user_pseudo_id
),
sessions AS (
SELECT
e.user_pseudo_id,
(SELECT value.int_value
FROM UNNEST(e.event_params)
WHERE key = 'ga_session_id') AS session_id,
MIN(e.event_timestamp) AS session_start_ts,
ARRAY_AGG(e.collected_traffic_source.manual_source IGNORE NULLS
ORDER BY e.event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS source,
ARRAY_AGG(e.collected_traffic_source.manual_medium IGNORE NULLS
ORDER BY e.event_timestamp LIMIT 1)[SAFE_OFFSET(0)] AS medium
FROM `project.dataset.events_*` AS e
JOIN first_purchase AS p
ON e.user_pseudo_id = p.user_pseudo_id
WHERE e._TABLE_SUFFIX BETWEEN '20251201' AND '20260131'
AND e.event_timestamp <= p.purchase_ts
GROUP BY e.user_pseudo_id, session_id
HAVING session_id IS NOT NULL
),
paths AS (
SELECT
user_pseudo_id,
STRING_AGG(
CONCAT(IFNULL(source, '(direct)'), ' / ', IFNULL(medium, '(none)')),
' > ' ORDER BY session_start_ts
) AS path,
COUNT(*) AS path_length
FROM sessions
GROUP BY user_pseudo_id
)
SELECT
path,
path_length,
COUNT(*) AS purchasers
FROM paths
GROUP BY path, path_length
ORDER BY purchasers DESC
LIMIT 100;Three choices in this query matter:
- It stops at the first purchase. Sessions after the purchase are not part of the path that led to it.
- It uses a longer date range than the purchase window would need. Sessions before
20251201are invisible, so users whose journey began earlier get truncated paths. Start your session range earlier than your purchase range to match a lookback window, for example 30 or 90 days earlier. - It maps missing values to
(direct) / (none). That matches how GA4 labels direct traffic, but a null can also mean broken tagging. See the checks below.
The wildcard events_* also matches events_intraday_* tables. Because intraday_... sorts after the digit-only date suffixes, the BETWEEN filter excludes them here. If you filter on a single date or use a different pattern, check that you are not mixing in partial intraday data.
Adding a lookback window
To approximate a 30-day lookback, keep only sessions that started within 30 days before the purchase. Add this condition to the sessions CTE, after the join:
AND e.event_timestamp >= p.purchase_ts - 30 * 24 * 60 * 60 * 1000000Event timestamps are in microseconds, so the arithmetic uses 1000000. A common mistake is subtracting seconds, which leaves a window that is a million times too short.
This is still only an approximation of GA4's window. GA4 measures the window from the touchpoint to the key event and applies different defaults for different event types. It is enough to see how path length shrinks as the window narrows.
Mapping to channel groups
Source and medium pairs are too granular for most analysis. google / cpc and bing / cpc are both Paid Search. You can map them with a CASE expression that follows the rules you use in your channel group:
CASE
WHEN medium IN ('cpc', 'ppc', 'paidsearch') THEN 'Paid Search'
WHEN medium = 'organic' THEN 'Organic Search'
WHEN medium = 'email' THEN 'Email'
WHEN medium = 'referral' THEN 'Referral'
WHEN source IS NULL AND medium IS NULL THEN 'Direct'
ELSE 'Other'
END AS channelThis is a simplification of the real default channel group rules, which also look at source lists, campaign names and auto-tagged click IDs. If you need exact parity, define your own mapping and document it, or use the channel group GA4 computes in the interface and the API.
Checks before you trust a path
- Share of
(direct) / (none)paths. A very high share often means missing UTM tags or a redirect that strips parameters. - Paths with
(not set)values. The same tagging gap shows up here as it does in the unassigned traffic row in the interface. - Sessions with a null
ga_session_id. TheHAVINGclause drops them. Count how many you dropped. - Single-session paths. A large majority of purchasers with one session is normal for some sites, but it means path analysis adds little.
- Consent mode and cookie loss. A user who declines or clears cookies appears as a new
user_pseudo_id, so their journey splits into short paths.
Using the Data API
The Data API is the right tool when you want the numbers the interface shows, programmatically. It returns aggregated, processed data, so it respects thresholds and uses GA4's own channel grouping. Send a runReport request to https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReport with this body:
{
"dateRanges": [{ "startDate": "30daysAgo", "endDate": "yesterday" }],
"dimensions": [{ "name": "sessionDefaultChannelGroup" }],
"metrics": [{ "name": "sessions" }, { "name": "keyEvents" }],
"orderBys": [{ "metric": { "metricName": "keyEvents" }, "desc": true }],
"limit": 10
}This response is session-scoped: each key event is counted under the channel group of the session in which it happened. That differs from the Advertising reports, which credit key events to channels by the selected attribution model. Putting the two side by side is a good way to see how much credit moves when a model assigns conversions to earlier touchpoints.
The standard Data API does not return Conversion paths report rows, so you cannot fetch the path table directly. If you need path-level data in a pipeline, the BigQuery export is the route.
| Need | Best source | Why |
|---|---|---|
| Ordered channel sequences per user | BigQuery export | Event-level rows let you order sessions |
| Session-scoped channel totals | Data API | Uses GA4's channel groups and is quick to run |
| Exact attributed credit by model | Advertising reports | The attribution logic runs only there |
| Check for tagging problems | BigQuery export | You can see raw null and `(not set)` values |
Exercise
Your rebuilt paths show 70% of purchasers with the single path (direct) / (none), but the Data API shows Paid Search and Organic Search driving most sessions. Name two likely causes and one query check for each.
Show answer
First, UTM parameters or auto-tagging may be lost, for example by a redirect, so the traffic source fields are null and the query maps them to direct. Check by counting sessions where collected_traffic_source.manual_source is null on every event, grouped by landing page. Second, the query may be reading the wrong field or taking a null from ANY_VALUE instead of the first non-null value, so sources are dropped. Check by comparing the ARRAY_AGG ... IGNORE NULLS result with a count of sessions that have at least one non-null manual_source. A third possible cause is that sessions before the date range start are missing, which truncates paths to the final direct visit.
Exercise
You change the lookback filter in the query from 30 to 90 days. What happens to average path length, and why can the result still differ from the report's 90-day setting?
Show answer
Average path length rises or stays the same, because more earlier sessions fall inside the window. It can still differ from the report because GA4 applies its own attribution model, counts per touchpoint type, uses User-ID or Google signals where available, and may apply data thresholds, none of which the raw query reproduces.