UPDATE: This post and the code presented here became mostly obsolete since the release of “universal analytics”. I’m leaving it here “as-is” for reference, but please note that the code and the WordPress plugin which uses it are now mostly useless and will likely have no effect if you are using an up to date universal analytics code (which you should!).
Google Analytics is one of the best tracking tools for web traffic, and by far the most used across the web. Moreover, it’s free. But it also has it’s flaws and downsides.
Here’s a major one: Analytics gives you the “time [spent] on site” and “bounce rate”, but these data are only based on Pageviews, because the code only fires once when a page is loaded. See where this goes ? The time spent on site, according to Analytics, is actually the time between the moment an user enters the site, and the moment when the last page of his visit is loaded.
Here’s what it means: say an user enters your site, spend 3 minutes reading an article, and leave (maybe he even added your site to his bookmarks to come back later). Analytics says the time on site is exactly 0:00, and counts this visit as a bounce.
So you have a 80% bounce rate and was wondering why ? It’s not because your site is not interesting, nor because your visitors are crazy freaks clicking everywhere without thinking! (at least not all of them…)
Another scenario is when your visitor lands on your site, spend 2 minutes reading an article, clicks a link and sped 3 more minutes on another page before leaving your site. Did you guess how Analytics will reflect this visit? 2 Pageviews and 2 minutes on site (instead of the actual 5 minutes).
“What’s the point of this criticism if you can’t do better anyway?” you ask. Well we can! Thanks to the Analytics’ API and this code by Brian Cray, you can help Analytics gather more accurate data.
Simply copy/paste this snippet at the bottom of your page, just before the </body> tag, and come back tomorrow to check your Analytics reports to find out how inaccurate your previous data were. This works no matter if you are using the old or new tracking code.
A visual example
I’ve installed this code on one of my sites at the end of February. The average pageviews on this site is about 2, most of the visitors come from Google and land on the very page they were interested in. Most of them they simply read it and then leave the site.
There were no difference in traffic between february and march, but take a look at the differences in Time on site (above) and Bounce Rate (below). We’re not even talking about accuracy anymore, the previous data were completely wrong.
Implementing in WordPress
Option #1: the neophyte coding handyman
Heresy! If WordPress was a religion, you would be excommunicated for doing this. But that’s still a working solution that many uninitiated would come up with:
From your WordPress dashboard, go to Appearence > Editor, and edit the footer.php file. Locate the closing </body> tag and paste this code just above. Done. This will work, but that’s a messy work.
WordPress doesn’t like hardcoded scripts in template files, and this is why it offers functions to properly handle scripts (wp_register_script() et wp_enqueue_script()). Using them will also improve performance and avoid conflicts between scripts.
There is another major downside to this option: this involves editing your theme, so what happens if you update it and decide to change for another theme in the future? The code you’ve just added will be lost, and you will need to manually add it again.
There is a better, and easier way…
Option #2: the done-for-you plugin
Here a tiny plugin I crafted to make you life easier. Using it is as easy as install, activate, forget!
What it does is simply properly register and enqueue a minified version of this script, so WordPress will call it where your theme has its “wp_footer()” hook.
How to use:
– Download GABounceFix.zip.
– From your WordPress Dashboard, go to Plugins > Add New > Upload. Pick the .zip file you’ve just downloaded and click “Install Now”.
– A few seconds later, WordPress tells you it has been successfully installed, and offers to activate it. So simply click “Activate”.
– Done. From now on this code will fire on every page of your site, and you will enjoy more real data in your Analytics reports!