Google Analytics API: Developer Guide

A practical guide to using the Google Analytics Data API for GA4. Covers authentication, common queries, and building custom reporting tools.

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, sessionMedium with metrics sessions, totalUsers
  • Content performance: dimension pagePath with metrics screenPageViews, averageSessionDuration
  • Conversion tracking: dimension eventName with metric eventCount, 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.google where 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.

Check your analytics from anywhere

On your morning commute. At a coffee shop. In a meeting. Pull up your analytics on any device and get instant answers.

  • Web dashboard on desktop & mobile
  • Discord bot for team channels
  • Slack integration for your workspace
  • MCP server for AI agents (Claude, Cursor)
See your traffic in 60 seconds →
ClawAnalytics mobile chat showing engagement rate breakdown with charts

How ClawAnalytics helps

Skip the dashboards. Get answers in seconds.

🔗
1

Connect GA4

One-click OAuth. Read-only access. Takes 30 seconds to link your Google Analytics property.

ClawAnalytics connections page showing Google Analytics properties linked
💬
2

Ask questions

Type in plain English. No query language, no filters, no date pickers. Just ask what you want to know.

ClawAnalytics chat interface with natural language query
📊
3

Get answers with charts

Instant responses with visualizations. Share charts with your team or export the data.

ClawAnalytics showing chart response to analytics query

See it in action

Ask a question. Get a chart. That simple.

ClawAnalytics Chat
ClawAnalytics chat interface showing a natural language analytics query with chart response

Works on web, Discord, and Slack. Also available as an MCP server for AI agents.

Leonidas Maliokas
"I used to open Google Analytics 5 times a day and still miss things. Now I get a summary every morning and ask follow-ups when something looks off. Takes 10 seconds instead of 10 minutes."

Leonidas Maliokas

Founder, Elanra Studios

🎮 5 games monitored 💼 3 businesses

Simple, honest pricing

Start free. Upgrade when you're ready.

Free

Try it out

$0 /month
  • 5 websites
  • 30 questions/month
  • Web dashboard
  • No credit card
Start Free

Website

For small businesses

$9 /month
  • 5 websites connected
  • 100 questions/month
  • Daily morning summary
  • Web dashboard + Discord
Get Started

Business

For agencies and portfolios

$79 /month
  • Unlimited websites
  • 2,000 questions/month
  • Everything in Pro
  • ✅ API access
  • ✅ MCP integration
  • ⭐ Priority support
Get Started

Stop opening dashboards.
Start asking.

Connect Google Analytics in 30 seconds. Get answers from the dashboard or Discord. Start free — no credit card needed.

Try it free — ask your first question
30-second setup Free plan available Cancel anytime

Got questions?

Which Google Analytics API should I use for GA4?
Use the Google Analytics Data API v1 (also called the GA4 Data API). The older Universal Analytics APIs (Reporting API v4, Management API) do not work with GA4 properties. The Data API supports all GA4 dimensions and metrics.
How do I authenticate with the Google Analytics API?
For server-to-server applications, create a service account in Google Cloud Console, download the JSON key file, and share your GA4 property with the service account email. For user-facing apps, use OAuth 2.0 with the analytics.readonly scope.
How does ClawAnalytics use the Google Analytics API?
ClawAnalytics connects to your GA4 property through the same Data API and translates natural language questions into API queries. Instead of writing code to pull reports, you ask a question and ClawAnalytics handles the API calls, pagination, and data formatting for you.

Related guides

More resources to help you get the most from your analytics.