Overview
TheGoogleAnalytics class provides integration with Google Analytics 4 (GA4) to fetch traffic metrics, engagement data, and conversion information for your content.
Installation
Authentication
The module uses Google service account credentials for authentication.Environment Variables
Service Account Setup
- Create a service account in Google Cloud Console
- Download the credentials JSON file
- Grant the service account “Viewer” access to your GA4 property
- Set the credentials path in your environment
Initialization
GA4 property ID. Defaults to
GA4_PROPERTY_ID environment variable.Path to service account credentials JSON. Defaults to
GA4_CREDENTIALS_PATH environment variable.Methods
get_top_pages
Get top performing pages by pageviews.Number of days to look back
Number of results to return
Filter pages by path (e.g., “/blog/”)
get_page_trends
Get traffic trends for a specific page over time.Page path to analyze
Number of days to analyze
Time granularity: “day” or “week”
get_conversions
Get conversion data by page.Number of days to look back
Filter pages by path
get_traffic_sources
Get traffic source breakdown for a page or entire site.Specific page path (None = all pages)
Number of days to analyze
get_declining_pages
Identify pages with declining traffic.Compare this many recent days vs previous period
Consider declining if drop exceeds this percentage
Filter pages by path
Example Usage
Source Code Reference
Location:data_sources/modules/google_analytics.py:24
The module uses the Google Analytics Data API v1beta with service account authentication. All requests are made using the BetaAnalyticsDataClient with read-only analytics scope.