标签:javascript jquery google-analytics
我正在尝试使用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>
解决方法:
您向_gaq.push()函数传递了太多参数.它只能使用3个字符串参数,但是你传递了4个.
_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "'])
// | category | action| label | value |
将2个参数组合成一个,它将起作用(或摆脱“点击”字符串进行操作,因为它是浪费).但是,你也没有传递变量名;你只是传递相当于变量名的字符串.看起来你真的想要做这样的事情:
_gaq.push(['_trackEvent', category, linkText , url]);
标签:javascript,jquery,google-analytics 来源: https://codeday.me/bug/20190826/1734362.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。