Delete user with Google Analytics User Deletion API to be GDPR compliant

Hi,

This is Jing, a data analyst with great passion for data science and big data technology. The EU General Data Protection Regulation (GDPR) is really a big thing that happens in this year. As a customer, feel more secure about my personal information online thanks to GDPR. Though as a data analyst, working with e-commerce, GDPR leads to some problems to my daily work. This blog is gonna to show you how to use User Deletion API provided by Google Analytics to delete users’ personal information to be GDPR compliant simply in a Google Spread Sheet.

Delete Bulk of Users with User Deletion API

Prerequisite

Before we start, I assume you have an “Edit” level 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, segment, event label, customer dimensions and etc.

Besides, you need to make sure your Google Cloud Platform is activated. For this project, it almost cost you like nothing. So, don’t worry about it.

Use Case : Delete a list of Segmented Users

To make a better explanation, I continue to use the use case I made up earlier in the blog Get Google Analytics data to your Jupyter Notebook by using Analytics Reporting API . 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. In earlier blog, I have shown you how to find the list of Segmented Users. Now, I am gonna show you how to delete them at one click to avoid potential GDPR issues in a Spread sheet!

Let’s begin!

Step 0 : Get the list of users’ client ID you want to delete

Step 1 : Create a Google Spread Sheet to use User Deletion API

Here is a template Google Spread Sheet I’ve already created to put your list of client IDs. Feel free to make a copy of it to build your owns on. If you are not interested in how to create this sheet, then Congratulations! We are done here.

Otherwise, please follow in steps:

  • Create a Google Spread Sheet
  • Tools -> Script Editor -> Edit project name, I named it as “user_deletion_API”
Google APPS Script
  • Modify the Google APPs script as below:
  • Go to Resources -> Advanced Google Services -> Enable Google Analytics API
Google Analytics Reporting API
  • Go to Resources -> Cloud Platform project -> currently associated project
User Deletion API Project
  • Click on the project ID -> Google Cloud Platform -> APIs & Services -> Library
Google API Library
  • Search for Analytics API -> Enable it 
Analytics API
  • Make it look nice 
  • Rename current sheet as “Main” -> Create a new sheet “Result” -> Add a function to cell “B3”
  • Fill in your Google Analytics Property ID
  • Download a button you like from Google 
  • Insert a image -> Assign script -> Type in the function name in Google APPS Script we created earlier. It calls “myFunction” in this case.
Assign Scrip
  • Finally it looks like below:
  • Click on “Start” Button, the scrip will run automatically
  • First time you run the script, it requires you to authenticate the script. Just follow the instruction from Google.  
  • If it runs successfully, the result will show in “Result” sheet.
Result for User Deletion

Step 2 : Put the list of users’ client ID in a Google Spread Sheet

  • Case 1: If you find it is convenient enough to manually cope paste the list to the Google Spread Sheet. Then Congratulations! You can begin use the shared template Google Spread Sheet to delete users! 
  • Case 2: If you were in case 2 also in Step 0, then refer my another blog Connect your Jupyter Notebook to Google Spread Sheet

Here is a Jupyter Notebook template if you were in case 2 in both Step 0 and Step 2. 

Find my complete code at GitHub

Congratulations! We are done here!

Now you should be able to delete a lot of users by one click! It might take some to setup all these things. But would you like to manually delete hundreds of users? Besides, what you have done here, you can easily reuse it next time!  

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

Get Google Analytics data to your Jupyter Notebook by using Analytics Reporting API

Hi,

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 data to your Jupyter Notebook or save to Excel. 

Prerequisite

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.

Let’s begin!

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 

https://github.com/jing-jin-mc/GA_reporting_API

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.