Is your Google Analytics missing page views? Introducing Asynchronous Tracking

Reading time: About 2 minutes

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

  • Goes at the end of an HTML document, before </body>
  • Uses document.write to insert script
  • Doesn’t capture events before page load
1
2
3
4
5
6
7
8
9
<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

  • Goes at the beginning of an HTML document, before </head>
  • Uses a DOM injection to insert script (geeks should like that one)
  • Captures events prior to page load
1
2
3
4
5
6
7
8
9
10
11
12
13
<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.

5 comments skip to comment form

  1. Nick Yeoman said— 9 hours later

    I like to block javascript on my browser. It’s probably best to use a php based solution for analytics.

    #1
  2. Brian Cray said— 10 hours later

    Nick: People often check Google Analytics against their logs on occasion, but given that I’d say 99% (throwing out a number there) of people browse with javascript turned on, I think Google Analytics provides an adequate sample.

    #2
  3. pavlicko said— 1 day later

    I just installed the new asynchronous code on one of my clients sites, and I really like the way you can add multiple tracking objects through the command array. I’ll have to run some speed tests later this week, but so far so good.

    and by the way, Nick – Brian’s right. Using php isn’t really a viable solution for the millions of sites running a MS server, either. Besides, I’ve tried surfing the net w js disabled and sure its faster, but the whole web experience starts to suck pretty bad.

    #3
  4. Keith said— 7 months later

    @Nick: Have you found PHP libraries that will log data into GA?
    @pavlicko: IIS7 runs php

    #4
  5. Franz said— 7 months later

    Can anybody help me? I want to add custom variables that I extract from the site’s source code. That means I cannot call _setCustomVar() before calling _trackPageview() with the asynchronous method, as the site HTML isn’t loaded at that point yet.

    Am I doomed or how can I solve this?

    #5
  6. Respond to this post—

Return to navigation
1534