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

Connect your Jupyter Notebook to Google Spread Sheet

Hi,

This is Jing, a data analyst with great passion for data science and big data technology. As a data analyst, working with Excel or Google Spread Sheet is sometimes inevitable, considering the reality that a lot your colleagues are more comfortable with seeing data or results in Excel or Google Spread Sheet. This blog gonna to show you how to connect your Jupyter Notebook to Google Spread Sheet to make your workflow more automatic.

Prerequisite

Before we start, I assume you already have Jupyter Notebook installed on you computer. 

Let’s begin!

Step 0 : Create a project and Enable Google Drive 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”, which I used in another blog Get Google Analytics data to your Jupyter Notebook by using Analytics Reporting API

Go to APIs & Service -> Library, find the Google Drive API and enable it.

Step 1 : Create a Service Account for your Jupyter Notebook

Go to APIs & Services -> Credentials -> Create credentials -> Service account key -> New service account, named it as “Google_Sheet” -> Select role as “Service Account User” for this account -> select “JSON”  -> click on “Create”. Download it, rename it as “service_account_GS.json” and put it in a folder you are comfortable with.

Step 2 : Create a Google Sheet and share it with Service Account E-mail

Open the “service_account_GS.json” you’ve just gotten. Find your “client_email”

Share the Google Sheet you just created with this “client_email”

Get the Sheet ID from the URL to the sheet

Step 3: Install the libraries

Open your terminal, type in the following command

pip install gspread oauth2client df2gspread

Step 4: Open your Jupyter Notebook

See the Notebook template here

Now you should see the Google Sheet is updated with the data you uploaded!


Congratulations! We are done here!

Now you should have successfully connect your Jupyter Notebook to a Google Sheet! Find more information about Google Spread Sheet at df2gspread

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.