How to Install Google Analytics in WordPress

Google Analytics Set up

Illustration by Filip Zywica

Understanding how visitors are getting to your site, what they are looking for and how they are interacting with your content is crucial if you want to become a successful blogger. Google Analytics (GA) is a great product able to answer for all those questions. Most importantly it’s completely FREE and incredibly easy to set up!

This article will step by step guide you through GA setup on your WordPress site/ blog. First, you will learn how to sign up to Google Analytics and how to create the tracking code. Then I will explain how to install this tracking code on your blog by using one of the 3 available options to choose from.

I. HOW TO SIGN-UP TO GOOGLE ANALYTICS

STEP 1: Sign up for Google Account

In order to signup for Google Analytics, you need an active Google Account (If you have a gmail, it means you already have one and you can use your gmail credentials to register and then use other Google products including GA). If you don’t have a Google Account, you will have to sign up for it- to do so, go to Google Sign Up Express page. After feeling the form, check the email you used to set up the account and follow the link to finish creating your Google account. Once you verify your email address, you will be able to access Google Account.

STEP 2: Sign up to Google Analytics

Now you have a Google Account, so you can register for GA. In order to do that, go to the Google Analytics Sign Up page and log in with your Google account login details.

Click ‘Sign up’ and you will land on a setting up page.

II. HOW TO CREATE GA TRACKING CODE

STEP 1. Set up your account

Now you are on the tracking code setting up page, which should look like the below:

Google Analytics- creating New Account

Google Analytics- creating New Account

Since you want to create a tracking for your WordPress site, ‘website’  as the property to track. Then you can choose between a Classic and Universal Analytics. In the time of writing, Universal Analytics is in a beta version, but it is already stable and will give you many more options to choose from in the future, therefore I highly recommend to go for this option, however if you prefer the Classic version, the choice won’t influence further steps in the article.

STEP 2: Set up your property

Now, it’s time to share more information about your website- since it’s pretty self-explanatory- provide Website URL (if your site is available under www. version then provide that bit as well), then choose industry category and reporting time zone and hit Get Tracking ID button. A pop-up window with Google Terms and Conditions will appear- click ‘I agree” and you will be presented with you tracking code, similar to this one:

STEP 3. Select and copy the code

Now, Google ask you to copy and paste this code into every webpage you want to track. Do not close this page, leave it open and go to your website in a new tab (in this way, you can always get back to it).

III. HOW TO INSTALL GOOGLE ANALYTICS TRACKING CODE ON YOUR SITE

Since your have a WordPress site, the good news is that installing the tracking code is very easy. There are a few ways to install it and you can choose from one of the following options:

  • Installing a plugin (easy option)
  • Adding code to HEADER.PHP (more difficult)
  • Adding code to functions.php

(only t Go to Appearance> Editor> and find your header.php file on the right- hand side and paste the code (from step xx) just after thetag.

OPTION 1: Installing a Plugin

You need to add GA tracking code to every single page, installing a plugin, will help you to do that in a very quick and easy way. There are quite a few plugins available although I recommend this plugin as it is very light and straight forward. In order to install it, go to your WordPress dashboard, then click on Plugins >Add New and search for Google Analytics. There will be many search results, but choose Google Analytics created by Kevin Sylvestre– install it and activate it. Then go to Settings> Google Analytics. Here you need to paste the property ID which can be found within your code created in step 5- it has this format ‘UA-XXXXXXXX-X’

OPTION 2: Adding code to header.php

If you do not want to install too many plugins on your site (too many plugins, can slow down your website), you can add the tracking code to the header file of your site. Head section (in header file) is used on every single page across your site. In order to add the code, go to Appearance> Editor and find your header.php file on the right- hand side, click on the file and paste the entire code which you copied in the step 5 just before the </head> tag and click Update the File button.

OPTION 3: Adding code to functions.php

I highly recommend to go for this solution ONLY if you have experience with php and you really know what you are doing!In order to add the code, go to Appearance> Editor and find your functions.php file on the right- hand side and click on the file. Then copy the below and paste it at the end of the file.

[php]// Include the Google Analytics Tracking Code (ga.js)
// @ http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
function google_analytics_tracking_code(){

$propertyID = ‘UA-XXXXX-X’; // GA Property ID

if ($options[‘ga_enable’]) { ?&amp;gt;

&amp;lt;script type="text/javascript"&amp;gt;
var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘&amp;lt;?php echo $propertyID; ?&amp;gt;’]);
_gaq.push([‘_trackPageview’]);

(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
&amp;lt;/script&amp;gt;

&amp;lt;?php }
}

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

// OR include GA tracking code before the closing body tag
// add_action(‘wp_footer’, ‘google_analytics_tracking_code’);
[/php]

After that do not forget replace the UA-XXXXX-X with your property ID, which you found it STEP 5 of this tutorial!

STEP 6: SAVE THE CODE IN GOOGLE ANALYTICS

Whatever method you chose, you should have your tracking installed! Now, go back to the other tab, where your tracking code is generated and click ‘Save” (from step 5). It takes up to 48hrs for Google to recognise that the tracking has been installed.

Now, relax and check in two days if your tracking code is working correctly. Btw, job well done! You deserved a nice treat now:)

P.S. If you come across any issue or something is not clear enough in this post, please leave a comment below and I will provide more details!

Mags Sikora

I'm Mags, an SEO Consultant, London