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.
- Sign into your host and navigate your WordPress plugins directory.. mostly in /… /wp-content/plugins
- 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://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://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.
Enjoy!
Frequently Asked Questions
How to create a simple WordPress Google Analytics plugin?
To create a simple WordPress Google Analytics plugin, you can follow a brief tutorial that shows you how to easily create a plugin to install the analytics code on your pages.
Where can I get the analytics code to install on my WordPress page?
To get the analytics code for your WordPress page, sign in to your Google Analytics account, go to Admin --> (Properties) --> Tracking Info --> Tracking Code, and you'll find the code there.
Why is Google Analytics recommended as the top choice for website analytics?
Google Analytics is recommended as the top choice for website analytics because it is widely used, provides comprehensive insights, and is free to use for website owners.
What is the downside of installing analytics code in the header page of a WordPress site?
The downside of installing analytics code in the header page of a WordPress site is that every time the theme is updated, the changes will be lost, requiring you to redo all the changes made previously.
How can I avoid losing analytics code changes when updating a WordPress theme?
To avoid losing analytics code changes when updating a WordPress theme, you can create a simple plugin with the analytics code and activate it instead of directly inserting the code into the header page.
What are the steps to install Google Analytics code on a WordPress page?
To install Google Analytics code on a WordPress page, first sign up for a free Google Analytics account, grab the analytics code from the Admin section, and insert it just before the </head> tag in the header of your WordPress page.
Why is it important to track website analytics?
Tracking website analytics helps webmasters understand how their site is performing, how many people are visiting, and how visitors are engaging with the content, which is crucial for making informed decisions and improving the site's performance.
What is the recommended method to install Google Analytics codes on WordPress pages?
The recommended method to install Google Analytics codes on WordPress pages is to create a simple plugin with the analytics code and activate it, which makes it easier to maintain the code and avoid losing changes when updating the theme.