How to Implement _trackPageview() with Google Analytics new Asynchronous Tab

Tracking pageviews in Google Analytics
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 + “’ type=’text/javascript’%3E%3C/script%3E”));

var pageTracker = _gat._getTracker(“UA-1111111-11”);


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&#8217; : ‘http://www&#8217;) + ‘’;
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?

2 thoughts on “How to Implement _trackPageview() with Google Analytics new Asynchronous Tab

  1. I use the custom URL schemes for easier tracking of the searchbox on the page, for instance “/search/en/12/searchquery”. Instantly, you can filter and funnel all the info you need, languages, returned searchresults and the query. This quickly shows what is missing on your page, where more content is needed and so on.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s