Skip to main content

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.

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

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.

Release files for analytics-cli 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for analytics-cli 0.1.1
File Size Uploaded
analytics_cli-0.1.1.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for analytics-cli 0.1.1
File Interpreter ABI Platform
analytics_cli-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.6 kB

Release files / analytics_cli-0.1.1.tar.gz

Download URL analytics_cli-0.1.1.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
30101a3fb3c87a5f9c68526747731da88ebe057682b5f9076287f1e3166eb8eb
BLAKE2b-256 checksum
How to use checksums
2ea67fea790662483f37bf599dfd59d036f79f161b042086806d33ddf0632c00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.11","id":"xantusia","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / analytics_cli-0.1.1-py3-none-any.whl

Download URL analytics_cli-0.1.1-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0072639bbf91289cd2dca5846efd417f5dc0fc8c759e1ed1253c0b38730bc1c
BLAKE2b-256 checksum
How to use checksums
06700da478f8e5d608495782fd7b7af20b598a56beb409b58775d08305ca2b76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"25.11","id":"xantusia","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page