The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Monitor Google Analytics data with Python

Monitoring Google Analytics data with Python allows for automated insights, reporting, and integration with other data tools. By leveraging the Google Analytics Reporting API (for GA3) or the Google Analytics Data API (for GA4), developers can extract and analyze web traffic metrics programmatically. Here’s a comprehensive guide on how to monitor Google Analytics data using Python, with a focus on GA4 (as Universal Analytics has been sunsetted).

Setting Up Google Analytics API Access

Before writing Python code, you’ll need access credentials to use the Google Analytics Data API.

Step 1: Create a Google Cloud Project

  1. Visit the Google Cloud Console.

  2. Create a new project.

  3. Navigate to APIs & Services > Library.

  4. Search for “Google Analytics Data API” and enable it.

Step 2: Create Service Account Credentials

  1. Go to APIs & Services > Credentials.

  2. Click Create Credentials > Service account.

  3. After creating it, go to the service account, select Add Key > JSON, and download the key file.

  4. In Google Analytics (admin panel), give the service account email Viewer access to the GA4 property.

Installing Required Python Libraries

bash
pip install --upgrade google-analytics-data

This installs the client library to interact with the GA4 Data API.

Authenticating the Service Account

python
from google.analytics.data_v1beta import BetaAnalyticsDataClient from google.analytics.data_v1beta.types import DateRange, Metric, Dimension, RunReportRequest from google.oauth2 import service_account # Load credentials credentials = service_account.Credentials.from_service_account_file("path/to/your/credentials.json") # Create the client client = BetaAnalyticsDataClient(credentials=credentials)

Querying GA4 Data with Python

To query GA4 data, you’ll need your property_id, which can be found in the GA4 admin section.

Example: Fetch Users and Sessions Over the Last 7 Days

python
property_id = "YOUR_GA4_PROPERTY_ID" request = RunReportRequest( property=f"properties/{property_id}", dimensions=[Dimension(name="date")], metrics=[Metric(name="sessions"), Metric(name="users")], date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], ) response = client.run_report(request) for row in response.rows: print(f"Date: {row.dimension_values[0].value}, Sessions: {row.metric_values[0].value}, Users: {row.metric_values[1].value}")

This basic snippet pulls daily sessions and users over the last week.

Creating Advanced Reports

You can customize the request to include other dimensions and metrics, such as:

  • Source/Medium

  • Country

  • Page path

  • Event name

Example: Top Pages by Views

python
request = RunReportRequest( property=f"properties/{property_id}", dimensions=[Dimension(name="pagePath")], metrics=[Metric(name="screenPageViews")], date_ranges=[DateRange(start_date="7daysAgo", end_date="today")], limit=10 ) response = client.run_report(request) for row in response.rows: print(f"Page: {row.dimension_values[0].value}, Views: {row.metric_values[0].value}")

Visualizing Data with Pandas and Matplotlib

You can integrate GA4 data with pandas for easier manipulation and plotting.

bash
pip install pandas matplotlib
python
import pandas as pd import matplotlib.pyplot as plt data = { "date": [], "sessions": [], "users": [] } for row in response.rows: data["date"].append(row.dimension_values[0].value) data["sessions"].append(int(row.metric_values[0].value)) data["users"].append(int(row.metric_values[1].value)) df = pd.DataFrame(data) df['date'] = pd.to_datetime(df['date']) plt.figure(figsize=(10, 5)) plt.plot(df['date'], df['sessions'], label='Sessions') plt.plot(df['date'], df['users'], label='Users') plt.title("Users and Sessions Over Time") plt.xlabel("Date") plt.ylabel("Count") plt.legend() plt.grid(True) plt.tight_layout() plt.show()

Automating GA Reports with Python Scripts

You can schedule your Python script to run daily using tools like:

  • cron (Linux/macOS)

  • Task Scheduler (Windows)

  • Cloud Functions or Cloud Run (GCP)

  • CI/CD tools like GitHub Actions

Store the report output in CSV, send via email, or push to a dashboard.

Export Data to CSV

python
df.to_csv("ga4_report.csv", index=False)

Handling API Limits and Pagination

GA4 API enforces quotas and response limits. To handle large datasets, use pagination or filter results incrementally.

python
request = RunReportRequest( property=f"properties/{property_id}", dimensions=[Dimension(name="country")], metrics=[Metric(name="activeUsers")], date_ranges=[DateRange(start_date="30daysAgo", end_date="today")], limit=100, offset=0 ) response = client.run_report(request)

Loop with increasing offsets if rowCount exceeds the limit.

Monitoring GA Events with Python

You can track events (custom or standard) such as:

  • Clicks

  • Scrolls

  • Form submissions

Use these event names as dimensions:

python
Dimension(name="eventName")

Example: Top events in last 30 days

python
request = RunReportRequest( property=f"properties/{property_id}", dimensions=[Dimension(name="eventName")], metrics=[Metric(name="eventCount")], date_ranges=[DateRange(start_date="30daysAgo", end_date="today")], limit=10 ) response = client.run_report(request) for row in response.rows: print(f"Event: {row.dimension_values[0].value}, Count: {row.metric_values[0].value}")

Integrating with Dashboards

You can push GA4 data into:

  • Google Sheets (via gspread)

  • Dashboards like Tableau, Power BI, or custom web apps

  • Data warehouses (e.g., BigQuery, Snowflake)

Example: Push to Google Sheets

bash
pip install gspread

Authenticate using a Google Service Account and write data to Sheets for internal dashboards.

Best Practices

  1. Secure Your Credentials: Never hardcode service account keys. Use environment variables or encrypted vaults.

  2. Handle API Errors: Wrap API calls in try/except blocks to manage downtime or invalid queries.

  3. Respect Quotas: Monitor usage and avoid hitting daily limits.

  4. Use Modular Code: Separate config, data fetching, and visualization into functions or classes.

  5. Automate Reporting: Integrate with CI/CD tools or cloud jobs for automatic data pulls.

Conclusion

Python is a powerful tool for monitoring and analyzing Google Analytics data. By using the Google Analytics Data API for GA4, developers can automate reporting, build insightful dashboards, and extract custom metrics tailored to their business needs. With the flexibility of Python libraries like Pandas and Matplotlib, GA data becomes not just accessible, but actionable.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About