Sub topic 18
Querying the Items Array in the BigQuery Export
Assume you know the GA4 interface, basic SQL, JavaScript arrays, and JSON. This chapter uses BigQuery Standard SQL and assumes your GA4 property is linked to BigQuery.
Querying the items array
GA4 exports one row for each event in tables such as events_20261128. The items field is a repeated record, which means one event can contain multiple product records. Use UNNEST(items) to turn each product into its own query row.
SELECT
item.item_id,
item.item_name,
SUM(item.quantity) AS units_sold,
SUM(COALESCE(item.item_revenue, item.price * item.quantity))
AS item_revenue
FROM `project_id.analytics_123456.events_*`,
UNNEST(items) AS item
WHERE _TABLE_SUFFIX BETWEEN '20261101' AND '20261128'
AND event_name = 'purchase'
GROUP BY item.item_id, item.item_name
ORDER BY item_revenue DESC;Replace project_id.analytics_123456 with your project and dataset. The wildcard reads several daily tables. _TABLE_SUFFIX limits which tables BigQuery scans.
UNNEST creates one row per item. The query then groups those rows by product. item_revenue is the preferred item-level revenue field when it is present. The fallback calculation uses unit price multiplied by quantity.
| BigQuery field | Meaning |
|---|---|
| `event_date` | Date when GA4 recorded the event |
| `ecommerce.transaction_id` | Order identifier |
| `item.item_id` | Product or stock-keeping-unit identifier |
| `item.quantity` | Number of units in the item row |
| `item.price` | Price of one unit |
This structure separates item metrics from event metrics:
Common data-quality pitfalls
Do not sum ecommerce.purchase_revenue after unnesting items. Event-level revenue can be repeated on every resulting item row, causing overcounting. Sum item-level revenue for product analysis. Sum event-level revenue from purchase rows without unnesting for order-level analysis.
Check these issues when results look wrong:
- A purchase event should have a stable, non-empty
transaction_id. - A transaction should not be sent more than once by the browser and server.
currencyshould be a valid three-letter currency code, andvalueshould use that currency.priceshould be the unit price, whilequantityshould be the number of units.- Product IDs should remain consistent across listing, cart, purchase, and refund events.
- Refunds should reference the original transaction and should not be counted as new purchases.
- Test orders and internal traffic should be excluded consistently.
Exercise
Exercise
Why can summing purchase revenue after UNNEST(items) produce an inflated total?
Show answer
UNNEST creates one row per item, so the same event-level revenue may appear once for every item. Use item-level revenue for item totals or aggregate event-level revenue before unnesting.