Short Automaton brings you a step-by-step beginners guide to using the Google Analytics API with Python 3. For now, we are only exploring GA3, also known as Universal Analytics (UA). Our goal is to provide an easy-to-use guide for new Pythonistas to wrap their arms around organic search data at scale.
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.
Step 2: Connecting to Google Analytics (UA) 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.
Step 3: Executing Google Analytics API batchGet Report Request with Python
Once you’re connected to Google Google Analytics, you can begin making requests to the API. There is really only one method available to make reports called
batchGet() and we cover the request body as well as the best, most scalable ways to parse the data into a Pandas DataFrame. You will not only learn how to write your code to pull any report you want, but also the reasoning behind the ways we parse the data, including tips to make light work of reusing your script. This can all be done with read-only access!
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.