Sub topic 16
Reproducing an Exploration with the GA4 Data API
Assume that you have Viewer access to a GA4 property, understand dimensions and metrics, and can read a pivot table. This chapter shows how to express a Free-form exploration as a runReport request to the GA4 Data API.
The API can reproduce the data query behind an exploration. It does not reproduce the Explore interface, cell colors, or chart settings. The API returns rows and metric values. Your application must format or visualize those results.
Map exploration settings to API fields
A Free-form table usually has:
- Rows, which become API dimensions.
- Values, which become API metrics.
- Date range, which becomes
dateRanges. - Tab filters, which become
dimensionFilterormetricFilter. - Segments, which may require separate requests or more advanced filtering. A segment in Explore is not always a direct API field.
For example, suppose the exploration has channel group and device category as rows, with sessions, engaged sessions, and users as values:
{
"dateRanges": [{ "startDate": "28daysAgo", "endDate": "yesterday" }],
"dimensions": [
{ "name": "sessionDefaultChannelGroup" },
{ "name": "deviceCategory" }
],
"metrics": [
{ "name": "sessions" },
{ "name": "engagedSessions" },
{ "name": "totalUsers" }
],
"limit": 25
}Send this body to the property endpoint:
POST https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReportReplace PROPERTY_ID with the numeric GA4 property ID. The request also needs Google authentication with permission to read the property.
The response contains a dimensionHeaders list, a metricHeaders list, and a rows list. Each row contains dimension values and metric values in the same order as their headers. A table-building program should match values by header name rather than relying on a fixed column position.
flowchart LR
A[Explore Variables] --> B[Dimensions and metrics]
B --> C[runReport request]
C --> D[Rows and metric values]
D --> E[Table or chart in your application]Add a filter
In Explore, filtering Device category to mobile narrows the table before you inspect the results. The equivalent request uses dimensionFilter:
{
"dateRanges": [{ "startDate": "28daysAgo", "endDate": "yesterday" }],
"dimensions": [{ "name": "sessionDefaultChannelGroup" }],
"metrics": [{ "name": "sessions" }],
"dimensionFilter": {
"filter": {
"fieldName": "deviceCategory",
"stringFilter": { "matchType": "EXACT", "value": "mobile" }
}
}
}The filtered dimension does not need to appear in dimensions. It can restrict the data while another dimension remains in the returned rows.
Check compatibility before coding
The API enforces many of the same compatibility rules as Explore. A session-scoped metric should be interpreted with session-compatible dimensions. User, session, and event scopes can produce confusing results when mixed. Some fields are unavailable together, and invalid combinations return an API error instead of a meaningful table.
Use the GA4 Data API metadata method to inspect available dimensions, metrics, descriptions, and compatibility information before constructing dynamic reports.
> callout note > A matching date range and field names do not guarantee identical totals. Time-zone settings, data freshness, thresholding, attribution differences, and row limits can change the result.
Exercise
Exercise
Which API fields represent the rows, values, and device filter in a Free-form exploration?
Show answer
Rows become dimensions, values become metrics, and the device filter becomes dimensionFilter.