Accessibility Navigation

Asynchronous Virtual Pageviews with Google Analytics

Posted on 11th February 2011 by Jacob Wyke

If you have moved over to the new Asynchronous Google Analytics (GA) tracking code you may have trouble finding how to create virtual pageviews as the manual seems to push you more towards using Events instead.

How do I tell if I’m using the new code?

Google provides a good step by step guide to work out which tracking code you’re using. The gist of it comes down to looking for the following piece of code in your <head> section:

var _gaq = _gaq || [];

If you see that piece of code, or any reference to ‘_gaq‘, then you’re using the newer asynchronous code.

New Asynchronous Virtual Pageview Code

Now that you know you’re using the newer asynchronous code, you will need to use the following snippet in your links to track virtual pageviews.

<a href="" onClick="javascript: _gaq.push(['_trackPageview', '/virtual-page']);">My Link</a>

If you’re not a fan of embedding JavaScript directly into the page (for graceful degradation) then you can call the same method within your external function:

function trackVirtualPageview(strPage){
    _gaq.push(['_trackPageview', strPage]);
}

Have Your Say

Have Your Say Form









Comments

  • Be the first to add your voice to this post!

    Start the discussion and leave your views or thoughts by writing a comment using the form above.