目标= _blank不适用于GA出站链接跟踪 [英] target=_blank doesn't work with GA outbound link tracking

查看:148
本文介绍了目标= _blank不适用于GA出站链接跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪点击出站链接并实施以下代码:

$ b

GA代码

b $ b

  var trackOutboundLink =函数(url){
ga('send','event','outbound','click',url,{'hitCallback ':
function(){
document.location = url;
}
});
}

链接

 < a class =postLinkshref =<?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

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

具有目标属性的链接

  < a class =postLinkshref =<?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> 

任何想法?

解决方案

如果您通过改变document.location通过JavaScript更改页面URL,那么在链接上拥有target =_ blank将不会执行任何操作。

但是当你跟踪一个内部链接时,你只需要使用hitCallback。如果您有外部链接,因此target =_ blank,您的原始选项卡将保持打开状态,ga跟踪事件将按正常完成 - 您无需担心在加载新页面之前确保它完成。 / p>

所以我想你应该改变你的点击处理程序是这样的:

  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)>内部链接< / a> 
< a href =http://www.example.com/onclick =return trackOutboundLink('http://www.example.com',true)>外部链接< / a> ;


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

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.

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?

解决方案

Having target="_blank" on a link will not do anything if you're changing the page URL via JavaScript by changing document.location.

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.

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>

这篇关于目标= _blank不适用于GA出站链接跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆