【问题标题】:jQuery to track events with Google AnalyticsjQuery 使用 Google Analytics 跟踪事件
【发布时间】:2011-08-02 17:37:49
【问题描述】:

我正在尝试使用 jQuery 查找页面上的所有链接,然后在单击它们时使用 Google Analytics 跟踪事件。

我的链接部分正常工作,我只是没有看到跟踪工作事件触发(使用 httpfox 进行监控时)。

谁能看出我做错了什么?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery and GA</title>

<!-- This Jquery reference is already on the page -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<!-- This script block should go anywhere on the page, ideally the head, or really ideally in the a master scripts file -->
<script>
$(document).ready(function() {

// Each time a link is clicked, this function gets called
$('a').click(function(){

// Get the url
var url = this.href;        


// Set the category to External link, unless it matches the portal link/url pattern (containing /community/), 
// in which case category gets set to whatever word follows /community/ in the path.
var category = "External link";
var matches = url.match(/\/community\/([^\/]+)/);
if (matches) {
category = matches[1];
}
alert(category);


// Get the link text
var linkText = $(this).text().trim();
alert(linkText);


// Alert the url, just for the order below's sake
alert(url);


// Track the click
//$(this).attr("onclick","_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "'])");
_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "'])


//return false;

});
});
</script>

</head>

<body>

<h1>Simulating links on Site</h1>

<ul>
<li><a href="http://inet.asdf.asdf.pvt/portal/server.pt/community/home/_articleviewer?ArticleId=s_011610">Read more</a></li>

</ul>

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24902347-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);
})();

</body>
</html>

【问题讨论】:

    标签: javascript jquery google-analytics


    【解决方案1】:

    您向_gaq.push() 函数传递了太多参数。它只能接受 3 个字符串参数,但你传递了 4 个。

    _gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "'])
         //                  |    category   | action|     label        |    value   |
    

    将两个参数组合为一个,它会起作用(或者去掉“点击”字符串以进行操作,因为它是一种浪费)。但是,您也没有传递变量名;您只是传递与变量名称等效的字符串。看起来你实际上是在尝试做这样的事情:

    _gaq.push(['_trackEvent', category,  linkText , url]);
    

    【讨论】:

      【解决方案2】:

      yahelc 的回答应该可以帮助您在事件跟踪中获得正确的参数,但看起来您在 &lt;/body&gt; 之前缺少一个结束 &lt;/script&gt; 标记,这可能意味着 GA根本不工作。

      【讨论】:

        【解决方案3】:

        或者,您可以使用数据属性和 jquery 插件来简化整个事情:https://github.com/pascalvgemert/jquery-analytics-event-tracking

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-03
          • 2015-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-11
          相关资源
          最近更新 更多