Search code examples
google-analyticsgoogle-analytics-apiweb-analytics

Google Analytics - how can I track a page name using the new asynchronous code?


I was using the below code provided by Google for Google Analytics.

<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"));
         ry {
                var pageTracker = _gat._getTracker("UA-XXXXX-X");
                pageTracker._trackPageview("PAGENAME");
            } catch (err) { }
 </script>

But Google has released Asynchronous Javascript Code (below).

<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';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

My QUESTION is: in the old code I used to track a page with PAGENAME (pageTracker._trackPageview("PAGENAME");). How can I do it using the new asynchronous code?


Solution

  • You can rewrite that third code line in the async tracking code and change it to the following:

    _gaq.push(['_trackPageview', 'PAGENAME']); 
    

    This is documented here