Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 目标=_blank不';不能使用GA出站链接跟踪_Javascript_Jquery_Google Analytics_Event Tracking_Outbound - Fatal编程技术网

Javascript 目标=_blank不';不能使用GA出站链接跟踪

Javascript 目标=_blank不';不能使用GA出站链接跟踪,javascript,jquery,google-analytics,event-tracking,outbound,Javascript,Jquery,Google Analytics,Event Tracking,Outbound,我想跟踪出站链接上的点击,并实现了以下代码: GA代码 var trackOutboundLink = function(url) { ga('send', 'event', 'outbound', 'click', url, {'hitCallback': function () { document.location = url; } }); } <a class="postLinks" href="<?php if (get_fiel

我想跟踪出站链接上的点击,并实现了以下代码:

GA代码

var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
<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>
链接

<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>
有什么想法吗?

如果您通过JavaScript更改document.location来更改页面URL,那么在链接上设置target=“\u blank”将不会起任何作用

但是,您只需要在跟踪内部链接时使用hitCallback。如果您有一个外部链接,因此target=“\u blank”,则您的原始选项卡将保持打开状态,ga跟踪事件将正常完成-您不必担心在加载新页面之前确保其完成

因此,我认为您应该将单击处理程序更改为:

var trackOutboundLink = function(url, isExternal) {
    var params = {};

    if (!isExternal) {
        params.hitCallback = function () {
            document.location = url;
        }
    }
    ga('send', 'event', 'outbound', 'click', url, params);

    return isExternal;
}
当您将其作为单击处理程序附加时

onclick="return trackOutboundLink(urlGoesHere, isExternalGoesHere)"
更具体的例子:

<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>

这将使所有链接在新窗口中打开:

    var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){window.open(url);}
   });
}
我只是更改
document.location=url
窗口。打开(url)源代码

您还可以更改函数名,其中一个用于新窗口链接,另一个用于相同的窗口链接。这将把第1行更改为:

var trackOutboundNewWindow = function(url) {
然后链接将是

<a href="http://www.example.com" onclick="trackOutboundNewWindow('http://www.example.com'); return false;">Check out example.com</a>

找到了解决方案(截至2016年2月6日)


var trackOutboundLink=函数(url){
ga(‘发送’、‘事件’、‘出站’、‘单击’、url、{
“传输”:“信标”,
“hitCallback”:函数(){document.location=href;}
});
}
然后让你的链接看起来像这样

<a href="http://www.example.com" onclick="trackOutboundLink('label name'); return true;" target="_blank">text</a>

这项工作:

hitCallback': function(){window.open(url);}

希望,事件跟踪不会受到任何影响。

只想支持温尼伯回答中的某个人。不让我评论,但他的解决方案有效

谷歌建议的代码(无法在新选项卡中打开链接)是:

:

:


干式-使用“
跟踪”
”类跟踪所有锚。注意:此代码对弹出窗口拦截器敏感。
窗口.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();
        });
    });
});

谢谢亚历克斯!我唯一想知道的是:链接将始终是一个外部链接。但是,有时它将在新选项卡中打开,有时它将在同一选项卡中打开(由用户选择)。我是否需要将“URL”和“isExternal”的相同URL传递给trackOutboundLink()函数?我的意思是isExternal是一个布尔值,用于判断链接是否在新窗口中打开。从PHP设置URL的方法与从PHP设置URL的方法相同。确切的实现将取决于您用于确定外部链接与内部链接的任何标准。'hitCallback':function(){if(target.attr('target')!=='u blank'){window.location.href=url;}您可能错过了很多事件,我认为hitCallback的全部目的是在新窗口中打开接收到的链接。因此,如果同时使用return true,它更像是一个冗余步骤。return false表示取消导航,所以go()函数可以执行此操作。尽管控制台中总是出现“Uncaught ReferenceError:href未在hitCallback定义”错误,但此操作非常有效。出现错误“Uncaught ReferenceError:href未在hitCallback定义”
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>
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>
/**
* 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();
        });
    });
});