如何在Google Analytics的自定义变量中使用变量

boz*_*doz 2 html javascript jquery google-analytics

我正在尝试跟踪我页面上的特定链接部分.我想知道点击了什么,所以我试图用jQuery设置一个点击处理程序,用Google Analytics注册一个自定义变量,但它不起作用.这是我的代码:

  <!--Google Analytics-->
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18698622-1']);
  _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);
  })();

$(function(){
$('a.link-item.bullet').click(function(){
_gaq.push(['_setCustomVar', 1, 'National Link', $(this).text(), 2]);
});
});
</script>
Run Code Online (Sandbox Code Playgroud)

已经过了大约3或4天,我还没有看到任何自定义变量注册.也许我不能在自定义变量中包含变量($(this).text())?以前有人试过吗?

dou*_*oug 7

看起来您只需要更改几行的顺序 - 特别是,必须在 调用 _trackPageview 之前设置自定义变量.

trackPageview()调用聚合来自各种来源(Cookie,位置栏,请求标头,DOM)的分析数据,将其连接并将其发送到请求标头中的Google Analytics服务器(用于_utm.gif).因此,如果在调用此函数时未设置自定义变量,则GA不会记录它.

在这种情况下,自定义变量的值由事件触发(例如,用户单击页面上的按钮),因此在事件触发时只需调用_setCustomVar().例如,

<input type="button" name="National Link", value=[dynamically set], onclick="my_function()", 2>
Run Code Online (Sandbox Code Playgroud)

*my_function()当然是调用_setcustomVar*