Sub topic 19
Querying the Items Array in the BigQuery Export
This chapter assumes you have the GA4 BigQuery export linked, you can write basic BigQuery SQL (SELECT, WHERE, GROUP BY), and you know what the items array and the purchase event contain from earlier chapters. It shows how to flatten items into rows and how to avoid the mistakes that distort revenue.
How items are stored in the export
The export writes one row per event into daily tables named events_YYYYMMDD. Streaming tables named events_intraday_YYYYMMDD hold today's data. items is a repeated record, which means each event row holds a list of product structs. A purchase with three products is still one row, and that row has an items list of length three.
Event-level money values sit in a separate ecommerce struct on the same row, for example ecommerce.transaction_id, ecommerce.purchase_revenue, and ecommerce.refund_value.
Common fields inside each item struct:
| Field | Meaning |
|---|---|
| `item_id`, `item_name` | Product identity |
| `item_brand`, `item_variant`, `item_category` to `item_category5` | Product attributes |
| `price`, `price_in_usd` | Unit price in the event currency and converted to USD |
| `quantity` | Units in the event |
| `item_revenue`, `item_revenue_in_usd` | Revenue GA4 attributes to the item, filled on purchase events |
| `item_refund`, `item_refund_in_usd` | Refunded amount, filled on refund events |
| `item_list_id`, `item_list_name`, `item_list_index` | Where the item was listed |
| `coupon`, `promotion_id`, `promotion_name` | Item-level promotion data |
Flattening with UNNEST
UNNEST(items) turns the list into rows. Each result row combines one item with the columns of its parent event. A comma join is a CROSS JOIN, so events whose items list is empty disappear from the result. Use LEFT JOIN UNNEST(items) AS item when you need to keep them.
The wildcard table events_* also matches the intraday tables. Their _TABLE_SUFFIX begins with intraday_, so a BETWEEN filter on date strings excludes them. The filter also limits how much data BigQuery scans, which controls cost.
Revenue per product
This query ranks products by item revenue over the last 30 days. It removes duplicate purchase events before flattening, because a duplicated transaction_id would count a product twice.
WITH purchases AS (
SELECT
ecommerce.transaction_id AS transaction_id,
items
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN
FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY))
AND FORMAT_DATE('%Y%m%d', CURRENT_DATE())
AND event_name = 'purchase'
AND ecommerce.transaction_id IS NOT NULL
AND ecommerce.transaction_id != '(not set)'
QUALIFY ROW_NUMBER() OVER (
PARTITION BY ecommerce.transaction_id
ORDER BY event_timestamp
) = 1
)
SELECT
item.item_id,
ANY_VALUE(item.item_name) AS item_name,
SUM(item.quantity) AS units_sold,
SUM(item.item_revenue) AS item_revenue
FROM purchases, UNNEST(items) AS item
GROUP BY item.item_id
ORDER BY item_revenue DESC
LIMIT 20;Replace my_project and analytics_123456789 with your project and dataset. The export often shows a missing transaction ID as null or as (not set), so the query filters out both. Without the filter, all such rows would share one partition and only one would survive.
A funnel per product
Because every funnel event carries items, you can count how many times each product moved through each step.
SELECT
item.item_id,
ANY_VALUE(item.item_name) AS item_name,
COUNTIF(event_name = 'view_item') AS views,
COUNTIF(event_name = 'add_to_cart') AS adds_to_cart,
COUNTIF(event_name = 'purchase') AS purchase_events
FROM `my_project.analytics_123456789.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20261101' AND '20261130'
AND event_name IN ('view_item', 'add_to_cart', 'purchase')
GROUP BY item.item_id
ORDER BY views DESC;These are event counts, not unique users or sessions. For user-level rates, also select user_pseudo_id and use COUNT(DISTINCT ...).
Do not sum event-level values after UNNEST
Flattening repeats the parent event's columns on every item row. If a purchase has three items and you sum ecommerce.purchase_revenue after UNNEST(items), that order's revenue is counted three times.
Use this rule:
- For order totals, query the event without
UNNESTand useecommerce.purchase_revenue. - For product totals, flatten and use
item.item_revenueoritem.price * item.quantity. - Do not mix the two in one aggregation.
SELECT
SUM(ecommerce.purchase_revenue) AS order_revenue,
COUNT(DISTINCT ecommerce.transaction_id) AS orders
FROM `my_project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20261101' AND '20261130'
AND event_name = 'purchase';Product revenue and order revenue also differ in definition. Item revenue excludes tax and shipping, and the order-level value you send may not follow the same rule. Differences between the two totals are normal, but they should be explainable.
Other gotchas
- Currency.
priceanditem_revenueuse the event's currency. Use the_in_usdfields only when you need one comparable currency, and note that GA4 does the conversion. - Refunds. A refund event has its own row. Subtract
item_refund(orecommerce.refund_valueat order level) from revenue to get net figures, and join to purchases bytransaction_id. - Custom item parameters. Item-scoped custom parameters appear in the
item_paramsrepeated record inside each item, with the same key and value layout asevent_params. - Data freshness. Intraday tables can change until the daily table is finalized. Compare reports against complete days only.
- Sampling and thresholds. The export is unsampled, so totals can differ from standard GA4 reports that apply thresholds or modeling.
Exercise
A purchase event has two items and ecommerce.purchaserevenue of 100. You run SUM(ecommerce.purchaserevenue) after FROM events_*, UNNEST(items) AS item. What number do you get for this order, and how do you fix the query?
Show answer
You get 200, because UNNEST produces one row per item and repeats the event-level purchaserevenue on both rows. Either remove UNNEST and sum ecommerce.purchaserevenue per event, or keep UNNEST and sum an item-level field such as item.item_revenue.
Exercise
Why does the revenue-per-product query filter on ecommerce.transactionid and use QUALIFY ROWNUMBER() before UNNEST?
Show answer
QUALIFY keeps only the first purchase event for each transaction_id, so a duplicated event does not count its items twice. Filtering out null and '(not set)' IDs first stops all rows without a real ID from collapsing into one partition, where only a single row would survive.