target = _blank不适用于GA出站链接跟踪

Cha*_*lls 6 javascript jquery google-analytics outbound event-tracking

我想跟踪出站链接的点击次数并实现以下代码:

GA代码

var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
Run Code Online (Sandbox Code Playgroud)

链接

<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>
Run Code Online (Sandbox Code Playgroud)

TARGET = _blank

target=_blank通过jQuery 添加属性,基于网站的访问者是否勾选复选框(然后将选择存储在cookie中).但是,如果我选择在新窗口中打开出站链接,则它不起作用.勾选复选框时,它会正确地将目标属性添加到链接中,但是当我单击链接时,它会在同一窗口中打开它.

与目标属性的链接

<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>
Run Code Online (Sandbox Code Playgroud)

任何的想法?

ale*_*exp 14

如果您通过更改document.location通过JavaScript更改页面URL,则链接上的target ="_ blank"将不会执行任何操作.

但是,当您跟踪内部链接时,您只需要使用hitCallback.如果您有外部链接,因此target ="_ 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;
}
Run Code Online (Sandbox Code Playgroud)

当你将其作为点击处理程序附加时

onclick="return trackOutboundLink(urlGoesHere, isExternalGoesHere)"
Run Code Online (Sandbox Code Playgroud)

更具体的例子:

<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>
Run Code Online (Sandbox Code Playgroud)


小智 5

只是想支持一些Guy In Winnipeg上面的回答.不会让我评论,但他的解决方案有效!

Google建议的代码(无法在新标签中打开链接)是:

var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
 });
}
Run Code Online (Sandbox Code Playgroud)

:

<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
Run Code Online (Sandbox Code Playgroud)

但是,如果您更改"document.location = url;" 到"document.location = href;" 并在链接标记中,更改"return false;" "回归真实" 并添加"target ="_ blank",链接将在新选项卡中打开,并跟踪出站链接.

所以,有效的代码是:

var trackOutboundLink = function(url) {
  ga('send', 'event', 'outbound', 'click', url, {
    'transport': 'beacon',
    'hitCallback': function(){document.location = href;}
 });
}
Run Code Online (Sandbox Code Playgroud)

:

<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return true;" target="_blank;">Check out example.com</a>
Run Code Online (Sandbox Code Playgroud)