Shoora
5/4/2018 - 11:03 AM

Simple plugin to add Google Analytics tracking code to your WordPress

Simple plugin to add Google Analytics tracking code to your WordPress

<?php

    /*

      Plugin Name: My Google Analytics Tracking Code
      Description: Add Google Analytics tracking code to WordPress website
      Version: 1.0

    */

    add_action( 'wp_head', 'my_google_analytics_output', 1 );
    function my_google_analytics_output() {

        ?>
        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'UA-XXXXXXXX-X');
        </script>
        <?php
    }

   /**
    *
    * end of plugin.
    *
    */

?>