Creating Virtual Pageviews with Google Analytics Asynchronous Tracking
Last week Google announced their asynchronous tracking for Google Analytics. This tracking is different in many ways: loads faster; code can be placed in the header rather than the bottom of the page; better uptime; and more. With the ability to place the analytics in the header the data is being recorded accurately because it is the first thing to load when someone visits the page. The previous snippet was placed at the bottom of the page; we would have to wait until the other javascripts were executed before it loaded. That meant if some other javascript was broken or someone left the page quickly, Google Analytics would not record the data.
Google has issued new instructions on how to create specialized implementations; however, one of the most commonly used features—virtual pageviews—is left out of the instructions. Virtual pageviews allows the analytics admin to track various elements of the page such as outbound clicks, form fields, downloads, etc.
By modifying the event tracking, I was able to create virtual pageviews.
The examples below show the original and async analytics code, as well as instructions for implementing virtual pageviews.
Old Google Analytics Script with placement
<!--Google Analytics--> <script type="text/javascript"><!--mce:0--></script> <script type="text/javascript"><!--mce:1--></script> <!--Google Analytics-->
New Asynchronous Google Analytics Script with placement
<!--Google Analytics--> <script type="text/javascript"><!--mce:2--></script> <!--Google Analytics-->
Original Script for How to Implement a Virtual Pageview
<a onclick="javascript: pageTracker._trackPageview ('/easyidentifier');" href=" http://www.someurl.com/">Click Here</a>
New Asynchronous Tracker Script for Google Analytics
<a onclick="javascript: _gaq.push(['_trackPageview', '/easyidentifier']);" href="http://www.someurl.com/">Click Here</a>
From the above examples, place original and async analytics in the header and replace the pagetracker part of the code with _gaq.push. The virtual pageviews are still placed within the element that you wish to track. The ”easyidentifier” in the virtual pageview signifies what the link will be titled as within the reports. Remember to mark your virtual pageviews with some kind of identifier to distinguish it from your actual pages (e.g., /outbound,/link or /external). This will make it easier to find them and possibly make into a goal.
If you need help with implementation of GA’s new features, write out your issues in the comments section and I’ll be happy to walk you through them.
~Lorrin



on December 11th, 2009 at 9:43 am
[...] Creating Virtual Pageviews with Google Analytics Asynchronous Tracking [...]
on January 9th, 2010 at 9:07 pm
Thanks For this code snipet. I use virtual pageviews all the time .. I wish Google would have clarifeid this in their help docs but instead I found it on your site .. Thanks!
If you name all of your outgoing links or affiliate links the same — you can some some fancy jquery code to save you time http://fieldsmarshall.com/8-tips-for-tracking-with-google/
anyways thanks
on January 21st, 2010 at 3:41 pm
[...] Creating Virtual Pageviews with Google Analytics Asynchronous Tracking [...]
on February 2nd, 2010 at 3:51 am
[...] Usage GuideMake Google Analytics Load Faster With Asynchronous Tracking - by Patrick Altoft Creating Virtual Pageviews with Google Analytics Asynchronous Tracking -by Lorrin AKPC_IDS += "1195,";If You Enjoyed This Post Subscribe to Our RSS Feed!!!Share This [...]
on February 3rd, 2010 at 8:18 pm
FYI – the javascript: protocol prefix is 100% unnecessary
on February 4th, 2010 at 10:40 am
Hi Brian,
I use the javascript: protocol because when I do not use it the virtual pageview does not work. Only with this protocol I can guarantee that it will work. Thanks
on May 17th, 2010 at 4:17 pm
Thanks, really helpful. I couldn´t find the new virtual pageviews in Google´s pages.
on June 17th, 2010 at 11:28 am
Thanks for this tip. It bugs me VPV were “dropped” for the async code. If for no other reason than it’s the most effective way to tie certain actions into goals. If they make it so you can turn certain events into goals, then I’ll be happy only using event tracking. Until then, I hope this method works.
Thanks Lorrin!
on July 7th, 2010 at 6:17 am
Helpful.
In my case, I’m interested in tracking clicks on outgoing links. An example of this approach is shown at http://www.google.com/support/analytics/bin/answer.py?hl=en-uk&answer=55527 .
I’m wondering if it is still desirable to add a delay when using the new asynchronous GA code — and, if so, are there any recommendations for a delay function for use with the new asynchronous GA code?
TIA and Best Regards!
on July 7th, 2010 at 5:47 pm
I asked (if a delay is still required) in the GA Help Forum; the answer was “No”.
on July 26th, 2010 at 6:26 am
Does anyone know if i can use this snippet with the ‘onsubmit’ function within the tag?