The Google Analytics Data API returns aggregated GA4 reports for a numeric property ID. Use it for scheduled reports, Sheets exports, or a controlled reporting interface. Choose a reporting method first, then authenticate and verify the returned metrics against a report with the same scope. Data API reporting basics
Authenticate the reporting identity
Enable the Google Analytics Data API in a Google Cloud project. Grant the querying identity access in GA4 Property Access Management. Use OAuth for user-authorized access or a service account for a scheduled backend. Prefer read-only scope for reports and managed or short-lived credentials when available. A measurement ID beginning with G- identifies a stream for tagging. A Data API request uses the numeric property ID. Official quickstart, OAuth scopes for runReport
Make a first reporting request
curl --fail-with-body -X POST \
"https://analyticsdata.googleapis.com/v1beta/properties/${GA4_PROPERTY_ID}:runReport" \
-H "Authorization: Bearer ${GA4_ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"dateRanges": [{"startDate":"7daysAgo","endDate":"yesterday"}],
"dimensions": [{"name":"sessionDefaultChannelGroup"}],
"metrics": [{"name":"sessions"},{"name":"keyEvents"}],
"returnPropertyQuota": true
}'
Provide the numeric property ID and a short-lived OAuth access token in the environment. Treat dimensionHeaders and metricHeaders as the schema and match each row's values by position. The API returns metric values as strings in the JSON representation. Keep the property timezone and date boundaries in the output. Query a separate aggregate when you need unduplicated active-user totals. runReport reference
Design for automation
- Use
getMetadatato identify available dimensions and metrics andcheckCompatibilitybefore deploying a new combination. ChooserunRealtimeReportorrunFunnelReportwhen the question requires those methods. - For a grouped report, request explicit ordering and page with
offsetandlimituntil the returned row count matches expectations. Inspect sampling, thresholding, and restriction metadata before sharing results. - Fix identity access for permission failures. Apply bounded backoff for eligible rate-limit or transient errors, preserve the last successful report, and alert the operator on repeated failure. Inspect
returnPropertyQuotato see remaining tokens. Data API quotas, Response metadata
See the Google Analytics overview, n8n reporting walkthrough, and GA4 automation services.