Adjusting Google Analytics analytics.js (Universal analytics) for meaningful blog statistics

I’m running a wordpress site, where we want to keep an eye on the number of visitors and their behaviour on the site. It turned out to be very easy to get started: Create an account in google analytics and 10 minutes later, you’ll be instructed to enter a code snippet within your head tags (This is the version served for so called Universal Analytics, where the file analytics.js is called.):
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxxxxx-x', 'sitename.com');
ga('send', 'pageview');
</script>

Since I use the very functional wordpress theme graphene, adding the code was very easy: there’s a special section in the theme settings for that.

A few days later, I realized that there was more to do. The number of bounces was quite high, as if the vast majority of the readers were some kind of bots, who closed a page just after opening it.

Some reading revealed the sad thing to me:
By default, Google analytics only registers each pageview. The time of a pageview is calculated as the time from when the page is opened, until a new page is opened in the same session. Unfortunately, this means that the viewing time for the page last opened becomes 0 (zero). And: if someone opens only one page and reads it carefully, before closing it, this will be regarded as a bounce with zero time on site.

I found some scripts to handle this problem, but they didn’t work, and the reason for this seems to be that I’m using the latest beta-version of google analytics, whish employs a script named analytics.js.

So, here’s what I have done: I just needed to add a couple of lines, now my script looks like this (as in the previous code sample, the site specific details are redacted):
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxxxxxx-x', 'sitename.com');
ga('send', 'pageview');
var myintervalid = setInterval(function() {ga('send', 'event', 'top', 'top', 'top');},30000);
setTimeout(function(){clearInterval(myintervalid);},1800000);
</script>

So, what will these two additional lines do?
30 seconds after a page was opened, if the page still is open, it will start sending a "beacon" in the form of an event to google analytics. This is repeated every 30 seconds. (Actually, every 30000 milliseconds…). As soon as the first beacon arrives, the pageview stops being a bounce. And when the reader closes the page, the pageview time will correspond to the latest received beacon.

By the way, if I in Google Analytics open the tab for real-time events, I can see these beacons coming in every 30 seconds. It gives a nice, warm feeling!

However, some people open a page and forget about it. In order to not have google analytics count hours of (empty) viewing, I added the last row, which terminates the repetition after half an hour (i.e. 1800000 milliseconds).

Why I named the event top? Just my abbreviation for "time on page".

Possible further development:
I might inhibit the beacon if the user isn’t scrolling, but that feels like a slightly bigger project for a less busy day.

And yes, it works. Now the bounce rates and visit times look more realistic. (It’s very normal that some 20% of the visitors leave the site within 30 seconds without opening a second page.)

Here’s a link to an article, that pointed me in the right direction: http://analytics.blogspot.com.es/2012/07/tracking-adjusted-bounce-rate-in-google.html

Did this help you? Please comment below (or ping me).

5 comments

1 ping

Skip to comment form

    • Elena on April 25, 2014 at 11:39
    • Reply

    Thanks for this useful tip! :)

  1. Thank you, this is exactly what I was looking for. Ever since I upgraded to universal analytics, our bounce rate has soared. I knew it was the 30 second thing, but not know how to fix it. You showed me. Thanks!

    Patty

  2. Thanks – this solved my problem! Used to have this working in Classic analytics, but had issues with getting it working Universal. Really like the cutoff after an hour feature too.

  3. Many many thanks: the single blog post really giving a practical and direct solution to the bounce rate / time on page issue in ga.
    Just wondering that it consumes more CPU and bandwidth with continuous sending…

    I will try to implement on my crossword and puzzles website

    Best
    Giovanni

    1. Hi Giovanni,
      if you’ve got thousands of visitors a day, then Google may want you to limit the collection of data. Processor load on your visitors’ computers is very small. (For example, throwing in tynt for text copy handling, or addthis for nice social sharing functionality both add much more tracking, traffic and processor load.)

  1. […] Google analytics will show you a script, that you should include on every page your server sends to its visitors. The script from google will report every page visited once. For a typical blog, you may want to keep better track on how long people stayed on the page. You can read more about this modification in a previous article. […]

Leave a Reply to Giovanni Cancel reply

Your email address will not be published.

3 × 2 =