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.

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
- Case 1: You already have a list of users’ client ID you want to delete. Then skip this step.
- Case 2: You know what kind of users you want to delete and know how to create a segment to find them. Then refer to my another blog Get Google Analytics data to your Jupyter Notebook by using Analytics Reporting API. It may help you get the list little easier.
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”

- Modify the Google APPs script as below:
function deleteUser() { | |
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = activeSpreadsheet.getSheetByName('Main'); | |
var resultsheet = activeSpreadsheet.getSheetByName('Result'); | |
var userInputs = sheet.getRange("A3:B").getValues(); | |
var webPropertyId = String(userInputs[0][0]); | |
var numClients = userInputs[0][1] | |
for (var i = 0; i<numClients;i++){ | |
var typeOfId = String(userInputs[2+i][1]); | |
var userId = String(userInputs[2+i][0]); | |
try { | |
var request = { | |
"kind": "analytics#userDeletionRequest", | |
"id": { | |
"type": typeOfId, | |
"userId": userId | |
}, | |
"webPropertyId": webPropertyId | |
}; | |
Analytics.UserDeletion.UserDeletionRequest.upsert(request); | |
resultsheet.appendRow(["'" + userId, 'yes']); | |
}catch(error) { | |
Browser.msgBox(error.message); | |
} | |
} | |
} |
- Go to Resources -> Advanced Google Services -> Enable Google Analytics API

- Go to Resources -> Cloud Platform project -> currently associated project

- Click on the project ID -> Google Cloud Platform -> APIs & Services -> Library

- Search for Analytics API -> Enable it

- 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.

- 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.

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