This repo contains a local CLI that interacts with the Google Analytics Admin and Data APIs.
GitHub: https://github.com/TudorAndrei/google-analytics-cli
Join the discussion and ask questions on Discord.
The CLI uses the Google Analytics Admin API and Google Analytics Data API to provide a set of commands.
account-summaries: Retrieves information about the user's Google Analytics accounts and properties.property-details: Returns details about a property.google-ads-links: Returns a list of links to Google Ads accounts for a property.property-annotations: Returns annotations for a property.
report: Runs a Google Analytics report using the Data API.custom-dimensions-metrics: Retrieves the custom dimensions and metrics for a specific property.
realtime-report: Runs a Google Analytics realtime report using the Data API.
✨ Watch the Google Analytics CLI Setup Tutorial on YouTube for a step-by-step walkthrough of these instructions.
Setup involves the following steps:
- Configure Python.
- Configure credentials for Google Analytics.
- Add credentials to a
.envfile.
Follow the instructions to enable the following APIs in your Google Cloud project:
Configure your Application Default Credentials (ADC). Make sure the credentials are for a user with access to your Google Analytics accounts or properties.
Credentials must include the Google Analytics read-only scope:
https://www.googleapis.com/auth/analytics.readonly
Check out Manage OAuth Clients for how to create an OAuth client.
Here are some sample gcloud commands you might find useful:
-
Set up ADC using user credentials and an OAuth desktop or web client after downloading the client JSON to
YOUR_CLIENT_JSON_FILE.gcloud auth application-default login \ --scopes https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform \ --client-id-file=YOUR_CLIENT_JSON_FILE
-
Set up ADC using service account impersonation.
gcloud auth application-default login \ --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \ --scopes=https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/cloud-platform
When the gcloud auth application-default command completes, copy the
PATH_TO_CREDENTIALS_JSON file location printed to the console in the
following message. You'll need this for the next step!
Credentials saved to file: [PATH_TO_CREDENTIALS_JSON]
Copy .env.example to .env in the project root (or in your current working
directory), and set the path to your credentials JSON:
GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to/credentials.json
GOOGLE_PROJECT_ID=your-project-idIf you use direnv, this repo includes .envrc with:
dotenvEnable it once in the project directory:
direnv allowInstall and run:
pip install -e .
analytics-cli --helpExamples:
-
List account summaries:
analytics-cli account-summaries
-
Get property details:
analytics-cli property-details 123456789
-
Run a core report:
analytics-cli report 123456789 \ --date-ranges '[{"start_date":"7daysAgo","end_date":"yesterday"}]' \ --dimension eventName \ --metric eventCount -
Run a realtime report:
analytics-cli realtime-report 123456789 \ --dimension country \ --metric activeUsers
Contributions welcome! See the Contributing Guide.
