GA4 in BigQuery · Ecommerce and conversions
Ecommerce funnel from view_item to purchase in GA4 BigQuery
Build a GA4 checkout funnel by flagging, per user, which of view_item, add_to_cart, begin_checkout, add_shipping_info, add_payment_info and purchase they reached, each step requiring the previous ones. It reads only event_name and user_pseudo_id, so it is cheap.
The query
WITH users AS (
SELECT
user_pseudo_id,
LOGICAL_OR(event_name = 'view_item') AS s1,
LOGICAL_OR(event_name = 'add_to_cart') AS s2,
LOGICAL_OR(event_name = 'begin_checkout') AS s3,
LOGICAL_OR(event_name = 'add_shipping_info') AS s4,
LOGICAL_OR(event_name = 'add_payment_info') AS s5,
LOGICAL_OR(event_name = 'purchase') AS s6
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id
),
steps AS (
SELECT
COUNTIF(s1) AS view_item,
COUNTIF(s1 AND s2) AS add_to_cart,
COUNTIF(s1 AND s2 AND s3) AS begin_checkout,
COUNTIF(s1 AND s2 AND s3 AND s4) AS add_shipping_info,
COUNTIF(s1 AND s2 AND s3 AND s4 AND s5) AS add_payment_info,
COUNTIF(s1 AND s2 AND s3 AND s4 AND s5 AND s6) AS purchase
FROM users
)
SELECT
step_number,
step,
users,
ROUND(SAFE_DIVIDE(users, FIRST_VALUE(users) OVER (ORDER BY step_number)), 4) AS share_of_first_step,
ROUND(SAFE_DIVIDE(users, LAG(users) OVER (ORDER BY step_number)), 4) AS share_of_previous_step
FROM steps,
UNNEST([
STRUCT(1 AS step_number, 'view_item' AS step, view_item AS users),
(2, 'add_to_cart', add_to_cart),
(3, 'begin_checkout', begin_checkout),
(4, 'add_shipping_info', add_shipping_info),
(5, 'add_payment_info', add_payment_info),
(6, 'purchase', purchase)
])
ORDER BY step_number;How it works
The query is a closed funnel at user level. It reduces the month to one row per user with a flag per step, then counts users who reached each step and every step before it. A user who purchased without a recorded view_item (for example by reordering from an account page) is not counted in later steps, which is usually what a funnel report should do.
The result is pivoted into one row per step with two ratios: the share of users from the first step and from the previous step. The biggest drop between consecutive steps is where to look first.
The flags ignore order and time. A user who added to cart in week one and viewed an item in week three still passes both steps. For a strict sequence, compare the first timestamp of each step (first variation), or build the funnel per session instead of per user by grouping on user_pseudo_id and ga_session_id.
GA4 Explorations funnels can be open or closed, per user or per session, and can be sampled. Match those settings before comparing numbers.
| Column | Meaning |
|---|---|
step | Funnel step, named after its event. |
users | Users who reached this step and all earlier ones. |
share_of_first_step | Users at this step divided by users at view_item. |
share_of_previous_step | Step-to-step conversion. |
Notes and cost
- Cost: only
event_nameanduser_pseudo_idfor one month, 51.9 MiB. A funnel per session addsevent_paramsand costs several times more; check whether user level answers the question. - The six event names are GA4's recommended ecommerce events, which the sample uses. If your site skips a step (for example no
add_shipping_info), remove it from the funnel or every later step drops to zero.
Variations
Ordered funnel with first timestamps
Each step counts only if it happened after the previous step's first occurrence.
WITH firsts AS (
SELECT
user_pseudo_id,
MIN(IF(event_name = 'view_item', event_timestamp, NULL)) AS t1,
MIN(IF(event_name = 'add_to_cart', event_timestamp, NULL)) AS t2,
MIN(IF(event_name = 'begin_checkout', event_timestamp, NULL)) AS t3,
MIN(IF(event_name = 'purchase', event_timestamp, NULL)) AS t4
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name IN ('view_item', 'add_to_cart', 'begin_checkout', 'purchase')
GROUP BY user_pseudo_id
)
SELECT
COUNTIF(t1 IS NOT NULL) AS view_item,
COUNTIF(t2 > t1) AS add_to_cart,
COUNTIF(t2 > t1 AND t3 > t2) AS begin_checkout,
COUNTIF(t2 > t1 AND t3 > t2 AND t4 > t3) AS purchase
FROM firsts;Funnel per session
WITH sessions AS (
SELECT
LOGICAL_OR(event_name = 'view_item') AS s1,
LOGICAL_OR(event_name = 'add_to_cart') AS s2,
LOGICAL_OR(event_name = 'begin_checkout') AS s3,
LOGICAL_OR(event_name = 'purchase') AS s4
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id, (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id')
)
SELECT
COUNTIF(s1) AS view_item,
COUNTIF(s1 AND s2) AS add_to_cart,
COUNTIF(s1 AND s2 AND s3) AS begin_checkout,
COUNTIF(s1 AND s2 AND s3 AND s4) AS purchase
FROM sessions;FAQ
How do I build a GA4 funnel in BigQuery?
Group events per user (or per session), flag each funnel event with LOGICAL_OR(event_name = '...'), and count users who have each flag and all previous ones.
Should a GA4 funnel be per user or per session?
Per user shows whether people eventually buy, across visits. Per session shows whether a single visit converts. Per session needs ga_session_id, which makes the query more expensive.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
