I've been using a script to track outbound links for a couple of months now. The script WORKS, but in the report generated by Google Analytics many URLs are having a trailing ":80" (default port number) at their end. Read on for more details.
It's maybe important to mention that the website tracking these outbound links has a tremendous amount of outbound traffic (multiply your fantasy by ∞).
It tracks ALL outbound links and tag them as "Outbound Links" in Google Analytics.
The script is heavily commented and has a few instances of console.log() to help debugging (these are kept commented out).
"Outbound Links" show on GA alright, under:
Content > Events > Top Events > "Outbound Links" [click on it] > [report showing all urls clicked]
Under the "Outbound Links" report, where I get all the links that were clicked, I get ":80" at the end of at least 2/3 of all links reported (probably more). GA treats http://example.com and http://example.com:80 as different links, separating them in the report. That's of course not desired.
Worth mentioning:
Links that end with ":80" always have more hits than their equivalent without ":80", anything from 40% to 60% more hits.
// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
$("a").on('click',function(e){
var url = $(this).attr("href");
// Console logs shows the domain name of the link being clicked and the current window
// console.log('e.currentTarget.host: ' + e.currentTarget.host);
// console.log('window.location.host: ' + window.location.host);
// If the domains names are different, it assumes it is an external link
// Be careful with this if you use subdomains
if (e.currentTarget.host != window.location.host) {
// console.log('external link click');
// Outbound link! Fires the Google tracker code.
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
// Checks to see if the ctrl or command key is held down
// which could indicate the link is being opened in a new tab
if (e.metaKey || e.ctrlKey) {
// console.log('ctrl or meta key pressed');
var newtab = true;
}
// If it is not a new tab, we need to delay the loading
// of the new link for a just a second in order to give the
// Google track event time to fully fire
if (!newtab) {
// console.log('default prevented');
e.preventDefault();
// console.log('loading link after brief timeout');
setTimeout('document.location = "' + url + '"', 100);
}
}
/*
else {
console.log('internal link click');
}
*/
});
});
You can track which outbound links are clicked by your users by adding a short, simple code in your Google Analytics Tracking code.
Fresheyeball answer is the correct one, but because many people have been asking for a complete answer, I'm going to post the final script with Fresheyeball's contribution.
// Outbound Link Tracking with Google Analytics
// Wallace Sidhrée - http://dreamyguy.com/
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
$("a").on('click',function(e){
var url = $(this).attr("href");
if (e.currentTarget.host != window.location.host) {
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80',''), url, 0);
if (e.metaKey || e.ctrlKey || this.target == "_blank") {
var newtab = true;
}
if (!newtab) {
e.preventDefault();
setTimeout('document.location = "' + url + '"', 100);
}
}
});
});
// Outbound Link Tracking with Google Analytics
// Wallace Sidhrée - http://dreamyguy.com/
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
$("a").on('click',function(e){
var url = $(this).attr("href");
// Console logs shows the domain name of the link being clicked and the current window
// console.log('e.currentTarget.host: ' + e.currentTarget.host);
// console.log('window.location.host: ' + window.location.host);
// If the domains names are different, it assumes it is an external link
// Be careful with this if you use subdomains
if (e.currentTarget.host != window.location.host) {
// console.log('external link click');
// Outbound link! Fires the Google tracker code.
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80',''), url, 0);
// Checks to see if the ctrl or command key is held down
// which could indicate the link is being opened in a new tab
// Also checks if target="_blank" on the link tag which indicates it should always be opened in a new tab
if (e.metaKey || e.ctrlKey || this.target == "_blank")) {
// console.log('ctrl or meta key pressed');
var newtab = true;
}
// If it is not a new tab, we need to delay the loading
// of the new link for a just a second in order to give the
// Google track event time to fully fire
if (!newtab) {
// console.log('default prevented');
e.preventDefault();
// console.log('loading link after brief timeout');
setTimeout('document.location = "' + url + '"', 100);
}
}
/*
else {
console.log('internal link click');
}
*/
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With