How to Remove PII from GA4? Here’s our 2-step GTM Solution

November 25, 2020

Alex Head of Data Engineering
Read more Read more After working in web development and analytics for 5 years, there is NO WAY Alex will let you do anything before he’s got a whole data architecture engineered into your website. His motto: “In God we trust. All others must bring data.” Don’t be intimidated, though. He’s super patient and human about it
Magda Founder & Head of Strategy
Read more Read more Magda started marketing for friends by stealing candies from her mom’s shop to distribute. She now does the grown-up thing and studies data science at Oxford University. She’s quite overfocused on marketing, statistics, and data. Between that and her hobbies in learning genetics, understanding psychology, doing exercise, and watching pandas, she doesn’t have much time left for the latest movies. Throw a Harry Potter her way – and super-educated Magda won’t get it at all.

Intro

I know there are many professionals out there who are having a hard time using the New Google Analytics, so I’ve made a quick and easy 2-step tutorial on how to remove PII via GTM.

With the release of the new Google Analytics (GA4) the solutions to the common problems for GA Universal Analytics are not always working any more.

This is also the case when you want to remove the PII (Personal Identifiable Information) from links via GTM.

When you have in GA user information such as email, name, address, or phone number, you have to stop collecting this information and the best way to fix that is through GTM.

If you already have a solution for this issue in GTM and you want to migrate to the new GA4 you will see that there are changes and your solution is not working anymore.

We want to share with you our solution to this common problem for the new GA4.

Step 1 – Create a new custom JS variable

To begin this setting you should go into GTM and create a new variable, then select the type Custom JavaScript.

In this variable, you should paste the following code, which will allow you to add the parameters that contain PII and those will not be shown in GA.

Basically, GTM will not send to GA the link parameters that you don’t want to send.

Feel free to copy and use the code below:

function() {
  
  var params = ['email', 'fname', 'lname', 'address', 'phone', 'occupation', 'city'];
  var a = document.createElement('a');
  var param,
      qps,
      iop,
      ioe,
      i;

  a.href = document.URL;

  if (a.search) {
 
    qps = '&' + a.search.replace('?', '') + '&';

    for (i = 0; i < params.length; i++) {
 
      param = params[i];
      iop = qps.indexOf('&' + param + '=');

      if(iop > -1) {

        ioe = qps.indexOf('&', iop + 1);
        qps = qps.slice(0, iop) + qps.slice(ioe, qps.length);

      }
 
    }

    a.search = qps.slice(1, qps.length - 1);
   
  }
 
  return a.href;
 
}


The custom variable should contain the previous code, and you can add or remove the parameters you have on the link from your website on the object from the first lines of the code (var params).

In our example, you can see parameters such as ‘email’, ‘name’, ‘phone’, but on your website maybe there are other parameters which contain PII and you want to remove them, so feel free to add or remove any parameter there, just use our example as a format to not break the code.

Step 2 – GA4 Configuration

If you are implementing GA4 via GTM you know that the first thing you have to configure is the new GTM tag named Google Analytics: GA4 Configuration.

After you have that in place the only thing you have to do is to add a new row on Fields to set and then simply add ‘page_location’ as field name and the newly created JS variable as value.

Here is how this setting should look like:

In the end, you simply click on Save and Preview this change. Now the page location will exclude the parameters that contain PII and you are ready to go with the new GA4.

Author

Alex Head of Data Engineering
Read more Read more After working in web development and analytics for 5 years, there is NO WAY Alex will let you do anything before he’s got a whole data architecture engineered into your website. His motto: “In God we trust. All others must bring data.” Don’t be intimidated, though. He’s super patient and human about it
Magda Founder & Head of Strategy
Read more Read more Magda started marketing for friends by stealing candies from her mom’s shop to distribute. She now does the grown-up thing and studies data science at Oxford University. She’s quite overfocused on marketing, statistics, and data. Between that and her hobbies in learning genetics, understanding psychology, doing exercise, and watching pandas, she doesn’t have much time left for the latest movies. Throw a Harry Potter her way – and super-educated Magda won’t get it at all.
Do you have a question related to
How to Remove PII from GA4? Here’s our 2-step GTM Solution
We'd love to hear it: