使用jQuery跟踪事件并使用Google Analytics

3
我正在尝试使用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>
3个回答

4

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

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

将其中的两个参数合并成一个即可,这样就能正常工作了(或者去掉动作中的“click”字符串,因为它是多余的)。但是,你没有传递变量名,只是传递了等同于变量名的字符串。看起来你实际上是想要做这样的事情:
_gaq.push(['_trackEvent', category,  linkText , url]);

1

yahelc的回答应该能帮助您将正确的参数传递到事件跟踪中,但是看起来在</body>之前缺少一个闭合的</script>标签,这很可能意味着GA还没有开始工作。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接