Skip to content

TudorAndrei/google-analytics-cli

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Google Analytics CLI (Experimental)

Python 3.10+ YouTube Video Views

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.

Commands 🛠️

The CLI uses the Google Analytics Admin API and Google Analytics Data API to provide a set of commands.

Retrieve account and property information 🟠

  • 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.

Run core reports 📙

  • report: Runs a Google Analytics report using the Data API.
  • custom-dimensions-metrics: Retrieves the custom dimensions and metrics for a specific property.

Run realtime reports ⏳

  • realtime-report: Runs a Google Analytics realtime report using the Data API.

Setup instructions 🔧

✨ Watch the Google Analytics CLI Setup Tutorial on YouTube for a step-by-step walkthrough of these instructions.

Watch the video

Setup involves the following steps:

  1. Configure Python.
  2. Configure credentials for Google Analytics.
  3. Add credentials to a .env file.

Configure Python 🐍

Install pipx.

Enable APIs in your project ✅

Follow the instructions to enable the following APIs in your Google Cloud project:

Configure credentials 🔑

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]

Configure .env

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-id

Optional: load .env automatically with direnv

If you use direnv, this repo includes .envrc with:

dotenv

Enable it once in the project directory:

direnv allow

Try it out 🥼

Install and run:

pip install -e .
analytics-cli --help

Examples:

  • 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

Contributing ✨

Contributions welcome! See the Contributing Guide.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 100.0%