简体   繁体   English

谷歌分析链接跟踪javascript剥离联盟链接

[英]Google analytics link tracking javascript strips affiliate links

We have an affiliate websites and we are attempting to track the clicks to outbound affiliates sites. 我们有一个联盟网站,我们正在尝试跟踪出站联属网站的点击次数。 I inserted the below code into our html. 我将下面的代码插入到我们的html中。

In header: 在标题中:

<script>
/**
* Function that tracks a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>

In the body on the link: 在链接的正文中:

<a href="http://domain.com?aff_link=123456" onclick="trackOutboundLink('http://domain.com'); return false;"> The link </a>

This script was correctly tracking affiliate link clicks but managed to strip the affiliate code from the domain when the user clicked. 此脚本正确跟踪联盟链接点击,但设法在用户点击时从域中删除联盟代码。 In other words 换一种说法

http://domain.com?aff_link=123456

became 成为

http://domain.com

As a result no commissions were tracked. 因此,没有跟踪任何佣金。 Does anyone know possibly why this is happening and how to track the links with GA and keep the affiliate links in tact? 有没有人知道为什么会发生这种情况以及如何跟踪与GA的链接并保持联盟链接的机智?

Thanks! 谢谢!

So the problem is that you pass a url without affiliate information in onclick="trackOutboundLink('http://domain.com') ... 所以问题是你在onclick="trackOutboundLink('http://domain.com')传递了一个没有联盟信息onclick="trackOutboundLink('http://domain.com') ...

This, instead, should work: 相反,这应该工作:

onclick="trackOutboundLink(this.href); return false;"

This should use the value of the link's "href" attribute. 这应该使用链接的“href”属性的值。

This being said, I would strongly suggest using native DOM events instead of the html "onclick" attribute. 话虽如此,我强烈建议使用原生DOM事件而不是html“onclick”属性。

For instance, using jQuery: 例如,使用jQuery:

$("a").click(function(event) {
    event.preventDefault();
    trackOutboundLink(this.href);
});

UPDATE UPDATE

The main benefit of the event listener approach (second option) is that there can be multiple event listeners on the same DOM node, for the same event. 事件侦听器方法(第二个选项)的主要好处是,对于同一事件,同一DOM节点上可以有多个事件侦听器。 This SO answer explains it very well, in great details. 这个SO答案很好地解释了它,非常详细。

Hope this helps! 希望这可以帮助!

So you want to track the full url instead of the root path. 因此,您希望跟踪完整的URL而不是根路径。 Please try to use this one. 请尝试使用这个。

var trackOutboundLink = function(event) {
   event.preventDefault();
   var url = event.target.href;
   ga('send', 'event', 'outbound', 'click', url, {
      'transport': 'beacon',
      'hitCallback': function(){document.location = url;}
   });
}

I hope it would help you. 我希望它会对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM