Here’s a snippet of code that a colleague and I wrote for a SiteCatalyst user who wanted to count all exit links as success events. This is helpful for media sites and others where linking to external partners is a major aspect of online success.

You can put it anywhere in your code after the instantiation of the ‘s’ object. It adds an event listener to every link on the page, then makes an exit link tracking call automatically if the link doesn’t match any of the values in your s.linkInternalFilters variable. It will not interfere with any other onclick event handlers on your site.

This code has two advantages over automatic exit link tracking:

  1. Allows you to set any variable/event on exit link clicks using the familiar s.tl() function. (For more information on how to use this function, see my previous post on the topic.)
  2. Allows you to pass the exit href’s domain (along with the subdomain, if you use the example below as-is) as the link name, which helps group link URLs for ease of reporting/analysis.

If you’re already using the exitLinkHandler plug-in, then this one isn’t necessarily an upgrade. But if you’re not already using a plug-in to manage your automatic exit link measurement, and would like to enhance the data passed along with exit links. . . well, then, a belated happy holidays to you. Enjoy. If you aren’t intimately familiar with link tracking, you may want to read about it here before you get started with the code below.

Feel free to modify/fix/rewrite this code based on your needs and preferences. (For example, this code could easily be tweaked to work with file downloads instead of exit links. Or, you may not want to capture the subdomain of the exit link URL.) I’m providing it merely as a suggestion to show how you might write some code to count custom success metrics or variables when a user clicks an exit link.

Some important instructions:

  • Put the entire block of code above the config variables in your s_code.js file. Do not put it inside of s_doPlugins.
  • Make sure to set s.trackExternalLinks=false when using this code. This is required for the functions below to take over exit link tracking.
  • Set variables and events as necessary. Make sure to add to s.linkTrackVars and s.linkTrackEvents appropriately. For example, to count an instance of event1 on every exit link click, set s.linkTrackVars=”events”, s.linkTrackEvents=”event1″, and s.events=”event1″ in the section labeled, “SET YOUR EXIT LINK VARIABLES AND EVENTS HERE.”
  • This code sets the exit link subdomain+domain (e.g. espn.go.com) as the link name (which you see in the Exit Links report). To use the full link URL instead, simply omit the third argument in s.tl().
  • The code uses the s.linkInternalFilters list to determine whether a link is internal/external. Make sure to set that crucial variable correctly.
  • As with any implementation change, make sure to test this code in a development environment before deploying to production!

Here’s the code:

/* Exit Link Manager v0.5 */
function getLinks(){for(var i=0;i<document.links.length;i++){lnk=document.links[i];
if(lnk.addEventListener){lnk.addEventListener('click',sendExitConversion,false)
}else if(lnk.attachEvent){lnk.attachEvent('onclick',sendExitConversion)}}}
function setBodyOnload(){if(window.addEventListener){
window.addEventListener('load',getLinks,false)
}else if(window.attachEvent){window.attachEvent('onload',getLinks)}}
setBodyOnload();
function sendExitConversion(event){var obj;event=event?event:window.event;
var s=s_gi(s_account);if(event.target){obj=event.target
}else if(event.srcElement){obj=event.srcElement}if(obj.href){
var exitLinks=s.linkInternalFilters;var exitArray=exitLinks.split(',');
if(s.trackExternalLinks==false){isExitLink=true;for(i=0;i<exitArray.length;i++)
{if(obj.href.toLowerCase().indexOf(exitArray[i].toLowerCase())>-1)
{isExitLink=false}}
exitURLStart=obj.href.substring(obj.href.indexOf('//')+2,obj.href.length);
if(exitURLStart.indexOf('/')>-1){exitURLEnd=exitURLStart.indexOf('/')
}else{exitURLEnd=exitURLStart.length}
var exitDomain=exitURLStart.substring(0,exitURLEnd);if(isExitLink){

/* SET YOUR EXIT LINK VARIABLES AND EVENTS HERE */
s.linkTrackVars="";
s.linkTrackEvents="";
s.events="";
s.tl(this,'e',exitDomain);
}}}}

In closing, note that another good way to enhance your exit links automatically is using VISTA or DB VISTA (which are even more automatic, because you don’t have to implement a thing!). Omniture Engineering Services can do pretty much anything to your exit link data after it has been collected so that no additional development is required in your SiteCatalyst implementation.

(Special thanks to Andreas Dierl [@ad0815] for giving some constructive feedback on the code above!)

As always, please leave a comment with any questions, thoughts, or suggestions that you may have! I’m also available Twitter or LinkedIn, or by e-mailing omniture care [at] adobe dot com.

3 comments
Adam
Adam

Is this the latest code for exitLinkHandler()?

Jenn Kunz
Jenn Kunz

Thanks, this pointed me in the direction I needed!

Will Crowther
Will Crowther

Thanks for the code, Ben! I will see if we can incorporate it into what we are doing.