Google Analytics 4 (GA4) Data Extraction using API in Python

Sheranga Gamwasam
5 min readFeb 25, 2024

--

Google Analytics is one of the main resource used in digital marketing industry to identify and understand their visitors/customers behavior in their website.

You may need to download the data to your datalake If you require a deep analysis such as Machine learning, Deep learning, Time series modelling using Google Analytics data, even though reports can be viewed from Google Analytics UI.

So, Our main objective of this is to download Google Analytics data with multiple dimensions and metrics for different date ranges.

Today I will address the steps to be followed to extract the data from Google Analytics through an API in Python.

  1. Create Google API Credentials.
  2. Generate a Refresh Token.
  3. Generate an Access Token.
  4. Download Google Analytics 4 (GA4) Data.

Create Google API Credentials

Credentials are needed to ingest the data from Google Analytics securely. Below steps are needed to be followed to obtain the credentials.

Create a Project in Google Cloud

Once you signed in to Google Cloud Platform, click on the “Select a project” drop down at the top left. When you click on that, it will pop up a module. If you’re doing this for the first time, you probably won’t have any projects. There may be cases that you use common organization email (eg: if your company uses Google Workspaces) and projects might exist in there. Within this pop module, at the top right, select New Project.

Once you create the project, it will start to spin up and eventually take you back to the homepage. You’ll have a notification that the project has been created and you can enter the project.

Enable required API to Project

This is your project dashboard. This is your home screen whenever you enter your project.

The first thing we’re going to do is enable Google Analytics Data API (for Google Analytics 4). You can enable more API later if you want. The process is exactly the same. So, Click on APIs & Services, then in the sub menu select Library.

Create an App

After you enable Google Analytics Data API, it takes you to the API & Services Page. Click on the OAuth consent screen.

First we need to select the User Type. If you’re using a personal Gmail account, you only have one option — External. If your Google account is a part of a Google Workspace (formerly G Suite), you have the option to use Internal. I recommend an Internal user type if it is available to you. In this article, I’ll walk us through the External option.

You’ll need to fill out the information on the app registration screen. Only three fields are required: the app name, the user support email (this will likely be your own email), and the developer contact information (again, likely your own email). After you’re done, hit the Save & Continue button to move on to the Scopes.

Click on the Add or Remove Scopes button and a menu will pop in from the right and select the Google Analytics Data API Scope (https://www.googleapis.com/auth/analytics.readonly) and click Save & Continue button.

Step 3 is adding users to test this. Add yourself to this as well as anybody else on your team that would be using these credentials. There is a limit of 100 users prior to app verification during the lifetime of the app. Once that is done, Save & Continue and you’ll be taken to a summary page for your OAuth Credentials and from there back to the dashboard.

Your publishing status will be in testing and there is a button to publish the app. If you aren’t actually making a public-facing app, you will probably never have to deal with publishing the app.

Create the Credential Json file

You can click on Credentials in the APIs & Services menu at this point. It’s time to create the OAuth credentials.

At the top of the credentials page, click on Create Credentials, then click on OAuth client ID. Since we’ll be using these credentials in Python, you’ll want to select the Desktop app in the Application type drop-down menu, give the name and click the create button. Once you click the create button credential json file can be download.

Now you have successfully completed the very first stage and received the required client credentials. Let’s move to the next step to generate an refresh token.

Generate a Refresh Token

Credential file is required to generate a refresh token. When you run the below code it will redirect to a web browser and generate a json file and it includes the refresh token that we required to create an access token.

Generate an Access Token

Access token is required to download Google Analytics data but it expires every hour. if you need to generate a new access token, you have to use a refresh token.

Download Google Analytics 4 (GA4) Data

Reference

--

--