How to Extract Data from Google Analytics Using Python
Pulling data directly from the Google Analytics interface is fine for a quick look, but it quickly becomes a repetitive, manual chore. If you want to automate your reporting, combine GA data with other sources, or perform more advanced analysis, you'll need a better way to get your data out. This tutorial will walk you through, step-by-step, how to use Python to connect directly to the Google Analytics API and extract your data for powerful, custom reporting.
Why Use Python with Google Analytics?
While the Google Analytics dashboard is powerful, it has its limits. Exporting CSVs every week is time-consuming and prone to errors. Using Python to programmatically access your GA data opens up a world of new possibilities:
- Automation: Schedule scripts to run daily, weekly, or monthly to fetch the latest data and automatically update your reports or dashboards. Say goodbye to manual CSV downloads.
- Overcome UI Limitations: The standard interface can sometimes be restrictive. With the API, you can pull more complex combinations of dimensions and metrics that might not be available in a standard report.
- Data Integration: This is a big one. You can pull your GA data into a Python environment and merge it with data from other sources like your CRM (Salesforce), ad platforms (Facebook Ads), or payment processors (Stripe) to get a complete picture of your customer journey.
- Advanced Analysis: Once your data is in a Pandas DataFrame, the possibilities are endless. You can perform advanced statistical analysis, build predictive models, create custom attribution models, or visualize your data in unique ways using libraries like Matplotlib or Seaborn.
Getting Started: Prerequisites
Before you write any code, you’ll need to have a few things set up. This initial setup is the most tedious part, but once it's done, you won't have to do it again.
Here’s what you’ll need:
- Python 3 installed on your computer.
- A Google Cloud Platform (GCP) account with an active project.
- Admin or Editor access to a Google Analytics 4 property that has some data in it.
- Familiarity with the command line or terminal to install Python libraries.
Step-by-Step API Setup Guide
To let our Python script talk to Google Analytics, we need to authenticate it. This involves creating a "service account" in Google Cloud, enabling the API, and then giving that account access to GA4.
Step 1: Enable the Google Analytics Data API
First, you need to tell Google Cloud that you intend to use the GA Data API.
- Go to the Google Cloud API Library.
- Select the project you want to use from the dropdown menu at the top of the page.
- In the search bar, type "Google Analytics Data API" and select it from the results.
- Click the blue "Enable" button. If it's already enabled, you're all set.
Free PDF Guide
AI for Data Analysis Crash Course
Learn how to get AI to do data analysis for you — the best tools, prompts, and workflows to go from raw data to insights without writing a single line of code.
Step 2: Create a Service Account and JSON Key
Instead of logging in with your personal Google account, your script will use a special service account for authentication. This is more secure and ideal for automated processes.
- Navigate to the Service Accounts page in your Google Cloud Platform project.
- Click "+ CREATE SERVICE ACCOUNT" at the top.
- Give your service account a name (e.g., "ga-python-reporter") and a description. The Service account ID will be generated automatically. Click "CREATE AND CONTINUE".
- Next, you need to grant this account a role. For simplicity, you don't need to assign any GCP roles right now. Just click "CONTINUE".
- Skip the last optional step and click "DONE".
- You'll now see your new service account in the list. To get the credentials your script needs, click the three dots under the "Actions" column and select "Manage keys".
- Click "ADD KEY" and choose "Create new key".
- Select "JSON" as the key type and click "CREATE".
A JSON file will be automatically downloaded to your computer. Treat this file like a password! Anyone with this file can access your data. Store it somewhere safe and don't commit it to a public GitHub repository. Rename it to something simple like credentials.json and place it in the same folder as your Python script.
Step 3: Add the Service Account to Your GA4 Property
Now you need to grant your new service account permission to view your Google Analytics data.
- Open the JSON credentials file you just downloaded. Find the value for
client_email. It will look something likega-python-reporter@your-project-name.iam.gserviceaccount.com. Copy this email address. - Go to your Google Analytics account and navigate to the Admin section (the gear icon in the bottom-left).
- Make sure you've selected the correct Account and Property. Under the "Property" column, click on "Property Access Management".
- Click the blue "+" icon in the top right corner and select "Add users".
- Paste the service account's email address into the "Email address" field.
- Under "Permissions," select "Viewer". The viewer role is sufficient for pulling data and is the most secure option.
- Click the "Add" button in the top right.
Great! All the setup is complete. Now for the fun part: writing the code.
Step 4: Install Python Libraries
Open your terminal or command prompt and install the necessary Google client library for Python, along with Pandas for data manipulation.
pip install google-analytics-data pandasWriting a Python Script to Pull GA Data
With our environment configured, we can now write a script to request data from the API. Create a new Python file (e.g., report.py) in the same folder where you saved your credentials.json file.
Here is a complete script to pull the number of sessions and active users by country for the last 30 days.
import os
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
DateRange,
Dimension,
Metric,
RunReportRequest,
)
import pandas as pd
# This is the path to your credentials.json file
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'credentials.json'
# Enter your Google Analytics 4 Property ID here
PROPERTY_ID = "YOUR_PROPERTY_ID"
# Initialize the Google Analytics Data API client
client = BetaAnalyticsDataClient()
# Define the request to send to the API
request = RunReportRequest(
property=f"properties/{PROPERTY_ID}",
dimensions=[Dimension(name="country")],
metrics=[Metric(name="activeUsers"), Metric(name="sessions")],
date_ranges=[DateRange(start_date="30daysAgo", end_date="today")],
)
# Execute the request
response = client.run_report(request)
# ---- Processing the API response into a Pandas DataFrame ----
# Prepare lists to hold the processed data
dimension_headers = [header.name for header in response.dimension_headers]
metric_headers = [header.name for header in response.metric_headers]
rows = []
# Loop through each row in the API response
for row in response.rows:
row_data = {}
# Get dimension values
for i, dim_value in enumerate(row.dimension_values):
row_data[dimension_headers[i]] = dim_value.value
# Get metric values
for i, met_value in enumerate(row.metric_values):
row_data[metric_headers[i]] = met_value.value
rows.append(row_data)
# Create the final Pandas DataFrame
df = pd.DataFrame(rows)
print("Google Analytics Data Extracted Successfully:")
print(df.head())Free PDF Guide
AI for Data Analysis Crash Course
Learn how to get AI to do data analysis for you — the best tools, prompts, and workflows to go from raw data to insights without writing a single line of code.
Breaking Down the Script
- Authentication:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'credentials.json'tells the Google client library where to find your secret key file to authenticate. - Property ID: You must replace
"YOUR_PROPERTY_ID"with your actual GA4 Property ID. You can find this in GA under Admin > Property Settings. - Client Initialization:
BetaAnalyticsDataClient()creates the client object that will handle communication with the API. - Building the Request: The
RunReportRequestobject is where you define exactly what you want.
You can find a full list of available dimensions and metrics in the official API documentation.
- Processing the Response: The API doesn't return a simple table. It returns a complex object that we need to parse. The second half of the script loops through the response object, extracts the dimension and metric values for each row, and neatly organizes them into a Pandas DataFrame, which is perfect for analysis.
Run the script from your terminal (python report.py), and you should see the first five rows of your data printed out, perfectly structured in a DataFrame!
Next Steps and Practical Use Cases
Now that you have your data in a DataFrame, you can do almost anything. Here are a few ideas:
- Export to CSV or Excel: Use
df.to_csv('ga_report.csv', index=False)to save your data for use in other tools. - Connect to Databases: Pipe the data directly into a SQL database for storage and further analysis.
- Build Visualizations: Use libraries like Matplotlib or Plotly to create custom charts that go beyond what's available in the GA interface.
- Email Reports: Combine this script with Python's email libraries to automatically send daily performance updates to your team.
Final Thoughts
Using Python to extract data from Google Analytics is a powerful way to automate your reporting and unlock deeper insights that aren't possible through the standard user interface. While it requires some initial setup, the investment pays off by saving you countless hours of manual work and enabling more sophisticated data analysis.
For those who need the power of automated reporting but don't want to manage scripts and API credentials, there are simpler solutions. We built Graphed to solve this exact problem. You can connect your Google Analytics account in a few clicks, and then use natural language - not code - to build real-time dashboards and ask questions like, "Show me my top 10 landing pages by sessions last month." It handles all the API complexities in the background, so you can go straight from data to insights in seconds.
Related Articles
How to Sell Mockups on Etsy: A Complete Guide for Digital Sellers
Learn how to sell mockups on Etsy — from creating your first product to pricing, listing optimization, and driving consistent sales.
The Bookmarks Market: Trends, Opportunities, and How to Win on Etsy
The bookmarks market is growing. Discover the trends, buyer personas, and strategies helping Etsy sellers win in this profitable niche.
How to Start a Bookmark Business on Etsy: A Step-by-Step Guide
Thinking of starting a bookmark business? Learn how to design, price, and sell bookmarks on Etsy for steady creative income.