javascript target=_blank 不适用于 GA 出站链接跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26938800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
target=_blank doesn't work with GA outbound link tracking
提问by Charles Ingalls
I want to track clicks on outbound links and implemented the following code:
我想跟踪出站链接的点击次数并实现了以下代码:
GA code
通用代码
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
function () {
document.location = url;
}
});
}
Links
链接
<a class="postLinks" href="<?php if (get_field('source_link')) echo get_field('source_link'); ?>" onclick="trackOutboundLink('<?php if (get_field("source_link")) echo get_field("source_link"); ?>'); return false;"><?php the_title(); ?></a>
target=_blank
目标=_blank
I add the target=_blank
attribute via jQuery based on whether the visitor to the website ticks a checkbox or not (the selection is then stored in a cookie). However, if I choose to open the outbound link in a new window it doesn't work. When ticking the checkbox it does correctly add the target attribute to the link but when I click on the link it opens it in the same window.
我target=_blank
根据网站访问者是否勾选复选框通过 jQuery添加属性(然后将选择存储在 cookie 中)。但是,如果我选择在新窗口中打开出站链接,则它不起作用。勾选复选框时,它确实将目标属性正确添加到链接,但是当我单击链接时,它会在同一窗口中打开它。
Links with target attribute
具有目标属性的链接
<a class="postLinks" href="<?php if (get_field('source_link')) echo get_field('source_link'); ?>" onclick="trackOutboundLink('<?php if (get_field("source_link")) echo get_field("source_link"); ?>'); return false;" target="_blank"><?php the_title(); ?></a>
Any idea?
任何的想法?
回答by alexp
Having target="_blank" on a link will not do anything if you're changing the page URL via JavaScript by changing document.location.
如果您通过 JavaScript 更改 document.location 来更改页面 URL,那么在链接上设置 target="_blank" 将无济于事。
However you only need to use the hitCallback when you're tracking an internal link. If you have an external link, and therefore target="_blank", your original tab stays open, and the ga tracking event will complete as normal - you don't have to worry about making sure it finishes before loading the new page.
但是,您只需要在跟踪内部链接时使用 hitCallback。如果您有外部链接,因此 target="_blank",您的原始选项卡将保持打开状态,并且 ga 跟踪事件将正常完成 - 您不必担心在加载新页面之前确保它完成。
So I think you'd want to change your click handler to be this:
所以我认为你想改变你的点击处理程序是这样的:
var trackOutboundLink = function(url, isExternal) {
var params = {};
if (!isExternal) {
params.hitCallback = function () {
document.location = url;
}
}
ga('send', 'event', 'outbound', 'click', url, params);
return isExternal;
}
And when you attach this as the click handler
当您将其附加为点击处理程序时
onclick="return trackOutboundLink(urlGoesHere, isExternalGoesHere)"
More concrete examples:
更具体的例子:
<a href="/" onclick="return trackOutboundLink('/', false)">An internal link</a>
<a href="http://www.example.com/" onclick="return trackOutboundLink('http://www.example.com', true)">An external link</a>
回答by Ted Converse
Just want to support Some Guy In Winnipeg's answer above. Won't let me comment, but his solution works!
只是想支持上面温尼伯的一些人的回答。不会让我发表评论,但他的解决方案有效!
Google's suggested code (does not work to open link in new tab) is:
Google 的建议代码(无法在新标签页中打开链接)是:
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = url;}
});
}
:
:
<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
However, if you change "document.location = url;" to "document.location = href;" and in the link tag, change "return false;" to "return true;" and add "target="_blank", the link will open in a new tab, and track the outbound link.
但是,如果您更改“document.location = url;” 到“document.location = href;” 并在链接标签中,更改“return false;” “返回真;” 并添加“target =“_blank”,链接将在新选项卡中打开,并跟踪出站链接。
So, the code that works is:
因此,有效的代码是:
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = href;}
});
}
:
:
<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return true;" target="_blank;">Check out example.com</a>
回答by Jon
This will make all links open in a new window:
这将使所有链接在新窗口中打开:
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){window.open(url);}
});
}
I just changes document.location = url;
to window.open(url);
code from https://support.google.com/analytics/answer/1136920
我只是改变了document.location = url;
对window.open(url);
从代码https://support.google.com/analytics/answer/1136920
you can also change the function name,and have one for new window links, and one for same window links. That would be change the 1st line to something like:
您还可以更改函数名称,一个是新窗口链接,一个是同窗口链接。这会将第一行更改为:
var trackOutboundNewWindow = function(url) {
And then the link would be
然后链接将是
<a href="http://www.example.com" onclick="trackOutboundNewWindow('http://www.example.com'); return false;">Check out example.com</a>
回答by Some Guy in Winnipeg
Found a solution (as of Feb 6 2016)
找到解决方案(截至 2016 年 2 月 6 日)
<script>
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = href;}
});
}
</script>
Then make your link look like this...
然后让你的链接看起来像这样......
<a href="http://www.example.com" onclick="trackOutboundLink('label name'); return true;" target="_blank">text</a>
回答by jenson-button-event
DRY - track all anchor's with a 'tracked
' class. Note this code is sensitive to popup blockers. The window.open
call needs to be outside the ga
call.
DRY - 使用“ tracked
”类跟踪所有锚点。请注意,此代码对弹出窗口阻止程序敏感。该window.open
调用需要外界ga
通话。
/**
* Function that tracks a click on an outbound link in Analytics.
*/
$(function() {
//only create event tracking if ga has loaded
ga(function(){
$("a.tracked").click(function(e) {
var url = $(this).attr("href");
var newWindow = ($(this)[0].target || '').toLowerCase() === '_blank';
if(newWindow) {
window.open(url, '_blank');
}
ga("send", "event", "outbound", "click", url, {
"hitCallback": function () {
if(!newWindow) document.location = url;
}
});
e.preventDefault();
});
});
});
回答by Vincent P.
this works:
这有效:
hitCallback': function(){window.open(url);}
Hope, that the event tracking is not affected in any way.
希望事件跟踪不会受到任何影响。