How to add your Google Analytics code to WordPress?


Having the ability to track your visits is a must for every site owner. There are many systems that you can use to track visits but the most popular method is by using Google Analytics. There are many ways to enable these stats in WordPress, many of them involve the use of plugins, which makes it very easy to add the associated tracking code to your site, but this can be overkill to simply add a few lines of code. Let me show you how to add this quickly and easily to your WordPress Website?

1. Login to Google Analytics and grab the tracking code.

You first need to grab your analytics code which can be found in admin > tracking info.

<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');</p>

<p>ga('create', 'UA-XXXXXXX-X', 'auto');
ga('send', 'pageview');</p>

<p>&lt;/script&gt;

2. Let’s create a function.

The code must be inserted in your functions.php file which can be edited using a code editor or the built in editor in WordPress. If you have read my previous tutorials then you should be familiar with a function in php. Let’s create a function to add our code as below?

&lt;?php
function add_google_analytics(){
?&gt;
//Insert your analytics code.
&lt;script&gt;
(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');</p>

<p>ga('create', 'UA-XXXXXXX-X', 'auto');
ga('send', 'pageview');</p>

<p>&lt;/script&gt;
&lt;?php
}
?&gt;

Add your own unique tracking code in the code where you see ‘UA-XXXXXXX-X’ as this is what google analytics uses to identify your site.

3. Now lets load the function in WordPress.

We now need to tell WordPress to load this function and add this to the header of your site. We can use a WordPRess specific hook “wp_head”, which will add the function to the WordPress queueing system and load it at the correct time.

add_action('wp_head','add_google_analytics' );

In the above code the add_action tells WordPress to load the function, and this has two parameters. The first parameter wp_head is a hook which tells WordPress where to load the function and the second parameter is the name of the function. Let’s see the completed code.

&lt;?php
function add_google_analytics(){
?&gt;
//Insert your analytics code.
&lt;script&gt;
(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');</p>

<p>ga('create', 'UA-XXXXXXX-X', 'auto');
ga('send', 'pageview');</p>

<p>&lt;/script&gt;
&lt;?php
}
add_action('wp_head','add_google_analytics' );
?&gt;

This can be a quick addition to your site, to allow you more information regarding your visitors and knowing this about your visitors can help you plan your marketing activities when blogging. This can also help you in future redesigns by knowing what your visitors are looking for and the browsers they use.
I hope you find this useful and be sure to leave your comments below.