Google Analytics(分析) - 如何使用新的异步代码跟踪页面名称?

时间:2011-04-07 20:18:04

标签: google-analytics google-analytics-api web-analytics

我使用了Google为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>

但Google已发布异步Javascript代码(如下)。

<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>

我的问题:在旧代码中我用PAGENAME跟踪一个页面(pageTracker._trackPageview(“PAGENAME”);)。我怎么能用新的异步代码呢?

2 个答案:

答案 0 :(得分:4)

您可以在异步跟踪代码中重写第三个代码行,并将其更改为以下内容:

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

记录在案here

答案 1 :(得分:3)

RTFM:http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._trackPageview

您的示例如下所示:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview', 'PAGENAME']);

  (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>

仅仅是指定第二个参数的情况。