Google 分析 - 带重定向的出站 link 跟踪
Google Analytics - Outbound link tracking with a redirect
我已经设置了 Google 分析来跟踪 outbound links. However, I have also installed a WordPress plugin called Amazon Link Localizer 修改传出 URL,使其最终形式是 prourls.com?url=www.amazon.com
而不是 www.amazon.com
。
是否可以修改 Analytics 脚本,以便仍然可以跟踪这些出站 link 点击?
试试这个:
首先,删除所有链接上的 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
参数的值
- 将该值传递给 GA 事件跟踪调用
第 1 步:将 trackOutboundLink
脚本添加到您的网站。
在页面 HTML 的 <head></head>
部分添加此代码:(完整的脚本位于此页面的底部,但使用此脚本有助于最小化页面大小)
<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.||"")?decodeURIComponent(n):e:/\blinksynergy\.com$/i.test(a.host)&&(e=(n=/[\?&]murl=([^&#]+)/.test(a.search)&&RegExp.||"")?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 提供的脚本的 modified/customized 版本:
https://support.google.com/analytics/answer/1136920?hl=en — for the old analytics.js
(或ga.js
)库。
https://support.google.com/analytics/answer/7478520?hl=en — for the new gtag.js
图书馆.
第 2 步:将 data-outbound-link="1"
添加到出站 links' HTML.
对于您要跟踪的每个出站 link,将其添加到 link 的 HTML:
data-outbound-link="1"
(如果您之前在 link 的 HTML 中添加了 onclick="trackOutboundLink('http://www.example.com'); return false;"
,请将其替换为上述代码。)
所以代替:<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>
完整脚本
下面是 custom 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.) || '';
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.) || '';
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>
我已经设置了 Google 分析来跟踪 outbound links. However, I have also installed a WordPress plugin called Amazon Link Localizer 修改传出 URL,使其最终形式是 prourls.com?url=www.amazon.com
而不是 www.amazon.com
。
是否可以修改 Analytics 脚本,以便仍然可以跟踪这些出站 link 点击?
试试这个:
首先,删除所有链接上的 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
参数的值 - 将该值传递给 GA 事件跟踪调用
第 1 步:将 trackOutboundLink
脚本添加到您的网站。
在页面 HTML 的 <head></head>
部分添加此代码:(完整的脚本位于此页面的底部,但使用此脚本有助于最小化页面大小)
<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.||"")?decodeURIComponent(n):e:/\blinksynergy\.com$/i.test(a.host)&&(e=(n=/[\?&]murl=([^&#]+)/.test(a.search)&&RegExp.||"")?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 提供的脚本的 modified/customized 版本:
https://support.google.com/analytics/answer/1136920?hl=en — for the old
analytics.js
(或ga.js
)库。https://support.google.com/analytics/answer/7478520?hl=en — for the new
gtag.js
图书馆.
第 2 步:将 data-outbound-link="1"
添加到出站 links' HTML.
对于您要跟踪的每个出站 link,将其添加到 link 的 HTML:
data-outbound-link="1"
(如果您之前在 link 的 HTML 中添加了 onclick="trackOutboundLink('http://www.example.com'); return false;"
,请将其替换为上述代码。)
所以代替:<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>
完整脚本
下面是 custom 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.) || '';
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.) || '';
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>