Anonymize IP addresses GA Universal Tracking Code

There are currently two types of Google Analytics, the normal universal Google Analytics (GA) and Google Analytics 4 (GA4).

GA4 makes the IP addresses of visitors anonymous without having to change anything to the settings. With GA, this is not the case, and an additional piece of code must be added to the tracking code if you’re not tracking via Google Tag Manager.

Tracking Code

A tracking code from GA looks like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id='UA-XXXXXXX-X'"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-XXXXXXX-X');
</script>

This is the code that needs to be added to the website. In the above code the tracking ID has been made anonymous so do not use the above code but follow the next steps.

Steps to anonymize IP addresses to make them GDPR proof

Follow the next steps to make Google Analytics Universal anonymize IP addresses. You need to do this in order to be GDPR proof, you don’t have to put Google Analytics Universal behind a cookie wall if use this technique.

  1. Go to Google Analytics
  2. Login with your account
  3. Go to the account via admin
  4. Choose the property where you want the data to be located
  5. In the list of the property it says Tracking Info, go there
  6. Click on Tracking Code in the submenu
  7. Copy the code under Global Site Tag (gtag.js)
  8. Before adding the code to the WordPress website, install the Insert Headers and Footers plugin
  9. Then go to settings > Insert headers and footer
  10. Add the code under Scripts in the Header.
  11. Copy this code: gtag(‘config’, ‘GA_TRACKING_ID’, { ‘anonymize_ip’: true });
  12. Paste the code under gtag(‘config’, ‘UA-XXXXXXX-X’);
  13. Click the save button at the bottom of the page

An example of the tracking code includes anonymizing the IP of the visitors.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id='UA-XXXXXXX-X'"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX-X');
gtag('config', 'GA_TRACKING_ID', { 'anonymize_ip': true });
</script>

Side note

A small side note, by doing this, your own IP address will also no longer be filtered by Google Analytics Universal. This can be annoying for customers, but otherwise, Google Analytics must get behind a cookie. You might also be interested in how to anonymize Hotjar tracking, so you can still track the user behavior without having to put Hotjar behind a cookie wall, or how to add scroll depth tracking via Google Tag Manager to your website.

Logo Freelance CRO specialist Sander Volbeda

Working remotely from Groningen, the Netherlands. Get in touch and let’s schedule a meeting, no strings attached.

Get in touch
© 2024 Sander Volbeda, All rights reserved
CoC: 53236734 VAT: NL002247968B39