我正在尝试跟踪我页面上的特定链接部分。我想知道点击了什么,所以我尝试使用 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>
大概有3、4天了,还没有看到自定义变量注册。也许我不能在自定义变量中包含变量 ($(this).text())?以前有人试过这个吗?
最佳答案
看起来您只需要更改几行的顺序——特别是,您的自定义变量必须设置之前 _trackPageview 被调用。
trackPageview() 调用聚合来自各种来源(cookie、地址栏、请求 header 、DOM)的分析数据,将其连接起来并将其发送到请求 header (对于 _utm.gif)中的 Google Analytics 服务器。因此,如果在调用此函数时未设置您的自定义变量,则 GA 不会记录它。
在这种情况下,自定义变量的值由事件触发(例如,用户单击页面上的按钮),因此只需在事件触发时调用 _setCustomVar() 即可。例如,
<input type="button" name="National Link", value=[dynamically set], onclick="my_function()", 2>
*my_function() 当然会调用 _setcustomVar*
关于javascript - 如何在 Google Analytics 的自定义变量中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4032998/