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.):
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

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

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):
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-xxxxxxxx-x', 'sitename.com');
ga('send', 'pageview');
var myintervalid = setInterval(function() {ga('send', 'event', 'top', 'top', 'top');},30000);

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).


1 ping

Skip to comment form

  1. Elena

    Thanks for this useful tip! :)

  2. Patty Bradley

    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!


  3. Craig Bailey

    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.

  4. Giovanni

    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


    1. Tobi

      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. Edit your first WP plugin and exclude yourself from tracking in google analytics » Tor-Björn Fjellner on GSM and IT

    […] 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

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

10 + sixteen =