How to Implement _trackPageview() with Google Analytics new Asynchronous Tab
If you are planning your migration to the new asynchronous Google Analytics tag then you probably need to migrate your override of the _trackPageview() code you set up using the old non-asynchronous code.Or perhaps you migrated your web analytics already and just realized that you didn’t code your new Google Analytics tag correctly. Either way, here’s your answer.
Old code sample: Synchronous
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"));var pageTracker = _gat._getTracker("UA-1111111-11");pageTracker._trackPageview('/landingpages/goal1.html');</script>
New code sample: Asynchronous
var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-1136567-11']);_gaq.push(['_trackPageview', '/landingpages/goal1.html']); (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';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();
Why override your trackPageview?
If you don’t change the default _trackPageview()command then the page recorded into your Google Analytics reports will be the fully-qualified URL of the page with the tag. This is usually fine for most implementations. But there are times when you need to control the page name being logged.
Why do people override page name
- Dynamic pages
Depending on your website’s technology, some pages (or the code that implements them) may show the same URL even though the pages are logically different and distinct to the website visitor. In those cases you need to have your webserver also assign a unique page name to each of the logical instances.
- Goal/thank you page
When you use goals you sometimes come across the need to define the goal in Google Analytics in advance of knowing the fully-qualified URL. Thus you need to pre-define the URL and have the web page assign it regardless of the where the page eventually resides.
I’d be interested in your experience with _trackPageview().
- Why do you use it over the default URL method?
- Are there any other gotchas migrating to the new Google Analytics asynchronous tag?