This is a follow up article for the how do I start a successful blog series that we have been publishing on this site to help users get started and create a successful blog.
Google Analytics (GA) is the de facto standard for tracking the statistics for properties on the web. It is free to use and is constantly being improved upon. It ties into many of the Google accounts that marketers and web masters use which includes Google AdWords and Google Webmaster Tools.
The reports in GA will let you know what pages on your web site get the most (and least) traffic and how the traffic reached the pages. You can learn the referring URL for the traffic, meaning what web site (if any) contained the link that lead to the page on your site. If the referring site was a search engine, a lot of times you can learn the keyword that was used to find your listing in the search results. When setup correctly it can also differentiate between organic and paid traffic.
More than the above, you can also learn the geographic location of your visitors, and the path they took through your site. And also when setup correctly, and if you know where to look in the reports, you can also learn the link the visitor took to leave your site. If the link is an outgoing affiliate referral link, you can tie an organic search visitor to an affiliate sale, for example.
You can get very deep into the analysis of your web site when Google Analytics is installed, especially when leveraging the extra features like outbound clicks, paid referrals, social traffic, etc, but the important thing first is to start tracking the data. You can analyze it later on. So, from day one you should be collecting data.
This requires a JavaScript by Google that is setup for a particular web property to be placed on every page of a web site. Fortunately this is very simple to do in WordPress since it is a templated system. What that means is that you can paste the code into just one template file and it will be used on every page of the site. Better yet, you can also “inject” the code on-the-fly rather than modify any template files. Plugins, custom fucntions, and theme settings can be used to accomplish that.
Installing Google Analytics
I will show you a few different methods that you can use to add Google analytics to your WordPress site.
Method 1) Add Google Analytics to a WordPress Template File
As pointed out above, you can add the GA code to a template file in the theme. This is relatively simple but it’s not the most desired method. Consider this: you have a theme installed that has one-click updates when a new version comes out. The new version may have updates for features, bugs, and/or security issues. You could manually make the updates to one or more files to apply the updates, but it is much simpler to use the one-click update.
If you make any manual changes to your theme, they will be overwritten by the update and have to be redone manually. This clearly isn’t ideal, but it is still common practice so I cover it in the video. If this is the route you want to take, once you get the GA tracking code, find the header.php template file and paste the code just below the opening “body” tag.
1. Go to Appearance >> Editor.
2. Under Templates on the right side, locate and click on the Header (header.php) file.
Note: Be sure you have a backup copy of that file in case something goes awry. You can copy it to your computer through FTP or File Manager. You can also copy and paste the content into an empty file, but if the site goes down as a result of the change, you will need to have your FTP software handy for uploading the backup file.
3. In the code locate the “body” HTML tag:
4. Paste the full GA code just below that.
5. Click the Update File button.
Method 2) Create a Custom WordPress Function to Install Google Analytics
A better way, if you have the know how is to create a custom function to install the Analytics code. There is usually a Theme Functions (functions.php) file that comes with a theme. It depends on the theme developer whether this file suffers the same issues as the Header (or other) file when updating. A lot of the time this is set aside for custom functions by the user (us) and not be overwritten during a theme update. You can’t count on that however. I won’t cover it here but creating your own custom plugin might be better.
Here’s how to add a function to install the GA code to WordPress:
1. Go to Appearance >> Editor.
2. Under Templates on the right side, locate and click on the Theme Functions (functions.php) file.
3. Add the code below to the file.
Now, there may be a placeholder for custom functions, there may not. If there are existing functions, it is safe to assume this file will be overwritten when an upgrade occurs. Just keep a backup of your functions and make it a habit to manually add them back in after a theme update. It’s easier to manually update a functions file than any other, since there is specific placement usually for the other files.
If there are no php tags you will need to add them. I won’t cover that here. If there is existing code in there, there will be existing php tags. Near the bottom of the file you should see a ? then the “greater than” symbol. Include your function just before that spot.
Finally, the code:
add_action('wp_footer', 'custom_ga1'); function custom_ga1() { echo "REPLACE-WITH-GA-CODE"; }
In the above code, I created a function called “custom_ga1” for lack of a better name. It’s important that function name is unique from WordPress functions, and functions of other themes and other plugins. If you replace the name be sure to replace it in both spots. I like to use the site name that I am working on as the prefix, and a meaningful name for the suffix. You can leave it as is and feel safe because there is very little chance of it causing any conflicts.
Of course, replace REPLACE-WITH-GA-CODE with your actual Google Analytics code. It’s important the code is wrapped in double quotes as is shown. This code with place the GA code in the footer of the site.
Method 3) Using a Plugin or Theme to Place the GA Tracking Code
A lot of times, especially these days, the Theme Options panel will have a spot to place tracking code scripts. If that’s the case, all you have to do is paste your Analytics code in there. Then there is no need to execute a custom function because the theme does it for you. Look around for a theme options page either under the Appearance menu or under a new menu option that matches the name of your theme.
You can also use a plugin to handle the process for you. This might be the ideal solution for most for a few reasons:
If you find a plugin from a great developer:
– You will only have to add your GA Property ID and it will fill in the rest of the code. That way, if there are changes to the GA script in the future, an update to the plugin will take care of that.
– It might turn on extra features like tracking outbound clicks.
– It should be a light-weight, cleanly coded plugin that won’t bog down your web site.
– It will make it super easy to install the GA code without having to muck around with template files and custom functions.
This plugin here by Yoast fits the bill. You still have to create a web property in your Google Analytics account (the steps are below) but the rest is very easy.
In fact, there is an option to let the plugin authenticate with your GA account so that you can choose the web property from a list. But you can also manually paste in your property ID (not the GA code, just the property ID).
It also lets you track outbound links and some other stats that may or may not be relevant to you. I like also how it lets you pick where you want the code to be placed. It will insert it in the header by default but doesn’t force that setting on you. I demonstrate its use in the video embedded into this article.
Quick Steps for Adding a Web Site to a Google Analytics Account
With the host of options for installing the GA code into our WordPress blog behind us, let’s finally have a look at how to get the code to be placed.
The first step of course is to get a Google account and login to it. If you don’t have one, just bite the bullet and get a Gmail account. This will be handy for setting up lots of useful accounts. You can still attach your own domain to your Gmail account so no-one has to know you are using a web based Google email address. There is a free way if you can still find the loophole, but it doesn’t cost a lot to set it up if you go the paid route.
At any rate, once you have a Google account and are logged in, follow these steps:
1. Visit google.com/analytics
2. Click Sign in (top right).
3. Click Admin (top right).
4. Click +New Account.
5. Fill in all fields, then click Get Tracking ID.
You will then be presented with the Property ID and the Google Analytics tracking code. Choose the appropriate one depending on which method you plan to use for adding the code to your WordPress site. It’s mostly just the plugins that will require just the Property ID, while most other methods will require the full Google Analytics code.
This was a good tutorial that is current and works. Google Analytics is an awesome tool and this post helped me set it up.
Great post and a must have resource for every online entrepreneur. Google Analytics is a golden tool for webmasters and should be used to check for vital website stats like number of unique visitors, average page visit time etc.
It helped me setup google analytics on my site. Thank you.