Tracking Exit Links in Google Analytics without GTM

Exit link tracking, in Google Analytics, provides the ability to see and understand where you users are going to if they leave the site. This can be helpful in deciding on a multitude of UX items moving forward. This posting will describe what you need to do and how you can go about setting it up.

For most who read this, they say to themselves, well why don’t you just use the auto tracking that GTM provides and be done with it? That is true but what if you arent using GTM and are using Signal as your TMS? Then you need custom logic to handle this. Below is code that you can plug and play to help you track this. One caveat is you will need to load the jQuery library before you load the below code:


//Google Analytics - Custom Exit Link Tracking
(function($) {

"use strict";

// current page host
var pageHost = window.location.host;

// click event on body
$("body").on("click", function(e) {

// abandon if link already aborted or analytics is not available
if (e.isDefaultPrevented() || typeof ga !== "function") return;

// abandon if no active link or link within domain
var link = $(e.target).closest("a");
if (link.length != 1 || pageHost == link[0].host) return;

// cancel event and record outbound link
e.preventDefault();
var href = link[0].href;
ga('secondTracker.send', {
'hitType': 'event',
'eventCategory': 'Exit Link',
'eventAction': 'clicked',
'eventLabel': href,
'hitCallback': loadPage
});

// redirect after 1/2 second if recording takes too long
setTimeout(loadPage, 500);

// send traffic to exit link URL
function loadPage() {
document.location = href;
}

});

})(jQuery); // pass another library here if required

That’s it. With this in place (whether in Signal, another TMS or on the page (hard coded)), you will be able to succesfully track exit links and have it recorded in GA as an event. You can also tweak this to pass it to a custom dimension if you want to potentially leverage data import to reclassify the URLs captured in the event label to more friendlier items.

Posted in Google Analytics, Web Analytics