[英]Adding an onclick jquery event to links that go to external sites
我需要將Google Analytics(分析)事件跟蹤添加到所有轉到外部域的鏈接。
我想我幾乎已經寫了代碼,但是我不知道在哪里添加!=
,到目前為止,我有以下內容:
$('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]);
}
});
使用JQuery選擇器僅獲取那些不去您的域的鏈接:
$('a:not([href^="http://mydomain.com"])').on('click', function() {
_gaq.push(['_trackEvent', 'extlink', 'click', this.href, 0, true]);
});
雖然,如果您需要外部鏈接,我認為這些鏈接應該以“ http://”開頭:
$('a[href^="http://"]').on('click', function() {
_gaq.push(['_trackEvent', 'extlink', 'click', this.href, 0, true]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.