google-analytics-cli
Google Analytics CLI for AI agents (and humans). Run custom reports with flexible dimensions and date ranges, monitor realtime active users, manage custom metrics from the GA4 API, and more.
Works with: OpenClaw, Claude Code, Cursor, Codex, and any agent that can run shell commands.
Installation
Tell your AI agent (e.g. OpenClaw):
Install this CLI and skills from https://github.com/Bin-Huang/google-analytics-cli
Or install manually:
npm install -g google-analytics-cli
# Add skills for AI agents (Claude Code, Cursor, Codex, etc.)
npx skills add Bin-Huang/google-analytics-cli
Or run directly: npx google-analytics-cli --help
For development:
pnpm install
pnpm build
How it works
Built on Google's official APIs. Handles service account authentication and request signing. Every command outputs structured JSON to stdout, ready for agents to parse without extra processing.
- GA4 Admin API — account/property management, data streams, key events, change history, access reports
- GA4 Data API — standard/pivot/batch reports, realtime, audience exports, metadata
Under the hood it uses the official Node.js client libraries @google-analytics/admin and @google-analytics/data. All API responses are passed through as JSON — no transformation or aggregation.
Setup
Step 1: Enable the Google Analytics APIs
Go to the Google Cloud Console and enable both APIs for your project:
If you don't have a project yet, create one first.
Step 2: Create a Service Account
- Go to IAM & Admin > Service Accounts in the same project.
- Click Create Service Account, give it a name (e.g.
analytics-reader), and click Done.
- Click on the newly created Service Account, go to the Keys tab.
- Click Add Key > Create new key > JSON, and download the key file.
Step 3: Place the credentials file
Choose one of these options:
# Option A: Default path (recommended)
mkdir -p ~/.config/google-analytics-cli
cp ~/Downloads/your-key-file.json ~/.config/google-analytics-cli/credentials.json
# Option B: Environment variable
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-key-file.json"
# Option C: Pass per command
google-analytics-cli accounts --credentials /path/to/your-key-file.json
Credentials are resolved in this order:
--credentials <path> flag
GOOGLE_APPLICATION_CREDENTIALS env var
~/.config/google-analytics-cli/credentials.json (auto-detected)
- gcloud Application Default Credentials
Step 4: Grant access in Google Analytics
- Open Google Analytics.
- Go to Admin (gear icon at bottom-left).
- Under Account or Property, click Access Management.
- Click + > Add users.
- Enter the Service Account email (find it in your key file's
client_email field, e.g. [email protected]).
- Assign the Viewer role (read-only access to all properties under the account).
- Click Add.
Adding at the Account level grants access to all properties under that account. You can also add at the Property level for more granular control.
Alternative: gcloud ADC (for local development)
If you prefer not to use a Service Account, you can authenticate with your own Google account:
gcloud auth application-default login \
--scopes="https://www.googleapis.com/auth/analytics.readonly"
This uses your personal Google account's Analytics access. Good for local development, not recommended for automation.
Usage
All commands output pretty-printed JSON by default. Use --format compact for compact single-line JSON.
You can pass a property ID as an argument, via --property, or set the GA_PROPERTY_ID environment variable. Both raw numbers and properties/ prefixed IDs are accepted (e.g. 123456789 or properties/123456789).
export GA_PROPERTY_ID=123456789
accounts
List all GA4 accounts and their properties.
google-analytics-cli accounts
property
Get details about a specific property.
google-analytics-cli property 123456789
ads-links
List Google Ads links for a property.
google-analytics-cli ads-links 123456789
annotations
List annotations (notes) for a property. Uses the Admin API v1alpha.
google-analytics-cli annotations 123456789
properties
List properties for an account.