Every webmaster wants to know how their site is doing, how many people are reading and how many are returning to the site.

To get clear understanding of how one’s website is performing, one needs to install analytics code on each page.

When it comes to website analytics, Google Analytics is king. There are other less popular website analytics providers across the web, but the one place most website owners look is Google Analytics, and it’s free.

So, if you’re running a WordPress blog or website and you wish to install Google Analytics code on your pages, this brief tutorial is going to show you how to easily create a simple plugin to get the job done.

The first thing you’ll want to do is to apply for a free Google Analytics account here.

After you’ve signed up, you’ll need to grab the analytics code to install on your WordPress page. To get the code, sign in to your Analytics account and go to Admin –> (Properties) –> Tracking Info –> Tracking Code.

There you’ll find the code to install on your pages. Google recommends to install the code in the header page of your site. WordPress themes have header pages.

Google recommends to install the code just before the </head> tag of the header page.

The one downside of installing analytics code into the header page of your site is, everytime you update the theme, all the changes will be lost.

You must redo all the changes you made previously. This can be a hard work, especially if you’ve made so many custom changes to your theme.

So, one of the best method to install analytics codes on your WordPress pages is to create simple plugin with the analytics code and activate it.

With this method, upgrading your WordPress blog or changing your themes will not remove the analytics codes from your site. This is a great solution.

Now to create a simple WordPress Analytics plugin, follow these steps.

  1. Sign into your host and navigate your WordPress plugins directory.. mostly in /… /wp-content/plugins
  2. In there, create a file and all it anything you want… for this tutorial, I’m going to name it google-analytics.php

Then add these lines to the beginning of the page.. Most if not all WordPress plugin files contain these lines.

<?php
/*
Plugin Name: Google Analytics Plugin
Plugin URI: https://www.liberiangeek.net
Description: Adds a Google analytics trascking code to your theme using wp_head.
Author: Richard
Version: 1.0
*/
?>

Now the plugin page is set.. What we need to do is create the actual function to install the code in the head of your themes. That can be accomplished using WordPress hook wp_head.

The code we’re using is the one below.

<?php
function google_analytics() {?>
<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-117907332-3', 'auto'); ga('send', 'pageview');
</script>
<?php }
add_action( 'wp_head', 'google_analytics', 10 );
?>

The final page should look like the one below by combing the two codes above. One thing to have in mind when creating the plugin is to make sure there are no blank lines in the file or you’ll have issues with your WordPress feeds.

<?php
/*
Plugin Name: Google Analytics Plugin
Plugin URI: https://www.liberiangeek.net
Description: Adds a Google analytics trascking code to your themes using wp_head.
Author: Richard
Version: 1.0
*/
?>
<?php
function google_analytics() { ?>
<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-117907234-2', 'auto'); ga('send', 'pageview');
</script>
<?php }
add_action( 'wp_head', 'google_analytics', 10 );
?>

That’s it! Replace the highlighted code with your analytics code, then save the file. Next, login to WordPress admin page, and go to your the Plugin page to activate the plugin.

The plugin should be activated.

simple google analytics plugins

Enjoy!