[英]Adding an onclick jquery event to links that go to external sites
I need to add Google Analytics event tracking to all links that go to external domains. 我需要将Google Analytics(分析)事件跟踪添加到所有转到外部域的链接。
I think I've almost wrote the code but I don't know where to add the !=
, so far I have the below: 我想我几乎已经写了代码,但是我不知道在哪里添加!=
,到目前为止,我有以下内容:
$('a').click (function(){
if(window.location.href.indexOf("http://mydomain.com") >1 && $(this).attr("id")){
_gaq.push(['_trackEvent', 'extlink', 'click', $(this).attr('id'),0,true]);
} else {
_gaq.push(['_trackEvent', 'extlink', 'click', $(this).attr('href'),0,true]);
}
});
Use the JQuery selector to get only those links not going to your domain: 使用JQuery选择器仅获取那些不去您的域的链接:
$('a:not([href^="http://mydomain.com"])').on('click', function() {
_gaq.push(['_trackEvent', 'extlink', 'click', this.href, 0, true]);
});
Although, if you want external links, I'd think those would be the ones that start with "http://": 虽然,如果您需要外部链接,我认为这些链接应该以“ http://”开头:
$('a[href^="http://"]').on('click', function() {
_gaq.push(['_trackEvent', 'extlink', 'click', this.href, 0, true]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.