Domain Investing & Website Development

Domaining site recommended by

If You Use Google Analytics You Must Read This

Michael Sumner • Apr 9, 2013 • 17 Comments

Google Analytics is a great piece of software, but it has a major flaw in the way it tracks average time on site and bounce rate.

Let’s take the worst-case scenario as an example; someone visits your site, spends five minutes reading an article, but has no further interaction with your site and then leaves.  Google Analytics measures the time he spent on the site as the difference between when he entered and his last page view.  In this scenario Google will see the difference as zero and call it a bounce when it was actually an engaged visitor.

Here is a great article on the subject that explains it better than I can, click here to read it.

The author provides some simple code you can add right before your </body> tag that will essentially ping Google every 10 seconds to let it know the visitor is still there.  It is important to note that you must put <script type=”text/javascript”> and </script> tags around his code or it will not work.  This will give you a more accurate picture of how long people are spending on your site and how often they actually bounce.

<script type="text/javascript">
(function (tos) {
  window.setInterval(function () {
    tos = (function (t) {
      return t[0] == 50 ? (parseInt(t[1]) + 1) + ':00' : (t[1] || '0') + ':' + (parseInt(t[0]) + 10);
    window.pageTracker ? pageTracker._trackEvent('Time', 'Log', tos) : _gaq.push(['_trackEvent', 'Time', 'Log', tos]);
  }, 10000);

I changed the code to be five seconds instead of ten for my own site because I wanted time on site to be accurate within five seconds.  The average time on site for this blog went from 47 seconds to 3 minutes and 21 seconds and the bounce rate dropped dramatically.

Using this trick to make Google Analytics more accurate is especially important if you are charging advertisers for banner space.  Your media kit will be grossly under-reporting the time people are spending engaged with your content if it contains statistics from Google Analytics and you haven’t added this code to your site.  I’m surprised Google doesn’t include this by default.

17 Responses to If You Use Google Analytics You Must Read This

  1. Laurence says:

    Thanks for the helpful Analytics tip, I have implemented it.

  2. Bill Hartzer says:

    You realize that this ‘tip’ was originally posted on that other guy’s site back in 2011, don’t you? It’s really nothing new. But still works.

    People have been having trouble implementing this code.

    Make sure that the code provided is put within the and tags, it has to be put within the ‘javascript’ in order to actually run. If you put the code right before the /body tag like the author suggests, the code won’t execute.

  3. admin says:

    Yes Bill, I am well aware that he posted this in 2011, I am perfectly capable of reading the post date at the top of his article. However, I suspect there are still a few people on earth who haven’t read his article… not everyone stays on top of everything like you do.

    Although clearly shows you didn’t add that code to your own site as of March 24th, but now it is there, so I’m guessing you added it before posting your helpful comment.

    Thanks for pointing out that his code needs to be wrapped in script tags, I’ll update my post to make sure people catch that.

  4. Thanks for sharing. This is really interesting. I have bookmarked this post for future reference and sharing with my family members and friends who have active websites.

    However, I have placed on my blog right now and if any of you can check and verify if the code I installed is correct?

    Does making 10000 to 5000 makes it to 5 seconds? I did by checking your source code. Just wondering if everything is alright?

    Thanks once again for this. It will definitely be helpful.

  5. Bill Hartzer says:

    Actually, I only noticed that it was an older post–there are so many gems out there like this that many haven’t seen before.

    Thanks for pointing it out, I figured I knew a lot about GA, but this is something that I didn’t know.

    I’m definitely testing this out thanks to your post!

  6. admin says:

    @Abdul – Your code looks good to me. Yes, I changed 10000 to 5000 to make it five seconds.

  7. Andreas J says:

    Highly useful, thanks!

  8. @admin
    Thanks very much for your swift response. Alright. I will put the code in same way on my other sites as well.

  9. I just came here to update you guys that this code is working perfectly. My bounce rate dropped from 60-65% to less than 20% for 2 days including today.

    Thanks very much once again 🙂

  10. Andrew Hyde says:

    Thanks, Surprised that Analytics isn’t more analytical.

  11. Rich says:

    I have tried everything I can to get this script to work and it simply doesn’t work…at least not for me. And yes I pasted the code right just as it is in the code box you have above – and yes it is appearing before tthe closing blody tag.

  12. admin says:

    Rich – What makes you say it isn’t working? Can you give a link to your site so I can check it out? Thanks.

  13. David says:

    Thanks for this.

    I’m fairly new to this and trying to catch up …… so, having implemented this code I’m now seeing a huge number of events created. Will it actually affect my Bounce Rate and Time on Site metrics directly or do I need to analyses these events tin some way?

    And … Am I now right in thinking then that those visits Google continues to report as bounces must be where the session ended within those first ten seconds?

  14. admin says:

    It will directly affect your bounce rate and time on site numbers within Analytics, you don’t have to do the calculations yourself.

    You are correct, this code will essentially change the definition of a bounce. It used to mean someone that visited a page on your site and left before visiting a second page. Now a bounce represents someone who left within the first 10 seconds.

  15. David says:

    Ok, quick update.

    Since installing this code my ‘bounces’ have gone down from 79% to 10% and average time on site from 1:41 to 11:47 .. fantastic!!

    However, what I have noticed is I am now getting what I can only describe as rogue records. These are having a pageviews value of zero and an average time on site of over 3 hours. I’m segmenting to ignore them, but they account for about 5% of visits

    These zero pageview records are only showing up since I implemented the script

    Any ideas what could be happening?

  16. admin says:

    Not sure. The way GA tracks a page view is with the following line of code in the standard tracking code that they give you:


    So if that failed, but you still managed to push the event in the code included in this article, it would probably register as a visit because of the event, but be missing the “pageview” part of the whole interaction.

    I don’t see how this code could cause a situation like that though. The only thing I could suggest without knowing your setup would be to have the standard tracking code in the header, and this event tracking code in the footer, so there’s some time between when the two push methods are fired. Not sure if that would make a difference, but worth a shot.

  17. Erwin says:

    Does this code works with the new Universal analytics tracking code?

Leave a Reply

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