How To Get Google Analytics 4 Property Report to Pandas Dataframe Using API

If your work is even slightly related to online marketing – you probably have heard already about new version of Google Analytics – Google Analytics 4 Property. Everybody are talking about it now because Google Analytics is the most popular web analytics tool, so a lot of businesses worldwide are depend on it. One of the new cool features about GA4 – is data transfer to Google BigQuery, which previously was available only for expensive Google Analytics 360. But BigQuery costs and it’s not cheap, especially for big amount of data. Still – sometimes we need to get raw data from Google Analytics for automatisation or data modeling purposes. I prefer using Python and Pandas for this kind of tasks, hope you too. 🙂

In previous version of Google Analytics (Universal Analytics) – Google offered us a great “analytics reporting API v4”, but what about the new GA4? It’s API called – Google Analytics Data API:
Getting access to your account with this API is quite similar to previous one. In my example – I am going to use Google service account generated in Google Cloud Platform. So in order to get access to API with service account you should do next steps:

  1. Go to Credentials section on your GCP project, create a Service account (if you already don’t have one) and download the secret json:
  2. Enable Google Analytics Data API for your project:
  3. Go to your Google Analytics 4 Property account and add your service account email in Account User Management Section as any other user:
  4. Now go to Property Settings and save somewhere your property id – you will need it later in Python script:

  5. Install python library google-analytics-data using PIP or add it to the requirements.txt in your python project:

Python example script

Now – you should be able to run the Python script below. All you should change in this example script – add path to you your service account secret json and add your property_id from step 4:

from import AlphaAnalyticsDataClient
from import DateRange
from import Dimension
from import Entity
from import Metric
from import RunReportRequest
import pandas as pd
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_your_google_secrets.json'

def ga4_response_to_df(response):
    dim_len = len(response.dimension_headers)
    metric_len = len(response.metric_headers)
    all_data = []
    for row in response.rows:
        row_data = {}
        for i in range(0, dim_len):
            row_data.update({response.dimension_headers[i].name: row.dimension_values[i].value})
        for i in range(0, metric_len):
            row_data.update({response.metric_headers[i].name: row.metric_values[i].value})
    df = pd.DataFrame(all_data)
    return df

def get_ga4_report_df(property_id, dimensions, metrics, start_date, end_date):
    dimensions_ga4 = []
    for dimension in dimensions:
    metrics_ga4 = []
    for metric in metrics:
    client = AlphaAnalyticsDataClient()
    request = RunReportRequest(entity=Entity(property_id=property_id),
    response = client.run_report(request)
    return ga4_response_to_df(response)

property_id = 'your_property_id'
dimensions = ['source', 'medium']
metrics = ['sessions']
start_date = '2021-01-01'
end_date = 'today'
df = get_ga4_report_df(property_id, dimensions, metrics, start_date, end_date)

The base for this script was taken from Google’s documentation:
I just added a function which transforms API response to Pandas DataFrame and some other little changes in order to make it more convenient. Hope it will help you to get your data from GA4 easier.

5 6 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
2 years ago

Nice Article Thanks for sharing

7 months ago

Great article.. Works for Beta libraries as well.