Google Analytics code 404 error tracking in WordPress PHP function file

I want to show google analytics on every page except I want to show it slightly different on the 404 template page.

I have 2 analytics codes. One of them is displayed on every page. The other is for displaying only on the 404 pages.

Read More

This is the php code I am working on that is inserted into the functions file. I can’t get it to work. So far all it does is execute the else statement. Please help.

if ( is_404() ) {
// Include the Google Analytics Tracking Code in 404
function google_analytics_tracking_code_404(){ ?>

<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-XXXXXXXX-23', 'mydomainname.com');
  ga('send', 'pageview', '404/?url='+ document.location.pathname + document.location.search +'&ref=' + document.referrer);

</script>

<?php }

// include tracking code before the closing head tag
add_action('wp_head', 'google_analytics_tracking_code_404');

// OR include tracking code before the closing body tag
// add_action('wp_footer', 'google_analytics_tracking_code');
}
else {
// Include the Google Analytics Tracking Code
function google_analytics_tracking_code(){ ?>

<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-XXXXXXXX-23', 'mydomainname.com');
  ga('send', 'pageview');

</script>

<?php }

// include tracking code before the closing head tag
add_action('wp_head', 'google_analytics_tracking_code');

// OR include tracking code before the closing body tag
// add_action('wp_footer', 'google_analytics_tracking_code');
}

Related posts

Leave a Reply

1 comment

  1. The function is_404() need to call inside the hook of wp_head.

    // Include the Google Analytics Tracking Code in 404
    function google_analytics_tracking_code_404(){ ?>
        if ( is_404() ) { ?>
    
            <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-XXXXXXXX-23', 'mydomainname.com');
              ga('send', 'pageview', '404/?url='+ document.location.pathname + document.location.search +'&ref=' + document.referrer);
    
            </script>
        <?php }
    }
    
    // include tracking code before the closing head tag
    add_action('wp_head', 'google_analytics_tracking_code_404');