Sub topic 14
Querying Custom Definitions with the Data API and BigQuery
Assume you know the GA4 event model, can access the property’s Data API credentials, and can run basic SQL. Registration makes a parameter available by a friendly definition in GA4 reports. The Data API and BigQuery expose that data differently.
Querying registered definitions with the Data API
The Google Analytics Data API uses API names, not the display names shown in Admin. For registered custom dimensions, the prefix identifies the scope:
| Definition | Data API name pattern | Example |
|---|---|---|
| Event-scoped dimension | `customEvent:parameter_name` | `customEvent:plan_tier` |
| User-scoped dimension | `customUser:parameter_name` | `customUser:customer_type` |
| Item-scoped dimension | `customItem:parameter_name` | `customItem:item_color` |
Custom metrics use the API name returned by the property’s metadata. For many event-scoped custom metrics, that name also uses the customEvent: prefix. Check metadata rather than guessing.
A runReport request can select a custom event dimension and a standard metric:
{
"dateRanges": [
{
"startDate": "28daysAgo",
"endDate": "today"
}
],
"dimensions": [
{
"name": "customEvent:plan_tier"
}
],
"metrics": [
{
"name": "eventCount"
}
]
}The response contains one row for each plan_tier value and its event count. A missing or invalid API name produces an error. Use the Data API metadata method to list dimensions and metrics available to the property, including their API names, compatibility, and descriptions.
The Data API applies GA4 reporting rules. It can return sampled or thresholded results in some situations, and reports may contain (other) when many rows are grouped. It also reflects processing delays. A parameter that was sent today may not be queryable immediately.
Querying the export in BigQuery
BigQuery exports GA4’s collected event records. Registration is not required for a parameter to appear in the export. The parameter must have been collected, and the export must contain the relevant date.
Event parameters are stored in the repeated event_params record. User properties are stored in user_properties. Ecommerce item parameters are nested inside the repeated items record.
SELECT
event_name,
(
SELECT value.string_value
FROM UNNEST(event_params)
WHERE key = 'plan_tier'
) AS plan_tier,
COUNT(*) AS events
FROM `my-project.analytics_123456789.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20261101' AND '20261128'
GROUP BY event_name, plan_tier
ORDER BY events DESC;A parameter value can use different fields in the value record. Read string_value, int_value, double_value, or float_value according to the value sent. In production queries, handle the expected type consistently.
To query an item parameter, first flatten the items, then flatten each item’s parameters:
SELECT
item.item_id,
(
SELECT value.string_value
FROM UNNEST(item.item_params)
WHERE key = 'item_color'
) AS item_color,
COUNT(*) AS item_rows
FROM `my-project.analytics_123456789.events_*`,
UNNEST(items) AS item
WHERE event_name = 'view_item'
GROUP BY item.item_id, item_color;BigQuery gives you rawer access than the Data API, but you must write the extraction logic, handle missing values, and avoid double-counting repeated records.
Choosing between the two
Use the Data API when you need GA4-compatible report results for a dashboard or application. Use BigQuery when you need row-level data, custom joins, advanced attribution, or validation of how parameters were actually collected. Compare date ranges, filters, time zones, and counting rules before expecting identical totals.
Exercise
A registered dimension is named “Customer type,” and its parameter name is customer_type. Which Data API dimension name should you try first, and where would you find the raw value in BigQuery?
Show answer
Try customUser:customer_type. In BigQuery, search the repeated user_properties records for the key customer_type, then read the appropriate value field.