Your product manager wants a Slack bot that posts daily traffic stats every morning at 9 AM. Your client wants a custom dashboard embedded in their admin panel. Your CEO wants a weekly PDF report comparing this quarter to last. All of these start with one thing: the Google Analytics API.
Once you know how to pull data programmatically, analytics stops being a dashboard you visit and becomes a data source you build on.
Why This Matters
Dashboards do not scale. Manually checking GA4 every morning works for one person. When five teams need different views of the same data, you need automation.
Custom reporting is a superpower. The GA4 interface forces you into its UI patterns. The API gives you raw data you can shape however you want. Combine it with CRM data, financial data, or product usage data to get a complete picture.
Integrations unlock workflows. Alert your team in Slack when traffic drops 20%. Populate a client-facing dashboard. Feed data into a machine learning model that predicts conversion likelihood. The API makes all of this possible.
It is free. The GA4 Data API has generous quotas. Most applications will never hit the limits. You get up to 200,000 requests per day per project and 10 concurrent requests.
How to Do It
Step 1: Set up Google Cloud credentials.
Create a project in Google Cloud Console. Enable the “Google Analytics Data API.” Create a service account and download the JSON credentials file. Then share your GA4 property with the service account email address (give it Viewer access in GA4 Admin).
Step 2: Install the client library.
For Python:
pip install google-analytics-data
For Node.js:
npm install @google-analytics/data
Step 3: Make your first request.
Here is a minimal Python example that pulls sessions by source for the last 7 days:
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
RunReportRequest, DateRange, Metric, Dimension
)
client = BetaAnalyticsDataClient()
request = RunReportRequest(
property=f"properties/YOUR_PROPERTY_ID",
dimensions=[Dimension(name="sessionSource")],
metrics=[Metric(name="sessions")],
date_ranges=[DateRange(start_date="7daysAgo", end_date="today")],
)
response = client.run_report(request)
for row in response.rows:
source = row.dimension_values[0].value
sessions = row.metric_values[0].value
print(f"{source}: {sessions} sessions")
Step 4: Understand dimensions and metrics.
Dimensions are attributes (country, page path, source). Metrics are numbers (sessions, users, conversions). You can combine up to 9 dimensions and 10 metrics per request.
Common combinations:
- Traffic analysis: dimensions
sessionSource,sessionMediumwith metricssessions,totalUsers - Content performance: dimension
pagePathwith metricsscreenPageViews,averageSessionDuration - Conversion tracking: dimension
eventNamewith metriceventCount, filtered to conversion events
Step 5: Handle pagination and quotas.
For large datasets, set limit and offset in your request. Check response.row_count to know if more data exists. Respect rate limits by adding retries with exponential backoff.
The Easier Way
Building API integrations takes time. If you need quick answers from your GA4 data without writing code, ClawAnalytics provides an interface where you ask questions directly:
- “What were our top traffic sources last month?”
- “Compare mobile vs desktop conversion rates for Q1”
- “Which landing pages have the highest bounce rate?”
For developers, this is useful for prototyping queries before implementing them in code, or for giving non-technical teammates access to the same data without building an internal tool.
Quick Wins
- Use the GA4 Query Explorer. Google provides a web-based tool at
ga-dev-tools.googlewhere you can test API queries without writing code. Use it to validate dimensions and metrics before coding. - Cache aggressively. Analytics data for past dates does not change. Cache completed date ranges and only query the current day fresh.
- Start with service accounts. OAuth 2.0 is more complex and only needed for multi-user apps. For internal tools, service accounts are simpler and do not expire.
- Monitor your quotas. Check usage in Google Cloud Console under APIs and Services. Set up alerts before you hit limits rather than after.