This is Jing, a data analyst with great passion for data science and big data technology. This blog gonna to show you how to using Google Analytics Reporting API to get Google Analytics upsampled data to your Jupyter Notebook or save to Excel.
Before we start, I assume you already have Jupyter Notebook installed on you computer and have access to a Google Analytics view of a website. Besides, I also assume you are familiar with Google Analytics, for example, you know what is client ID, session, event label, customer dimensions and etc.
Use Case : Get a list of segmented users’ client ID
To make a better explanation, I made up a use case which may sounds silly. Let’s imagine a scenario where for certain kind of reasons, some users’ phone numbers are saved in the GA Event Labels during their online sessions because of some tracking setups. We want to find out these users’ client ID and then later on we want to delete them to avoid potential GDPR issues.
In this blog, I will show you how to find these users and get a list of these users’ Client ID in your Jupyter Notebook. And then later on, I will show you how to delete these users in GA with once click in another blog.
In this use case, your Jupyter Notebook is an application, sending request to Google Analytics server. So, your Jupyter Notebook is also a client to Google, which also have a Client ID. This might be confusing with client id in Google Analytics. Sorry for this.
Step 0 : Create a project and Enable Google Analytics Reporting API
Create a project for your application in Google Cloud Console if you do not have one. In this blog, I created a project called “user deletion”.
Go to APIs & Service -> Library, find the Google Analytics Reporting API and enable it.
Step 1 : Create the client credential for your Jupyter Notebook for requesting Google Analytics
Go to APIs & Services -> Credentials -> Create credentials -> OAuth client ID
Select Other, name our client ID as “Jupyter_Notebook”, click on Create -> OK
Then you can find your client credentials on the Credentials page. By clicking on the download button, you can download your client credential as a json file. Rename it as “client_secret.json” and put it in a folder you are comfortable with.
Step 2: Install the client library for your Jupyter Notebook
If you have done this before, skip this. Otherwise, Open you terminal -> type in the command below. More information about Google API Client Libraries for Python.
sudo pip install --upgrade google-api-python-client
pip install --upgrade oauth2client
Step 3: Create a segment in Google Analytics to find the target users
Go to User Explorer -> Add Segment -> Conditions, write an Regex to find the users whose event labels have “phoneNumber =”. Then save this Segment as “phoneNumber”
Step 4: Clone the code from jing-jin-mc Github
In the repository, you have all the code you need. But there are some-places you need to modify to make this code work for you.
Step 5: Get the segment ID and Custom Dimension ID
Go to Query Explorer, authenticate ourselves and get the segment ID and Custom Dimension ID for the Client ID in Google Analytics. When we type in Client ID, we know Custom Dimension 42 (in our website) is set for Client ID.
Similar way to get the segment id for the segment we just created:
Step 6: Jupyter Notebook template for Getting data from Google Analytics
Now, open the Jupyter Notebook template and modify the code with the information you have gotten. For the first time to run the template, you will need to authorise your notebook in the web browser. Just follow the instruction from Google and go back to your Notebook , you will see it is done.
Congratulations! We are done here!
Now you should have successfully gotten Google Analytics data for this use case in your Jupyter Notebook! Besides, I will show you how to upload the data frame to a Google Sheet in another blog to avoid copy paste issues.
This is not something amazing, but hopefully will make your work a little bit easier! This is Jing, thanks for reading my blog! Feel free to commend and ask questions! I will get back to you as much as possible.