How to add Google Analytics to WordPress sites

GoogleAnalytics

Google Analytics is a fabulous analytics tool enabled on more than 10 million websites across the globe. Here we will add Google Analytics to WordPress site. Obviously, we will need a Google Account first. Here I will skip this because most of us already have a GMail or Adsense Account or even an account associated to Android Phone. So going to straight to Google Analytics Sign up page.

Important: If you do not have Google account and Google is forcing you to create an email id. Then you may directly go to google.com/analytics and start signup process for Google Analytics with your existing email address. You may also use your Google Apps account to sign up Analytics.

Sign Up Google Analytics Account:

Once you have logged in to Google Account you may sign up Google Analytics account. The Analytics signup page will be something like this. Add account name, site name, website url, industry category and time-zone for your reporting. Don’t worry about any discrepancy, you may change all these information later.

GA-setup

After filling the information required proceed to accept terms of the services and sign up. If everything goes right you will get your tracking code and Google Analytics account instantly. Your Analytics account id will be something like “UA-47713692-1” and some JavaScript code something like below. Some WordPress themes including thesis theme framework have built-in functionality where we have to enter the tracking id only. For rest of the themes we will need the entire script.

ga-tracking-code

Adding Google Analytics code to WordPress site:

Once we have Google Analytics code we will now add this tracking code to the WordPress site. There are a few approaches to do that.

  1. Add the code using wp_head or wp_footer hook.
  2. Directly add the code to theme file (by modifying header.php or footer.php)
  3. Using a plugin or theme feature

Add the code using wp_head or wp_footer hook.

You may add a few lines of code to theme’s functions.php  file.

/**
* How to add Google Analytics to WordPress by @_Anand_Kumar
* Tutorial link http://blogsynthesis.com/?p=1435
**/
add_action( 'wp_head', 'synth_google_analytics');
function synth_google_analytics() { ?>
<!-- REPLACE THIS LINE WITH GOOGLE ANALYTICS CODE -->
<?php }

Do not forget to replace <!-- REPLACE THIS LINE WITH GOOGLE ANALYTICS CODE --> with your actual Google analytics code.

That’s all. Your WordPress site will, now, be tracked by Google Analytics.

Directly add the code to theme file (by modifying header.php or footer.php)

You may copy and place the obtained code to header.php file. Here, we will have to find the closing head tag ie </head>. Once you find the code place the Analytics code just above it.

This method is do not works will all themes because many themes do not have header.php or the theme developer do not suggest to modify the code directly to theme file.

Add Google Analytics to WordPress using theme feature / option.

A lot of themes (including Genesis, Thesis) have options to hook some code to header and/or footer. You may use this feature. This is one of the easy (and safe) way to add scripts to your sites.

  • Genesis user may add the code to “Header and Footer script” under Genesis Setting menu.
  • Thesis 2 user may navigate to Thesis >> Site >> Google Analytics (just place the id, which will be like UA-123456-1. See the highlighted image 2) OR add the whole tracking code under Thesis >> Site >> Tracking Script.
  • There are a few plugin who offers some features add the code at ease.

Now if you might ask which method I prefer! Frankly I will prefer either method one or three (if my theme has such feature.)

Here are a few reasons behind it.

  • Using a plugin for everything sucks. It will slow down the speed of your site and ultimately provide bad user experience.
  • If the theme has such functionality then we may add a number of scripts using just one hook.
  • Modifying Theme file will be hard to maintain at some point in future.
  • An intermediate (or even you) may opt for a site specific plugin. This will be best option to add cross theme functionally. Here you may find more information about site specific plugin. Note: Any of the above three method will be enough for this purpose.
  • I like adding the Google Analytics code to WordPress Head because it provides more accurate analytics. While some blogger add the code at the end of the body tag. Read this article for details.

I this article helped you don’t forget to share with your friends. Your every tweet, fb like or +1 will help this site grow. So, help us if you liked. If you have any problem or suggestion leave a comment below.

Published by

Anand Kumar

I am the person behind this site.

4 thoughts on “How to add Google Analytics to WordPress sites”

  1. Thanks for the code. Google seems to keep changing its recommendations over the years for where to place the GA code. Google’s latest recommendation is to place the code directly after the opening body tag, and so try using ‘genesis_before’ instead of ‘wp_head’ when adding to functions.php

  2. Can I get a straight answer from somebody on Google analytics and WordPress.com not.org. Can I or can I not put analytics on.com. I know I can’t use the plug-in but is there a way to do it with some simple and clear explanation about where to paste the code? Again, I’m interested in Google analytics and.com not self hosted.org

    Frustrated

Comments are closed.