Add Google Tag Manager Code

Follow these instructions to get your tracking code ID

2) Add the first snippet into the the head like so (change the UA-XXX bit).

add_action('wp_head', function(){ 
    ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->

    <script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-XXX"></script>

    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-XXX');
    </script>
    <?php
});

3) Add the body snippet to the footer

add_action('wp_footer', function(){
    ?>
    <!-- Global site tag (gtag.js) - Google Ads: XXX -->
    
    <script async src="https://www.googletagmanager.com/gtag/js?id=AW-XXX"></script>
    
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'AW-XXX');
    </script>
    <?php
});

4) Clear your cache

5) Use this tool, or this extension, to make sure it was installed properly

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.