Search code examples
javascriptgoogle-analyticsprivacy

Google Analytics and anonymizeIp


I have set up Google Analytics and received this tracking code here

<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','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-********-1', '*****.com');
    ga('send', 'pageview');
</script>

But I also need to implement the anonymizeIp function because of legal issues. It sais here that you have a _gat object and just have to call that function. But in the tracking code I received, there is no _gat object.

I've also seen code snippets related to this issue which look completely different than my tracking code.

The burning question is: How do I implement the anonymizeIp in my tracking code correctly?


Solution

  • You're using the newer Universal Analytics (analytics.js) code, which has a different syntax. Try

    ga('create', 'UA-********-1', '*****.com');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
    

    Docs at: IP Anonymization