
Google analytics is a functionality that you will need on almost all of the websites you create. It allows you to track all sorts of data about the visitors of your website. You can see the most popular pages, time on site, bounce rate of your site and how the visitor found your site.
In this tutorial we are going to see how you will add this to your WordPress site.
To start off you need to sign up for an account for Google Analytics. When you register an account you are given some Javascript to add to your website so you can track what your visitors are looking at.
There are a few different solutions you can go with to add Google Analytics code to your website.
- Add the Javascript code to your WordPress theme
- Write PHP code to add the Javascript to the footer of your WordPress theme
- Install a WordPress plugin to add Google Analytics to your page
- Write a WordPress plugin to add Google Analytics to your page
As there are multiple different ways of doing this which is the best way?
Add Javascript Into Theme
You can add the Javascript inside your WordPress theme, this is fine it will work and you will start collecting data. But the problem is what if you change your theme in the future and forget to copy over your Google Analytics code...your lose all your data.
Write Code In Functions.php
You can write PHP code into your functions.php file so that it will add your Google Analytics code into the footer of your theme by using the wp_footer action. Using this method means you could code something to be able to change the Google Analytics code in the future. But you don't really change the code enough times to grant doing this, also you have the same problem as above in that if you change your WordPress theme you have lost your Google Analytics code.
Download A WordPress Plugin
The next option is to use a WordPress plugin to add the Google Analytics code into the header or footer of your theme. In theory this is the best solution to this problem as your Google analytics code will still be added if you change your theme.
The reason why this could be a bad solution is because you are using another developers plugin so you can not be sure exactly how their plugin works and if any additional code is being ran that isn't needed.
Create A WordPress Plugin
The best solution for this problem will be to create your own WordPress plugin to add the Google analytics code to the header or footer of your WordPress theme.
The reason why this is the best solution you can go for is, one if you change your theme your Google analytics code will still be added to your theme, two you have full control over how your code works. Some existing plugins that do this will only add the Javascript to the footer of the theme, but webmaster tools needs your Javascript to be added in the head tag.
Creating your own plugin you will have full control over if you have your code inserted into the head tag or the footer of your theme.
To create you own plugin to add Google Analyics, first create a new WordPress plugin. Add a folder into your wp-content/plugins and add a new php filer with the following code.
/*
Plugin Name: Google Analytics
Plugin URI: http://www.google.co.uk
Description: A plugin to add Google analytics to your theme
Version: 1.0
Author: Author name
Author URI: http://www.example.com
License: GPL2
*/
// Assign action to the wp_footer
add_action('wp_footer', 'add_googleanalytics');
// Run this function on the wp_footer action
// This will put this code inside the footer.php file
function add_googleanalytics() {
// Paste your Full Google Analytics Javascript here within a php echo
}
Extended Google Analytics for WordPress
If you want a plugin that has a bit more functionality to it, try this premium WordPress plugin on your WordPress site.

- Add’s google analytics code
- Auto link adsense to analytics
- Auto event tracking for downloads, mailto and outbound links
- Select which file extensions to download track
- Tool to generate campaign tracking links to use in newsletters and other campaigns
