Brian Cray ··· Home > Blog > Analytics > Is your Google Analytics missing page views? Introducing Asynchronous Tracking

Is your Google Analytics missing page views? Introducing Asynchronous Tracking

What happens to your Google Analytics if someone leaves the page before it has fully loaded? The page view doesn't get recorded and Google Analytics fails as an accurate source of user data.

Asynchronous Tracking for Google Analytics

Google has fixed missing data issues with Asynchronous Tracking. Just read this quote straight from the Asynchronous Tracking documentation:

"Even if [a] button is clicked before the browser has finished loading ga.js, the event will be captured and eventually executed. Using traditional tracking, the browser might throw an exception in this situation."

While this quote refers to event tracking, I believe it applies to any of Google Analytics' tracking methods, including page views.

Google Analytics Asynchronous Tracking works by queuing up tracking commands for execution when the analytics script finishes loading. Since the Asynchronous Tracking code is put in the <head> of an HTML document, it will always finish loading in time.

Comparing traditional vs. Asynchronous Tracking code

Traditional tracking code for Google Analytics

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}</script>

Asynchronous Tracking code for Google Analytics

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

</script>

Begin using Google Analytics Asynchronous Tracking today

I became aware of Asynchronous Tracking thanks to this article on Web Resources Depot, and I've been using it since. So far there have been no issues with tracking or loading times. In fact, there seems to be improvements in load times and reporting.

If you do not use advanced tracking options such as event tracking or custom variables, just replace your current tracking code with the Asynchronous Tracking code (above) and you're done!

If you do use advanced tracking options make sure you read over the Asynchronous migration examples and migrate all of your tracking code to the new format.