IP Anonymization in Google Analytics – With Script Example

IP Anonymization

Looking for an example of IP Anonymization in Google Analytics?

This article shows you a before and after example of how to anonymize an IP address by modifying the Google Analytics script on your website.

Once done, you will effectively hide your visitor’s IP Address when they visit your site.

Great for GDPR and Data Privacy concerns!

You’ll also discover the pros and cons of making IP addresses anonymous for your website visitors.  Plus, some background about why it’s good to do.

 Google Analytics IP Anonymization

With focus on data privacy ever increasing and GDPR now in effect, it’s a good idea to consider hiding the IP address of your users when they visit your website.

Disclaimer – I’m not a lawyer and can’t give legal advice about the GDPR.

I can however, share what I’m doing to protect my visitors’ data privacy concerns around their IP address (considered personal data by the GDPR).

Note:  Your IP Address is Safe!

By the way, I’ve already implemented this here on Online Business Ambitions.  I consider data privacy to be important and I’m happy to let you know that Google Analytics has anonymized your IP address on this site.  🙂

Pros and Cons

From the perspective of an online business owner, there are some pros and cons to hiding your visitor’s IP address.

The biggest pros with IP anonymization are that you’re keeping your visitor’s data privacy in mind and taking steps to comply with GDPR.

Another pro is that if you’re not collecting that data, there’s no need to obtain consent from you users to do that.

The biggest con is that the location info for your visitors is reduced a bit.  As a generalization, maybe you won’t be able to figure out exactly where a person is located…  But, you’ll likely still be able to see the city and state (region) where someone is from.

At least that’s what I’ve noticed.

Frankly, most people with online businesses don’t need to get more granular then city or state.  Given that, the pros definitely outweigh the cons in this case.

What IP Anonymization Does

When you use the anonymize IP feature in Google Analytics, it takes the last octet (think segment or piece) of the IP address and changes it to zero.

That’s what makes it anonymous since each visitor usually has their own, unique IP address.

So for example, if a visitor’s IP address is 66.249.75.84, the anonymize IP feature will change it to 66.249.75.0.

Pretty simple actually!

Modifying Your Google Analytics Script

One of the reasons why I’m writing this post is that I had to hunt around for good examples of the script changes you need to make.

It took me awhile to find a good example of IP anonymization in Google Analytics.

So, with this post, my goal is to show you a simple before and after example of what you need to do to implement IP anonymization in Google Analytics on your own website.

Now, not to complicate things, but there are a couple different “flavors” of Google Analytics you can have on your website.

One is the legacy library and the other is the newer implementation that uses Google Tag Manager.

The older legacy implementation is ga.js and the newer one is analytics.js.

Don’t get too worried about this.  As soon as you look at the script on your site, you’ll see which one you picked to implement.

Script Examples

So by now you’re probably saying, “I get it.  Enough with the background…  Show me what to do!”

First off…

DO NOT CUT AND PASTE THESE EXAMPLES AND PUT THEM INTO YOUR WEBSITE!

That won’t work anyway.

Go to your own Google Analytics admin page and get your own tracking code.  Then modify the code to implement the anonymizeIp function for your own website.

With that said, below are the before and after scripts for each type of implementation.

Choose yours accordingly, depending on what you already have on your site, and put in your own UA ID where the xxxxxxxx-x is.

If in doubt, or want to change what’s there now to be more up to date, go with the newer implementation using Google Tag Manager.

The lines with the changes are in bold.

OK, here we go…

Legacy – ga.js (Older Implementations)

Before Making the Change:

<script> (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

})(window,document,’script’,’https://www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-xxxxxxxx-x’, ‘auto’);

ga(‘send’, ‘pageview’);

</script>

After Making the Change:

Here’s what it should look like when done:

<script>

(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

})(window,document,’script’,’https://www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-xxxxxxxx-x’, ‘auto’);

ga(‘set’, ‘anonymizeIp’, true);

ga(‘send’, ‘pageview’);

</script>

Notice that the line with the anonymizeIp in it has been added between the create and send functions.

That’s what you need to add to your own Google Analytics script on your website.

And now onto the newer implementation using Google Tag Manager…

Newer – analytics.js (When using Google Tag Manager)

Before Making the Change:

<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx-x”></script>

<script>

window.dataLayer = window.dataLayer || [];

function gtag(){dataLayer.push(arguments);}

gtag(‘js’, new Date());

gtag(‘config’, ‘UA-xxxxxxxx-x’);

</script>

After Making the Change:

Here’s what it should look like when done:

<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx-x”></script>

<script>

window.dataLayer = window.dataLayer || [];

function gtag(){dataLayer.push(arguments);}

gtag(‘js’, new Date());

gtag(‘config’, ‘UA-xxxxxx-x’, { ‘anonymize_ip’: true });

</script>

Notice that the line with the config function now has new code with { ‘anonymize_ip’: true } added inside it.

You’ll need to modify that line in your own Google Analytics script on your website and add that code inside it.  Be sure to get the brackets and parenthesis correct.

Conclusion

Whew…  OK!

Now I know why I couldn’t find a simple example.  This blog post took awhile to write!

Anyway, I hope you got some benefit out of it.

IP anonymization is important for your visitor’s privacy.  With just some simple modifications to your Google Analytics script to implement it, it’s well your worth time to make the change.

There’s little reason not to do it and your users will appreciate that you take their data privacy seriously.

That’s it!

If you see any errors in the code or would like to share any more info on anonymizing IP addresses, please leave a comment below…

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *