Google Analytics 4 API Setup
This guide walks you through setting up automated GA4 data fetching.
Prerequisites
- Same service account from GSC setup (or create new one)
- GA4 property with data
Step 1: Enable the API
- Go to Google Cloud Console
- Select the same project used for GSC
- Go to APIs & Services → Library
- Search for “Google Analytics Data API”
- Click Enable
Step 2: Get Your GA4 Property ID
- Go to Google Analytics
- Select your 360tft property
- Click Admin (gear icon)
- Under Property Settings, find Property ID
- Copy the number (e.g.,
123456789)
Step 3: Grant Access to Service Account
- In GA4, go to Admin → Account Access Management
- Click + → Add users
- Enter the service account email (same as GSC)
- Set role to Viewer
- Click Add
Step 4: Add GitHub Secrets
Add these secrets to your GitHub repo:
| Secret Name | Value |
|---|---|
GSC_SERVICE_ACCOUNT |
Already done ✓ |
GA4_PROPERTY_ID |
Your GA4 property ID (e.g., 123456789) |
Step 5: Test Locally
cd scripts/ga4
# Copy service account from GSC folder
cp ../gsc/service_account.json .
# Install dependencies
pip install google-analytics-data
# Run with property ID
python fetch_ga4_data.py --property YOUR_PROPERTY_ID
What Data Is Collected
Overview Metrics
- Sessions, Users, New Users
- Page Views
- Bounce Rate, Engagement Rate
- Average Session Duration
Breakdowns
- Top Pages: Most viewed pages with engagement metrics
- Landing Pages: Entry pages with bounce rates
- Traffic Sources: Where visitors come from
- Devices: Mobile vs Desktop vs Tablet
- Countries: Geographic distribution
Insights Generated
- High bounce rate pages (need improvement)
- Low engagement pages (need more content)
- Top performing content
- Traffic source analysis
Output
Reports saved to:
scripts/ga4/data/ga4_latest.json- Most recentscripts/ga4/data/ga4_report_YYYY-MM-DD.json- Historical
Combined with GSC
When both GSC and GA4 are set up, you get:
| GSC | GA4 |
|---|---|
| Search queries | User behavior |
| Rankings | Bounce rates |
| Impressions | Session duration |
| Click-through rates | Traffic sources |
| Search performance | Engagement metrics |
Together: Full picture of how users find your site AND what they do when they arrive.