home / skills / cdeistopened / opened-vault / ga4 - archived 2026-01-29

ga4 - archived 2026-01-29 skill

/.claude/skills/_archived/ga4 - archived 2026-01-29

This skill queries GA4 data via the Analytics Data API to fetch pages, sources, sessions, and conversions for analytics insights.

npx playbooks add skill cdeistopened/opened-vault --skill ga4 - archived 2026-01-29

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
4.4 KB
---
name: ga4
description: Query Google Analytics 4 (GA4) data via the Analytics Data API. Use when you need to pull website analytics like top pages, traffic sources, user counts, sessions, conversions, or any GA4 metrics/dimensions.
---

# GA4 - Google Analytics 4 Data API

Query GA4 properties for analytics data: page views, sessions, users, traffic sources, conversions, and more.

## Setup

### Required Environment Variables

Add to `.env`:
```
GA4_PROPERTY_ID=your-property-id
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REFRESH_TOKEN=your-refresh-token
```

### Setup Steps

1. **Enable API**: [Google Analytics Data API](https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com)
2. **Find Property ID**: GA4 Admin → Property Settings → Property ID (numeric)
3. **Create OAuth**: Google Cloud Console → APIs & Services → Credentials
4. **Get Refresh Token**: Run initial auth flow

## Commands

### Top Pages (by pageviews)
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric screenPageViews \
  --dimension pagePath \
  --limit 30
```

### Top Pages with Sessions & Users
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metrics screenPageViews,sessions,totalUsers \
  --dimension pagePath \
  --limit 20
```

### Traffic Sources
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric sessions \
  --dimension sessionSource \
  --limit 20
```

### Landing Pages
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric sessions \
  --dimension landingPage \
  --limit 30
```

### Custom Date Range
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric sessions \
  --dimension pagePath \
  --start 2026-01-01 \
  --end 2026-01-28
```

### Filter by Page Path (Blog Posts)
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric screenPageViews \
  --dimension pagePath \
  --filter "pagePath=~/blog/"
```

### Track Specific Article Performance
```bash
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metrics screenPageViews,sessions,totalUsers,bounceRate \
  --dimension date \
  --filter "pagePath=/blog/waldorf-vs-montessori" \
  --start 2026-01-28 \
  --end 2026-02-28
```

## Available Metrics

| Metric | Description |
|--------|-------------|
| `screenPageViews` | Total page views |
| `sessions` | Number of sessions |
| `totalUsers` | Total unique users |
| `newUsers` | First-time visitors |
| `activeUsers` | Users with engagement |
| `bounceRate` | Single-page session rate |
| `averageSessionDuration` | Avg time on site |
| `conversions` | Goal completions |
| `eventCount` | Total events |

## Available Dimensions

| Dimension | Description |
|-----------|-------------|
| `pagePath` | URL path |
| `pageTitle` | Page title |
| `landingPage` | Entry page |
| `sessionSource` | Traffic source |
| `sessionMedium` | Traffic medium |
| `sessionCampaignName` | Campaign name |
| `country` | Visitor country |
| `city` | Visitor city |
| `deviceCategory` | Desktop/Mobile/Tablet |
| `browser` | Browser name |
| `date` | Date |

## Output Formats

```bash
# Default: Table format
python3 scripts/ga4_query.py --metric sessions --dimension pagePath

# JSON output
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --json

# CSV output
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --csv
```

## SEO Content Tracking

### After Publishing New Content

Track article performance over time:

```bash
# Week 1: Check initial traffic
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metrics screenPageViews,sessions,bounceRate \
  --dimension date \
  --filter "pagePath=/blog/waldorf-vs-montessori" \
  --start 2026-01-28 \
  --end 2026-02-04

# Compare to similar content
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric screenPageViews \
  --dimension pagePath \
  --filter "pagePath=~/blog/" \
  --limit 20
```

### Monthly SEO Report

```bash
# Top organic landing pages
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metrics sessions,totalUsers,bounceRate \
  --dimension landingPage \
  --filter "sessionSource=google" \
  --limit 30

# Traffic by source
python3 .claude/skills/ga4/scripts/ga4_query.py \
  --metric sessions \
  --dimensions sessionSource,sessionMedium \
  --limit 20
```

## Integration Notes

- Credentials shared with GSC skill
- Data is near real-time (unlike GSC's 3-day delay)
- Use with `seo-content-production` skill for performance tracking

Overview

This skill queries Google Analytics 4 properties through the Analytics Data API to pull site metrics like pageviews, sessions, users, conversions, and traffic sources. It provides a lightweight CLI to request metrics and dimensions, filter results, and export as table, JSON, or CSV. Use it to inspect page-level performance, traffic breakdowns, and custom date ranges for SEO or product analysis.

How this skill works

The skill authenticates with OAuth using environment variables (property ID, client ID, client secret, refresh token) and calls the Analytics Data API to fetch metric/dimension reports. You specify metrics, dimensions, date ranges, limits, and optional filters on the CLI; the script returns tabular output or serialized JSON/CSV for downstream processing. Common presets include top pages, landing pages, traffic sources, and article-level time series.

When to use it

  • Check top pages by pageviews or sessions for a given date range
  • Break down traffic by source/medium or campaign
  • Inspect landing page performance and conversion counts
  • Track a specific article’s daily performance after publishing
  • Export analytics data for reports or automated pipelines

Best practices

  • Store GA4_PROPERTY_ID and OAuth credentials in environment variables and never commit them
  • Use filters to limit results to blog paths or specific pages to reduce API usage
  • Request only the metrics and dimensions you need to minimize response size
  • Compare consistent date ranges when measuring content performance
  • Export JSON or CSV when integrating into reporting or automation workflows

Example use cases

  • List top 30 pages by screenPageViews to prioritize content updates
  • Pull sessions by sessionSource and sessionMedium to evaluate acquisition channels
  • Query daily pageviews and bounceRate for a new article during week 1
  • Generate monthly organic landing page report filtered to sessionSource=google
  • Filter results to /blog/ paths to compare similar content performance

FAQ

What credentials are required?

You need GA4 property ID plus OAuth client ID, client secret, and a refresh token set as environment variables.

Can I export results for automated reports?

Yes — the CLI supports table, JSON, and CSV output formats for downstream automation.