What You’ll Get
- Page views and sessions by article
- Average engagement time and scroll depth
- Bounce rate and engagement rate
- Conversion tracking (sign-ups, trials)
- Traffic sources (organic, direct, referral)
- Declining content alerts
Prerequisites
- Admin access to your Google Analytics 4 property
- A Google Cloud project (or create a new one)
Setup Instructions
Enable the Google Analytics Data API
- Go to the Google Cloud Console
- Select or create a project for your SEO Machine integration
- Navigate to APIs & Services > Library
- Search for “Google Analytics Data API”
- Click on the API and click Enable
Create a Service Account
- In Google Cloud Console, go to APIs & Services > Credentials
- Click Create Credentials and select Service Account
- Fill in the service account details:
- Name:
seo-machine-ga4(or any descriptive name) - Description: “Service account for SEO Machine GA4 integration”
- Name:
- Click Create and Continue
- Skip the optional steps (click Continue then Done)
Download the Service Account Key
- Find your newly created service account in the Credentials page
- Click on the service account email
- Go to the Keys tab
- Click Add Key > Create new key
- Select JSON as the key type
- Click Create
- The JSON key file will automatically download
Grant Access to Your GA4 Property
- Go to Google Analytics
- Select your GA4 property
- Click Admin (gear icon in bottom left)
- Under Property, click Property access management
- Click the + button and select Add users
- Enter the service account email (looks like
[email protected]) - Select the role Viewer (read-only access)
- Uncheck “Notify new users by email”
- Click Add
Get Your GA4 Property ID
- In Google Analytics, click Admin
- Under Property, click Property Settings
- Copy your Property ID (format:
123456789)
Available Methods
TheGoogleAnalytics class provides these methods:
Get Top Pages
Fetch top performing pages by pageviews:Get Page Trends
Analyze traffic trends for a specific page:timeline: List of pageviews by periodtrend_direction: “rising”, “declining”, or “stable”trend_percent: Percentage changetotal_pageviews: Sum across all periods
Get Conversions
Track conversion data by page:Get Traffic Sources
Breakdown of traffic sources:Find Declining Pages
Identify content losing traffic:Data Returned
Each page data object includes:Integration with Performance Agent
The Performance Agent automatically uses GA4 data:- Fetches data from GA4
- Identifies declining content
- Finds high-traffic articles needing updates
- Prioritizes next actions
- Creates recommendations report
Troubleshooting
Error: Permission denied
Error: Permission denied
- Verify the service account email is added to your GA4 property with Viewer permissions
- Double-check you’re using the correct Property ID in
.env - Wait a few minutes after adding the service account for changes to propagate
Error: API not enabled
Error: API not enabled
- Make sure you enabled the Google Analytics Data API in Google Cloud Console
- Wait a few minutes after enabling for changes to take effect
- Verify you’re using the correct Google Cloud project
Error: Invalid credentials
Error: Invalid credentials
- Verify the JSON key file is in the correct location:
credentials/ga4-credentials.json - Ensure the file hasn’t been corrupted or modified
- Check that the service account hasn’t been deleted in Google Cloud Console
Error: No data returned
Error: No data returned
- Check date ranges (some properties have data delays)
- Verify the Property ID is correct
- Ensure your property has data for the requested time period
- Try a longer time range (e.g., 90 days instead of 7)
Testing Your Integration
Verify GA4 is working:What’s Next
Google Search Console
Add search performance data to complement GA4 traffic metrics
DataForSEO
Enable competitive keyword research and SERP analysis