简体   繁体   English

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

[英]How to use a variable in Google Analytics' Custom Variable

I'm trying to track a particular section of links on my page. 我正在尝试跟踪我页面上的特定链接部分。 I want to know what is clicked, so I'm trying to set up a click handler with jQuery to register a custom variable with Google Analytics, but it's not working. 我想知道点击了什么,所以我试图用jQuery设置一个点击处理程序,用Google Analytics注册一个自定义变量,但它不起作用。 Here's my code: 这是我的代码:

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

It's been about 3 or 4 days, and I haven't seen any custom variables register yet. 已经过了大约3或4天,我还没有看到任何自定义变量注册。 Maybe I can't have include a variable ($(this).text()) in the custom variable? 也许我不能在自定义变量中包含变量($(this).text())? Anyone tried this before? 以前有人试过吗?

It looks like you just need to change the order of a couple of lines--in particular, your Custom Variable must be set before _trackPageview is called . 看起来您只需要更改几行的顺序 - 特别是, 必须在 调用 _trackPageview 之前 设置自定义变量

The trackPageview() call aggregates the analytics data from various sources (cookies, location bar, request headers, DOM ), concatenates it and sends it to the Google Analytics servers in the Request Header (for the _utm.gif). trackPageview()调用聚合来自各种来源(Cookie,位置栏,请求标头,DOM)的分析数据,将其连接并将其发送到请求标头中的Google Analytics服务器(用于_utm.gif)。 So if your Custom Variable is not set when this function is called, it is not logged by GA. 因此,如果在调用此函数时未设置自定义变量,则GA不会记录它。

In this case, the value of your Custom Variable is triggered by an event (eg user clicking a button on the page), so just call _setCustomVar() when the event fires. 在这种情况下,自定义变量的值由事件触发(例如,用户单击页面上的按钮),因此在事件触发时只需调用_setCustomVar()。 Eg, 例如,

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

*my_function() of course, calls _setcustomVar* * my_function()当然是调用_setcustomVar *

I agree that for your situation, event tracking is indeed a better fit. 我同意,对于您的情况,事件跟踪确实更合适。 (A user may visit a page without clicking a link, or maybe click more than once?) Custom variables are handy if you know something about a user ahead of time, but as doug points out, you have to set them before calling _trackPageview . (用户可以在不点击链接的情况下访问页面,也可以多次点击?)如果您提前知道某个用户的某些内容,自定义变量很方便,但正如doug指出的那样,您必须在调用_trackPageview之前设置它们。

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
  _gaq.push(['_setCustomVar', 1, 'name-goes-here', 'value-goes-here']);
  _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>

On a side note, there is a known bug using custom variables when tracking characters that need to be URL encoded ( http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=2cdb3ec0be32e078&hl=en ). 另外,在跟踪需要进行网址编码的字符时,存在使用自定义变量的已知错误( http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=2cdb3ec0be32e078&hl=en )。

In your case, 'National Event' would be saved as 'National%20Event' using custom variables, which is not ideal either. 在您的情况下,“国家事件”将使用自定义变量保存为“National%20Event”,这也不理想。

If done properly, you can actually use both custom variables and event tracking within the same application, and build a custom report that references both data sources. 如果操作正确,您可以在同一个应用程序中实际使用自定义变量和事件跟踪,并构建一个引用这两个数据源的自定义报表。 ( http://code.google.com/apis/analytics/docs/gdata/gdataReferenceValidCombos.html ). http://code.google.com/apis/analytics/docs/gdata/gdataReferenceValidCombos.html )。

For my purposes, it was better for me to use Event Tracking and not a custom variable. 出于我的目的,我最好使用事件跟踪而不是自定义变量。 For tracking the clicks to a certain set of links, I could just use the following code: 为了跟踪某些链接的点击次数,我可以使用以下代码:

$(function(){
$('a.link-item.bullet').click(function(){
_gaq.push(['_trackEvent', 'National Link Event', $(this).text()]);
});
});

I'm not sure why this worked and not the custom variable function; 我不确定为什么这有效,而不是自定义变量函数; must be that _setCustomVar relies on the _trackPageview function and _trackEvent does not. 必须是_setCustomVar依赖于_trackPageview函数而_trackEvent不依赖。 This code appears after the _trackPageview function, and it works great: on a click, the event appears as a "National Link Event" with an action named after the link text. 此代码出现在_trackPageview函数之后,并且效果很好:在单击时,该事件显示为“国家链接事件”,其中的操作以链接文本命名。

I can still see why it would be useful to use variables in the _setCustomVar function, and I still haven't figured that one out, but I haven't any personal use for it yet. 我仍然可以看到为什么在_setCustomVar函数中使用变量会很有用,而我仍然没有想到它,但我还没有任何个人用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM