📊 GA4 CLI

Google Analytics 4 Data Retrieval Tool

🚀 Quick Installation

npm install -g kanmi-ga4-cli

Or install as part of the complete KanmiLabs suite:

npm install -g kanmi-suite-cli

✨ Features

🔐 OAuth2 Authentication

Secure authentication with Google Analytics using industry-standard OAuth2 flow.

📊 Multiple Report Types

Generate custom reports, comparisons, top events analysis, and page performance reports.

📁 Multiple Output Formats

Export data as Table, JSON, Markdown, or CSV formats for different use cases.

🔄 Date Range Comparisons

Compare current performance against previous periods with automatic calculations.

💾 File Export

Save reports directly to files with customizable naming and location.

🎯 Flexible Metrics

Use any GA4 metric and dimension combination for custom analysis.

🔧 Setup Requirements

Prerequisites

Google Cloud Setup

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Google Analytics Data API
  4. Enable the Google Analytics Admin API (optional, for property listing)
  5. Create OAuth2 credentials:
    • Go to APIs & Services → Credentials
    • Create credentials → OAuth client ID
    • Application type: Desktop application
    • Download the JSON file
⚠️ Important: Save your credentials file as credentials.json in your project directory or specify the path using environment variables.

📖 Usage

Available Commands

Command Description Options
ga4-cli auth Authenticate with Google Analytics --reset
ga4-cli properties List available GA4 properties --format
ga4-cli report Generate custom reports --property, --metrics, --dimensions, --date-range
ga4-cli compare Compare time periods --property, --current, --previous
ga4-cli top-events Analyze top events --property, --limit, --date-range
ga4-cli top-pages Analyze top pages --property, --limit, --date-range

Basic Examples

1. Authentication

ga4-cli auth
Opens browser for OAuth flow and saves authentication tokens locally.

2. List Properties

ga4-cli properties --format table
Lists all GA4 properties you have access to in a formatted table.

3. Generate Basic Report

ga4-cli report \ --property "123456789" \ --metrics "sessions,pageviews,users" \ --dimensions "date" \ --date-range "last-7-days" \ --format json

4. Compare Time Periods

ga4-cli compare \ --property "123456789" \ --current "last-7-days" \ --previous "previous-7-days" \ --format table

5. Top Events Analysis

ga4-cli top-events \ --property "123456789" \ --limit 10 \ --date-range "last-30-days" \ --output events-report.json

6. Export to File

ga4-cli report \ --property "123456789" \ --metrics "sessions,bounceRate,avgSessionDuration" \ --dimensions "country,city" \ --date-range "last-month" \ --format csv \ --output analytics-export.csv

⚙️ Configuration

Environment Variables

# Optional: Custom credentials path GA4_CREDENTIALS_PATH=/path/to/credentials.json # Optional: Custom token storage path GA4_TOKENS_PATH=/path/to/tokens.json # Optional: Default property ID GA4_DEFAULT_PROPERTY=123456789

Available Metrics & Dimensions

GA4 CLI supports all standard GA4 metrics and dimensions. Common examples:

Popular Metrics:

Popular Dimensions:

🔗 Related Tools

📞 Support