在新窗口中打开外部链接并跟踪出站点击事件 [英] Open external link in new window and track outbound click event
本文介绍了在新窗口中打开外部链接并跟踪出站点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码来追踪谷歌分析中的出站链接(在本网站的某处找到它)。我有两个问题:
- 有时,输出的e.currentTarget.host部分显示我自己的域 - 而不是显示点击导致的域。任何想法为什么我的域名有时会出现?
-
是否可以修改此代码以执行以下操作(1)强制链接在新窗口中打开和(2)如图所示出站点击事件。
$(function(){
$(a)。on 'click',function(e){
var url = $(this).attr(href);
if(e.currentTarget.host!= window.location.host){
_gat._getTrackerByName()._ trackEvent(Outbound Links,e.currentTarget.host,url,0);
if(e.metaKey || e.ctrlKey){
var newtab = true ;
}
if(!newtab){
e.preventDefault();
setTimeout('document.location =''+ url +'',100);
}
}
});
}); <
解决方案
- 代码比较链接中的域使用窗口域:
e.currentTarget.host!= window.location.host
- 如果您的域名有www前缀(例如www.domain.com
),但有一些链接没有它(如domain.com/link。它们会被认为是一个外部链接。
- 原始代码在设置document.location之前使用了一个延迟,以便为_trackEvent的跟踪请求留出时间。由于您希望所有非现场链接在新窗口中打开,因此可以消除延迟。 任何www或子域前缀:
jQuery(function($){
$('a [href ^ =http://],a [href ^ =https://]')
.not('[href * =mydomain.com]')
.click (函数(e){
var url = this.href;
_gat._getTrackerByName()._ trackEvent(Outbound Links,e.currentTarget.host,url,0);
e .preventDefault();
window.open(url);
})
});
-
$('a [href ^ = http://],a [href ^ =https://]')
- 选择以开头的链接http://
或https://
。这应该包括所有出站链接。 -
.not('[href * =mydomain.com]')
- 抛出包含mydomain.com
的任何链接,以防有任何以开头的内部链接http://
... -
e.preventDefault();
- 防止正常关注链接。
此外,如果您使用的是当前的异步Google Analytics代码,则可以将_trackEvent调用缩短为
_gaq.push(['_ trackEvent','Booking',Outbound Links,e.currentTarget.host,url,0]);
I am using the code below to track outbound links in google analytics ( found it somewhere on this site ). I have 2 issues coming up:
- Sometimes, the e.currentTarget.host part of the output shows my own domain - instead of showing the domain where the click leads to. Any idea why my domain shows up on occasion ?
Is it possible to modify this code to do the following (1) force link to open in new window and (2) track the outbound click event as shown.
$(function() { $("a").on('click',function(e){ var url = $(this).attr("href"); if (e.currentTarget.host != window.location.host) { _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0); if (e.metaKey || e.ctrlKey) { var newtab = true; } if (!newtab) { e.preventDefault(); setTimeout('document.location = "' + url + '"', 100); } } });
});
解决方案- The code compares the domain in the link with the window domain:
e.currentTarget.host != window.location.host
-- If your domain has a www prefix (likewww.domain.com
) but have some links without it (likedomain.com/link.html
), they'd be considered an external link. - The original code uses a delay before setting document.location to allow time for the _trackEvent's tracking request. Since you want all off-site links to open in a new window, the delay can be eliminated.
The following code should work regardless of any www or sub-domain prefix:
jQuery(function($) { $('a[href^="http://"],a[href^="https://"]') .not('[href*="mydomain.com"]') .click(function(e) { var url = this.href; _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0); e.preventDefault(); window.open(url); }) });
$('a[href^="http://"],a[href^="https://"]')
-- select links that start withhttp://
orhttps://
. This should be include all outbound links..not('[href*="mydomain.com"]')
-- throw out any links containingmydomain.com
, just in case there are any internal links that start withhttp://
...e.preventDefault();
-- prevent link from being followed normally.
Also, if you're using the current, asynchronous Google Analytics code, you can shorten the _trackEvent call to
_gaq.push(['_trackEvent', 'Booking', "Outbound Links", e.currentTarget.host, url, 0]);
这篇关于在新窗口中打开外部链接并跟踪出站点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
查看全文