Google Analytics IP Anonymization

In order to be complient to country specific data privacy law and restrictions (i.e. in Germany), you should enable the Analytics IP anonymization feature in critical countries. Unfortunately in many countries it's still not finally resolved if IP adresses are personal identity information (PII).
This page should provide an overview which possibilities exist in order to implement IP address anonymization with Google Analytics.


Standard IP Anonymization with Classic Analytics

In oder to enable the IP Anonymization for legacy Analytics (Async Code Version), you just need to add the following line at the tracker intitalization.

_gaq.push (['_gat._anonymizeIp']);

If Google Tag Manager is used, the option for IP anonymization can be directly enabled using a corresponding checkbox in the GTM web interface.

GTM Legacy Analytics IP Anonymization Option

Standard IP Anonymization with Universal Analytics

In oder to enable the IP Anonymization for Universal Analytics, you just need to add the following line at the tracker intitalization.

ga('set', 'anonymizeIp', true);

If Google Tag Manager is used, the IP anonymization can be enabled by setting the field "ipAnonymization" in the GTM web interface for the Analytics tag.

GTM Universal Analytics IP Anonymization Option

Advanced IP Anonymization with Universal Analytics

In some cases it could make sense to implement an enhanced IP anonymization - in example in order to mask out the last two IP address blocks. As such an implementation is not possible with the standard options provided by analytics, you need to provide some own, also a bit more complex code, which modifies the Analytics Measurement protocol trackinig request, passing an own serverside anonymized IP address before sending it to the Google server.

To achieve this goal, you have to override the sendHitTask function the following way (ANONYMIZED_CLIENTIP has to be replaced by the IP address that has been anonymized on server-side).

function sendHitTask(model){ 
    var payLoad = model.get('hitPayload');
    var trackingBaseUrl = '//www.google-analytics.com/collect?';

    if (payLoad.length < 400) {
        var collectUrl = window.location.protocol + trackingBaseUrl + payLoad + '&uip=' + ANONYMIZED_CLIENTIP;
        var myImage = new Image();
        myImage.src = collectUrl;
    } else {
        var r = window.ord || Math.floor(Math.random() * 1e16);

        var myIframe = document.createElement('IFRAME');
        myIframe.width = "1px";
        myIframe.height = "1px";
        myIframe.style.display = 'none';
        myIframe.name = "tiframe_" + r;

        var myForm = document.createElement('FORM');
        myForm.method = 'POST';
        myForm.action = window.location.protocol + trackingBaseUrl;
        myForm.target = "tiframe_" + r;
                
        var parameterValuePairs = payLoad.split('&');

        for (var vpi=0; vpi < parameterValuePairs.length; vpi++) {
            var splittedParameterValuePair = parameterValuePairs[vpi].split('=');
            var parameterName = decodeURIComponent(splittedParameterValuePair[0]);
            var parameterValue = (splittedParameterValuePair.length == 1) ? '' : decodeURIComponent(splittedParameterValuePair[1]);

            var parameterInput = document.createElement('INPUT');
            parameterInput.type = 'HIDDEN';
            parameterInput.name = parameterName;
            parameterInput.value = parameterValue;
            myForm.appendChild(parameterInput);
        }

        var parameterInput = document.createElement('INPUT');
        parameterInput.type = 'HIDDEN';
        parameterInput.name = 'uip';
        parameterInput.value = ANONYMIZED_CLIENTIP;
        myForm.appendChild(parameterInput);

        document.body.appendChild(myForm);
        document.body.appendChild(myIframe);
        myForm.submit();
    }
}

To finally override the sendHitTask function, you have to add the following code on the UA Tracker initialization or, if you are using Google Tag Manager, do the same using the GTM interface: define sendHitTaskFunction as custom JavaScript variable and set the sendHitTask field for the corresponding Analyitcs tag.

ga('create', 'UA-XXXX-Y', 'auto');

ga(function(tracker) {
  tracker.set('sendHitTask', function(model) {
        // enter your send hit task function here
  });
});
GTM sendHitTask Override

Disadvantages of IP Anonymization

  • Practically there are no real disadvantages when using the standard IP anonymization feature of Analyctics, where the last 8 bit of the IP address are masked out
  • In case that more than the last 8 bit of the IP address are masked out, the user location cannot be detected and analyzed anymore. Therefore the Analytics reports showing user location statistics cannot be used anymore.
  • In general you should be careful when setting up IP filters for your Analytics dataviews as the filters will be applied to the already anonymized IP address. This results in the fact that you cannot filter a single IP address only, but rather a block of 256 adresses in the minimal case (i.e. 192.168.10.0/24)