Google Analytics – GDPR

Adding Google Anyltics code and adding option to disable it:

   <!-- Google Analytics -->
    <script async src='//www.google-analytics.com/analytics.js'></script>
    <script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-xxxxxx-x', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
    </script>
    <!-- End Google Analytics -->


    //HTML LINK
    <a class="ga-disable" href="#">Link</a>
    <a href="javascript:gaOptout()">Link</a>


    // Set to the same value as the web property used on the site
    var gaProperty = 'UA-xxxxxx-x';
    // Disable tracking if the opt-out cookie exists.
    var disableStr = 'ga-disable-' + gaProperty;
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
        window[disableStr] = true;
    }
    // Opt-out function
    function gaOptout() {
        document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
        window[disableStr] = true;
        alert('Das Tracking durch Google Analytics wurde in Ihrem Browser für diese Website deaktiviert.');
    }




    jQuery(document).ready(function($) {
        $(".ga-disable").click(function(event) {
            event.preventDefault();
            gaOptout();
        });
    });

Google Analytics – tracking

Include the Google Analytics Tracking Code – WP Functions

add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>
    <script type="text/javascript">
        (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-166254-45', 'auto');
        ga('send', 'pageview');
    </script>
<?php }

Google analytics – tracking

Include the Google Analytics Tracking Code (ga.js) – HEADER

<script async src='https://www.googletagmanager.com/gtag/js?id=UA-40742215-1'></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){
        dataLayer.push(arguments);
    }
    gtag('js', new Date());
    gtag('config', 'UA-000000-1', { 'anonymize_ip': true });
</script>