google-analytics-cli
Google Analytics CLI & Skills 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: Choose a project and enable the APIs
- Open the Google Cloud Console and select a project. If you do not have a project, create one.
- Enable both APIs in the API Library. The CLI needs both:
Step 2: Create a service account and download a JSON key
- 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 the new service account and open the Keys tab.
- Click Add Key > Create new key > JSON. The browser downloads the key file.
[!NOTE]
Key creation blocked? Your organization can block service account key creation with the constraints/iam.managed.disableServiceAccountKeyCreation policy. Organizations created on or after May 3, 2024 enforce this policy by default. See Secure-by-default organizations and Create and delete service account keys.
To fix this, ask your organization admin to override the policy for this project. Or use Application Default Credentials instead of a key.
Step 3: Place the credentials file
The CLI looks for credentials in this order and uses the first one it finds:
--credentials <path> flag
--profile <name> flag (or GA_CLI_PROFILE env var) → ~/.config/google-analytics-cli/profiles/<name>.json (see Managing multiple GA accounts)
GOOGLE_APPLICATION_CREDENTIALS env var
~/.config/google-analytics-cli/credentials.json (default path)
- gcloud Application Default Credentials (see below)
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 and --profile cannot be used together; --credentials overrides GA_CLI_PROFILE.
Step 4: Grant access in Google Analytics
The service account needs a role on each Google Analytics account or property. You must be an Administrator of the account or property to add users to it.