How To Remove PII From Being Sent To GA4 With Quick JavaScript

How To Remove PII From Being Sent To GA4 With Quick JavaScript

Google mandates that no data be passed to GA4 that contains Personally Identifiable Information (PII) which includes email addresses and phone numbers.

Companies who send in PII may face consequences like legal issues, deletion of GA4 data, or the inability to use Google products.

I've seen many forms and email platforms which append the user's email address in the URL as a query parameter.

An example of this would be:

example.com/[email protected]

When loading GA4, the full page URL gets sent to Google. Meaning that you would not be considered compliant.

Thankfully, with a quick Javascript variable in Google Tag Manager, you can easily strip these parameters (and PII) and send clean URLs to Google.

(Ideally you should work with developers to ensure that this email isn't being captured.. but this is a quick fix.)


Here is a quick guide on how you can remove PII from URLs in GA4.

Step 1: Create A Custom Javascript Variable:

  1. Within Google Tag Manager, navigate to "Variables"
  2. Select the "New" blue button under "User-Defined Variables".
  3. A new pop-up will occur. Click on "Choose a variable type to begin setup...


4. Under variable type, select "Custom JavaScript"


5. Copy and paste the below code into the field. Note: change "YOUR_QUERY_PARAMETER" to the unique parameter in your URL that is capturing the email address. For example:

www.example.com/[email protected]

Where "customer_email" would be the query parameter you want to exclude the text from, as this is the PII.

function(){
  var blocklist = 'YOUR_QUERY_PARAMETER,email,address_line_1,address_line_2,city,state,zip_code,full_name,first_name,last_name,phone_number,postcode'.split(',');
  var replaceWith = '';

  var url = location.href;
  var sanitizedUrl = url.replace(/((\?)|&)([^#&=]+)(?:=([^#&]*))?/g, 

function(input, delim, qmark, key, val) {
    if (-1 === blocklist.indexOf(key))
      return input;
    else
      return replaceWith ? delim + key + '=' + replaceWith : qmark || '';
  }).replace(/\?&*$|(\?)&+/, '$1');

  return sanitizedUrl;
}        

6. Name the JavaScript variable and hit "Save"


You will now need to add this script to the GA4 configuration tag, so it will block the emails from being sent within the page_location in GA4.


  1. Navigate to your configuration tag and under "Configuration Parameter" add the following:
  2. Create "Add Parameter" and under the Configuration Parameter (left hand box) type in "page_location".
  3. On the value, you will add the custom javascript variable you created. This strips out the PII.



Jeremy H.

Transforming Data into Insights & Growth | Digital Analytics, Measurement Strategy & Implementation Expert

6 个月

There's a simplified method to redact PII data (such as email addresses and query parameters) from GA4 using a built-in feature. This is also useful if the GA4 setup does not include GTM. To configure, navigate to Admin > Data Collection and modification > Data streams > [your data stream] > Events > Redact data

要查看或添加评论,请登录

Nichelle Halstrom的更多文章

社区洞察

其他会员也浏览了