GA4 in BigQuery · Ecommerce and conversions
Item revenue and units sold in GA4 BigQuery
Item-level ecommerce data in the GA4 export is the repeated items record. UNNEST(items) on purchase events gives one row per item sold, with item_name, item_category, quantity and item_revenue_in_usd. Sum them per item for units and revenue.
The query
SELECT
item.item_name,
item.item_category,
SUM(item.quantity) AS units,
ROUND(SUM(item.item_revenue_in_usd), 2) AS item_revenue_usd,
COUNT(DISTINCT ecommerce.transaction_id) AS orders
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'purchase'
GROUP BY item_name, item_category
ORDER BY item_revenue_usd DESC
LIMIT 20;How it works
items is a repeated record with one element per product in the event: item_id, item_name, item_brand, item_variant, item_category to item_category5, price and price_in_usd, quantity, item_revenue and item_revenue_in_usd, coupons, list and promotion fields. Ecommerce events such as view_item, add_to_cart and purchase carry it.
Here you want one row per item, so the comma join FROM events_*, UNNEST(items) AS item is the right pattern, unlike event parameters where a scalar subquery is better. Each purchase event becomes as many rows as it has items. Google's schema says item_revenue_in_usd is price times quantity and is populated for purchase events only, which is why the query filters on purchase.
Fields of the event (such as ecommerce.transaction_id or traffic_source.medium) stay available on every item row. Do not sum event-level values such as ecommerce.purchase_revenue_in_usd after the unnest: each order's revenue would be repeated once per item.
| Column | Meaning |
|---|---|
item_name / item_category | Product name and top-level category from the item. |
units | Sum of quantity. Google's schema says quantity defaults to 1 when not sent. |
item_revenue_usd | Sum of item_revenue_in_usd. |
orders | Distinct transactions that included the item. |
Notes and cost
- Cost: across the sample
itemsis 946.5 MiB, the second largest column afterevent_params, but its subfields are stored separately. This month, reading only five item fields, costs 110.7 MiB.SELECT item.*orTO_JSON_STRING(items)would read all of them. - Filtering
event_name = 'purchase'keeps the result right but does not cut bytes: BigQuery reads the referenceditemssubfields for every event in the scanned tables. item_categoryvalues must be consistent in your tagging. In GA4 they are free text, soT-Shirtsandt-shirtsare separate categories.
Variations
Item funnel: viewed, added to cart, purchased
SELECT
item.item_name,
COUNTIF(event_name = 'view_item') AS views,
COUNTIF(event_name = 'add_to_cart') AS adds_to_cart,
SUM(IF(event_name = 'purchase', item.quantity, 0)) AS units_purchased,
ROUND(SAFE_DIVIDE(COUNTIF(event_name = 'add_to_cart'), COUNTIF(event_name = 'view_item')), 3) AS view_to_cart_rate
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name IN ('view_item', 'add_to_cart', 'purchase')
GROUP BY item_name
HAVING views >= 100
ORDER BY views DESC
LIMIT 20;Revenue by category
SELECT
item.item_category,
SUM(item.quantity) AS units,
ROUND(SUM(item.item_revenue_in_usd), 2) AS item_revenue_usd
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'purchase'
GROUP BY item_category
ORDER BY item_revenue_usd DESC;FAQ
How do I get product revenue from GA4 BigQuery?
Unnest items on purchase events and sum item.item_revenue_in_usd per item_name or item_id.
Why does item revenue not add up to purchase revenue?
Purchase revenue is set per order and can include values the items do not, depending on how the tag builds it. Item revenue is price times quantity per item. Compare them per transaction to see the gap on your data.
Why are my totals too high after UNNEST(items)?
Event-level fields repeat on every item row. Summing ecommerce.purchase_revenue_in_usd after the unnest counts each order once per item.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
