我已设置Google Analytics跟踪outbound links。但是,我还安装了一个名为Amazon Link Localizer的WordPress插件,该插件修改了外发网址,使其最终格式为helloworld ()
而不是prourls.com?url=www.amazon.com
。
是否可以修改Google Analytics脚本,以便仍然可以跟踪这些出站链接点击?
答案 0 :(得分:3)
第1步:将trackOutboundLink
脚本添加到您的网站。
在页面的HTML {em>的<head></head>
部分添加此代码(完整脚本位于此页面底部,但使用此脚本可帮助最小化页面大小) < / p>
<script>
var trackOutboundLink=function(t,e){var n,o="object"==typeof t?t.href:t,a=document.createElement("a");function c(){document.location=o}e=e||o,a.href=e,/\bprourls\.com?$/i.test(a.host)?e=(n=/[\?&]url=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e:/\blinksynergy\.com$/i.test(a.host)&&(e=(n=/[\?&]murl=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e),"function"==typeof gtag?gtag("event","click",{event_category:"outbound",event_label:e,transport_type:"beacon",event_callback:c}):ga("send","event","outbound","click",e,{transport:"beacon",hitCallback:c})};window.addEventListener("load",function(){var t,e,n=document.getElementsByTagName("a")||[];function o(t){t.preventDefault();var e=this.getAttribute("data-outbound-link");trackOutboundLink(this.href,"1"===e?"":e)}for(t=0;t<n.length;t++)(e=n[t]).href&&!/^#/.test(e.href)&&location.host!==e.host&&""!==e.getAttribute("data-outbound-link")&&e.addEventListener("click",o,!1)},!1);
</script>
请注意,该脚本是Google Analytics提供的脚本的修改/自定义版本:
https://support.google.com/analytics/answer/1136920?hl=en - 适用于旧版analytics.js
(或ga.js
)。
https://support.google.com/analytics/answer/7478520?hl=en - 适用于新的gtag.js
库。
第2步:将data-outbound-link="1"
添加到出站链接HTML。
对于您要跟踪的每个出站链接,请将其添加到链接的HTML:
data-outbound-link="1"
(如果您之前已将onclick="trackOutboundLink('http://www.example.com'); return false;"
添加到链接的HTML中,请将其替换为上述代码。)
所以代替:<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
..使用此:<a href="http://www.example.com" data-outbound-link="1">Check out example.com</a>
完整的脚本
以下是自定义 trackOutboundLink
脚本的完整,未经过说明的版本:(仅供参考)
<script>
var trackOutboundLink = function(link, url2track) {
var url2visit = ('object' === typeof link) ? link.href : link,
a = document.createElement('a'),
b;
url2track = url2track || url2visit;
a.href = url2track;
// Dynamic redirect URLs via Prourls.com; this was for Amazon URLs.
if (/\bprourls\.com?$/i.test(a.host)) {
b = (/[\?&]url=([^&#]+)/.test(a.search) && RegExp.$1) || '';
url2track = b ? decodeURIComponent(b) : url2track;
// Dynamic redirect URLs via Link Synergy; this was for Footlocker URLs.
} else if (/\blinksynergy\.com$/i.test(a.host)) {
b = (/[\?&]murl=([^&#]+)/.test(a.search) && RegExp.$1) || '';
url2track = b ? decodeURIComponent(b) : url2track;
}
// All other URLs are not parsed and expected to be valid outbound URLs.
function onTracked() {
//alert('Tracked: ' + url2track);
document.location = url2visit;
}
if ('function' === typeof gtag) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': url2track,
'transport_type': 'beacon',
'event_callback': onTracked
});
} else {
ga('send', 'event', 'outbound', 'click', url2track, {
'transport': 'beacon',
'hitCallback': onTracked
});
}
};
window.addEventListener('load', function() {
var links = document.getElementsByTagName('a') || [],
i, a;
function _go(e) {
e.preventDefault();
// Track a custom URL address, if specified.
var b = this.getAttribute('data-outbound-link');
trackOutboundLink(this.href, '1' === b ? '' : b);
}
for (i = 0; i < links.length; i++) {
a = links[i];
if (!a.href || /^#/.test(a.href) || location.host === a.host) {
continue;
}
if ('' !== a.getAttribute('data-outbound-link')) {
a.addEventListener('click', _go, false);
}
}
}, false);
</script>
答案 1 :(得分:2)
试试这个:
首先,删除所有链接上的onclick
处理程序。
第二次在<head>
块
var trackOutboundLink = function(url) {
var newUrl = url;
var re = /prourls\.co.*\?.*url=(.*)/
var matches = url.match(re);
if (matches && matches.length > 0) {
newUrl = matches[1]
}
// console.log(newUrl);
ga('send', 'event', 'outbound', 'click', newUrl, {
'transport': 'beacon',
'hitCallback': function(){document.location = url;}
});
};
var btns = document.querySelectorAll('a.btn');
for (var i = 0, l = btns.length; i < l; i++) {
btns[i].addEventListener('click', function(e) {
// console.log(e.target.href);
trackOutboundLink(e.target.href);
});
}
现在修改trackOutboundLink()
:
prourls.com?url=XXXX
url
参数