Overview
The Analytics client provides methods to fetch organic traffic data from Google Analytics 4 (GA4) using the Analytics Data API v1. It focuses on organic search traffic analysis and landing page performance.Installation
Class: AnalyticsClient
getOrganicTrafficData()
Fetches comprehensive organic search traffic data for a specified period.propertyId- GA4 property ID (numeric, e.g.,123456789)accessToken- Valid Google OAuth access tokenstartDate- Start date inYYYY-MM-DDformatendDate- End date inYYYY-MM-DDformat
- Automatically calculates previous period for comparison
- Filters for
Organic Searchtraffic only - Returns daily trend data for charting
- Includes top landing pages with engagement metrics
ReportGenerationError with:
401- Authentication expired, requires reauthentication403- Insufficient permissions or quota exceeded429- Rate limit exceeded (retryable)500+- Server errors (retryable)
getTopLandingPages()
Fetches top landing pages from organic search traffic.propertyId- GA4 property IDaccessToken- Valid Google OAuth access tokenstartDate- Start date inYYYY-MM-DDformatendDate- End date inYYYY-MM-DDformatlimit- Number of pages to return (default: 10)
getTrafficSources()
Breaks down traffic by channel grouping.getConversionData()
Fetches conversion metrics including organic search conversions.getAccessibleProperties()
Lists all GA4 properties accessible to the user.validatePropertyAccess()
Validates that a property ID is accessible with current credentials.true if property is accessible, false otherwise
Example:
Static Methods
formatPropertyId()
Cleans property ID by removing theproperties/ prefix if present.
GA4 Metrics Explained
Sessions vs Users
- Sessions: Total number of sessions (including returning users)
- Active Users: Unique users who had an engaged session
Engagement Metrics
- Bounce Rate: Percentage of sessions without engagement (GA4 definition)
- Average Session Duration: Average time users spend per session (seconds)
Traffic Filtering
All methods filter bysessionDefaultChannelGroup = 'Organic Search' to focus on SEO performance.
Channel Groups:
- Organic Search
- Direct
- Referral
- Organic Social
- Paid Search
- Display
Error Handling
All methods use automatic retry with exponential backoff:401- Access token expired, user needs to reauthenticate403- Property access denied or API not enabled429- Quota exceeded (25,000 requests per day)500- GA4 API temporarily unavailable
Rate Limiting
Google Analytics Data API quotas:- Requests per day: 25,000 per project
- Requests per 100 seconds: 2,000 per project
- Concurrent requests: 10 per project
- Batch requests using
Promise.all()for parallel data fetching - Cache results for frequently accessed data
- Use date ranges efficiently (max 93 days recommended)
- Monitor quota usage in Google Cloud Console
Authentication
Requires Google OAuth 2.0 with the following scope:Date Formats
All date parameters use ISO 8601 format:YYYY-MM-DD
Relative Dates (alternative):
todayyesterday7daysAgo30daysAgo
Complete Usage Example
GA4 vs Universal Analytics
This client uses GA4 (Google Analytics 4) exclusively. Key differences:| Feature | GA4 | Universal Analytics |
|---|---|---|
| User Metric | Active Users | Users |
| Session Definition | Engagement-based | Time-based |
| Bounce Rate | Not engaged sessions | Single-page sessions |
| API | Analytics Data API v1 | Reporting API v4 |
| Property ID | Numeric (123456789) | UA-XXXXXX-X |