Sub topic 13
Pulling the Same Numbers with the Data API
This chapter assumes that you have a GA4 property with collected data, know its property ID, and have basic familiarity with JSON and HTTP. You also need credentials authorized to use the Google Analytics Data API.
The Data API lets you request report data programmatically. The runReport method uses the same basic ideas as the GA4 interface:
- A dimension describes how rows are grouped, such as
pagePathoreventName. - A metric is a number measured for each row, such as
screenPageViewsoreventCount. - A date range limits the reporting period.
- A filter keeps only rows that match a condition.
- An order controls how rows are sorted.
The basic flow is:
Querying pages and engagement
To reproduce a Pages and screens style report, group the data by pagePath and request page views and engagement metrics. The endpoint includes your property ID:
POST https://analyticsdata.googleapis.com/v1beta/properties/PROPERTY_ID:runReport
The request body can look like this:
{
"dateRanges": [{ "startDate": "28daysAgo", "endDate": "yesterday" }],
"dimensions": [{ "name": "pagePath" }],
"metrics": [
{ "name": "screenPageViews" },
{ "name": "engagedSessions" },
{ "name": "engagementRate" },
{ "name": "userEngagementDuration" }
],
"orderBys": [{ "metric": { "metricName": "screenPageViews" }, "desc": true }],
"limit": 10
}This asks for the ten pages with the most screenPageViews. Each returned row contains the page path and the requested metric values. userEngagementDuration is reported in seconds. You can divide it by 60 when displaying minutes.
The API returns metric values as strings, so your application should convert numeric values before calculating totals or drawing charts. Keep the date range, dimensions, and metric definitions visible in your application so another person can reproduce the query.
Querying events
To reproduce a row in the Events report, use eventName as the dimension. A dimension filter can restrict the response to one event:
{
"dateRanges": [{ "startDate": "7daysAgo", "endDate": "yesterday" }],
"dimensions": [{ "name": "eventName" }],
"metrics": [
{ "name": "eventCount" },
{ "name": "totalUsers" }
],
"dimensionFilter": {
"filter": {
"fieldName": "eventName",
"stringFilter": {
"matchType": "EXACT",
"value": "file_download"
}
}
}
}eventCount counts occurrences. totalUsers counts distinct users associated with the event during the selected period. These values answer different questions: one user can trigger the same event many times.
Comparing API results with the interface
The API and the GA4 interface may show different-looking results when their settings differ. Check all of these before comparing numbers:
- The date range and time zone.
- The property and data stream.
- The dimension used for grouping.
- Filters, comparisons, and search terms.
- Report limits and sampling or thresholding behavior.
- Whether the interface uses a default metric that your request omitted.
The API is useful for scheduled exports, custom dashboards, and repeatable analysis. It does not remove GA4's data-collection limitations. Missing events, consent-mode effects, attribution rules, and reporting delays can affect both the API and the interface.
Exercise
You need the number of newsletter_signup events and the users who triggered them during the last seven complete days. Which dimension and metrics should your runReport request use?
Show answer
Use the eventName dimension and the eventCount and totalUsers metrics. Add a date range from 7daysAgo to yesterday, then filter eventName exactly to newsletter_signup.