Beginner’s Python Guide to Google Analytics 4 (GA4) API

Beginners Python Guide to Google Analytics 4 (GA4) API

Short Automaton brings you a step-by-step beginners guide to using the new Google Analytics 4 API with Python 3. Our goal is to provide an easy-to-use guide for new Pythonistas to wrap their arms around their website data at scale as well as for experienced Google Analytics API users who are working to migrate their scripts to GA4 by the sunset date of July 1, 2023.

Step 1: Creating Google API Credentials

Before you even begin to start coding, you need to create credentials so your Python program can talk to Google Search Console without needing a browser. We walk you through how to create a project in the Google Cloud platform, add APIs to your project, create an OAuth Consent Screen, and finally create your OAuth credentials. This has recently been updated to include the new Google Analytics Data API for use in GA4.

Guide to Creating Google API Credentials

Step 2: Connecting to Google Analytics 4 API with Python

After creating your credentials, you can put them to use in Python. We walk you through one of the most efficient ways to authenticate your Python script so you can begin interacting with the Google Analytics API. The method we walk you through is more likely to be similar to OAuth flows you may have worked with in the past for Google Analytics or Google Search Console as opposed to the current Get Started guide for GA4 provided by Google. I want to make migration to GA4 easy.

Connecting to Google Analytics 4 (GA4) API with Python

Step 3: Executing Google Analytics 4 API batchRunReport Report Request with Python

Once you’re connected to Google Google Analytics 4, you can begin making requests to the API. Although there are more methods available in GA4 as compared to Universal Analytics, we will keep things simple by using the batchRunReport() method, which is designed to be a comparable match to the batchGet() method in Universal Analytics. We cover the request body as well as the best, most scalable ways to parse the data into a Pandas DataFrame. You will learn how to write your code to pull any report you want, whether you’re starting from scratch or working to migrate your existing Universal Analytics API Python scripts. This can all be done with read-only access!

Google Analytics 4 API Report with Python

Feedback and Comments

We hope this Python How-To has been helpful to you. Please let me know if you have any feedback that can improve this guide, each article, or future guides. I’d also love to get your comments or requests to expand upon this guide or to create additional multi-step guides. And, of course, I love just hearing that you’ve found this useful! You can send a message from the contact page or you can tweet to me at @ShortAutomaton.

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top