Brian Cray ··· Home > Blog > Javascript, Analytics > Time On Site & Bounce Rate: Get the real numbers in Google Analytics

Time On Site & Bounce Rate: Get the real numbers in Google Analytics

Google Analytics has a major problem with the way it calculates its Time On Site metric: its based on the length of time between a user entering your site and their last page view. Can't see the holes yet? Here are 2 scenarios where this becomes a major problem (Bob will be our user):

Worst case scenario

In the above scenario Bob enters your site, loyally reads an article for 2 minutes and 13 seconds, saves it to his browser boomarks, then leaves. Can you see the problem? Bob never interacted with the webpage. To Google, this is a bounce. And bounced visits get marked with 0:00 Time On Site. In other words, your Bounce Rate just got inflated and your Avg. Time On Site just got its ass kicked.

Another bad scenario

Let's say Bob enters your site and even visits a second page, spending a total of 2 minutes and 13 seconds on your site before leaving. But Google messes this up too: since Bob never interacted with the 2nd page, Google doesn't know how long he spent on the second page. Therefore, Time On Site = Visit to second page - Entrance time = 1 minute and 11 seconds, not 2 minutes and 13 seconds.

Google's calculation of Time On Site and Bounce Rate is limited by its unit of measure: the Pageview. But Time On Site has very little to do with pageviews and Bounce Rates—defined as "the percentage of people who come to your website and leave instantly" by Avinash Kaushik—has nothing to do with pageviews.

Solution

With Google's Event Tracking API, we'll tell Google Analytics that Bob is still on our site every 10 seconds. To make things even better, an event is an engagement equivalent to a pageview, so it too increases Time On Site and unbounces the visit. The result? A more accurate picture of user engagement.

Javascript magic

Paste this javascript snippet right before </body> in your HTML pages. It won't matter if you're using the new or old tracking code.

(function (tos) {
  window.setInterval(function () {
    tos = (function (t) {
      return t[0] == 50 ? (parseInt(t[1]) + 1) + ':00' : (t[1] || '0') + ':' + (parseInt(t[0]) + 10);
    })(tos.split(':').reverse());
    window.pageTracker ? pageTracker._trackEvent('Time', 'Log', tos) : _gaq.push(['_trackEvent', 'Time', 'Log', tos]);
  }, 10000);
})('00');

Once this code is installed, your site will update Google Analytics every 10 seconds under the Event Category "Time", the Event Action "Log", and the Event Value will be based on the pattern of 0:10, 0:20, 0:30, 0:40, 0:50, 1:00, 1:10, etc.

Conclusion

Although Google Analytics has its flaws, it compensates in part with a powerful tracking API that keeps getting better. The snippet I've written above can more than solve its Time On Site and Bounce Rate reporting issues. Furthermore, if you're a Google Analytics ninja (or ninja in training), break out the event values into a histogram that shows the distribution of site visit duration. Cheers!