0

I'm building a simple bit of jQuery to use with Google events tracking, and everything works up until the ga function -- I thought I could just place the variables in the function, but it's not working. I know because if I use an alert to display the variables before the ga function, I get the alert. But, if I place the alert after the line, it doesn't work.

jQuery('.button[data-track="true"').click(function() {
        var eventCategory = jQuery(this).data("category");
        var eventAction = jQuery(this).data("action");
        var eventLabel = jQuery(this).data("label");
        ga('send', 'event', eventCategory, eventAction, eventLabel);
});

This is an example Google gives:

$('#button').on('click', function() {
  ga('send', 'event', 'button', 'click', 'nav-buttons');
});

Anyway to use the variables in the ga function?

4

2 回答 2

1

确保您对 Google API 的引用位于需要它的 JS 之前。

于 2013-10-13T19:16:19.920 回答
1

ga()调用中使用变量应该没有问题。好像ga()还没有定义。

  1. 您是否在 Firebug 控制台或 Chrome 开发工具控制台中看到任何错误?
  2. ga()是新的 Universal Analytics ( analytics.js) 的一部分——您脑子里已经有了 analytics.js 的代码片段,而不是 ga.js,对吧?
  3. 您应该能够ga在控制台窗口(Firebug 或 Chrome 开发工具)中键入并查看函数定义。
于 2013-10-13T19:34:18.307 回答