Google Analytics:_setvar到新的跟踪代码

时间:2011-04-08 10:04:49

标签: javascript google-analytics

我一直在使用旧版本的分析代码,并使用以下内容跟踪不同类型的用户

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxxx");
pageTracker._setVar('memberlevel-2');    pageTracker._trackPageview();
} catch(err) {}</script>

如何在新的异步代码中使用它?谷歌分析论坛已经死了,我没有回复:(

2 个答案:

答案 0 :(得分:3)

尝试阅读此内容以设置自定义变量:

http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html

跟踪网页浏览的示例代码:

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

答案 1 :(得分:3)

_setVar仍然有效,但您应该使用_setCustomVar,因为它更强大。

以下是使用异步代码的旧代码:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_setVar', 'memberlevel-2']); 
  _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);
  })();

要使用setCustomVar,您可以这样做:

  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_setCustomVar', 1, 'memberlevel', '2', 3]);// page-level scope (3), in slot #1
  _gaq.push(['_trackPageview']);