Here’s a snip­pet of code that a col­league and I wrote for a Site­Cat­a­lyst user who wanted to count all exit links as suc­cess events. This is help­ful for media sites and oth­ers where link­ing to exter­nal part­ners is a major aspect of online success.

You can put it any­where in your code after the instan­ti­a­tion of the ‘s’ object. It adds an event lis­tener to every link on the page, then makes an exit link track­ing call auto­mat­i­cally if the link doesn’t match any of the val­ues in your s.linkInternalFilters vari­able. It will not inter­fere with any other onclick event han­dlers on your site.

This code has two advan­tages over auto­matic exit link tracking:

  1. Allows you to set any variable/event on exit link clicks using the famil­iar s.tl() func­tion. (For more infor­ma­tion on how to use this func­tion, see my pre­vi­ous post on the topic.)
  2. Allows you to pass the exit href’s domain (along with the sub­do­main, if you use the exam­ple below as-is) as the link name, which helps group link URLs for ease of reporting/analysis.

If you’re already using the exitLinkHan­dler plug-in, then this one isn’t nec­es­sar­ily an upgrade. But if you’re not already using a plug-in to man­age your auto­matic exit link mea­sure­ment, and would like to enhance the data passed along with exit links… well, then, a belated happy hol­i­days to you. Enjoy. If you aren’t inti­mately famil­iar with link track­ing, 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 pref­er­ences. (For exam­ple, this code could eas­ily be tweaked to work with file down­loads instead of exit links. Or, you may not want to cap­ture the sub­do­main of the exit link URL.) I’m pro­vid­ing it merely as a sug­ges­tion to show how you might write some code to count cus­tom suc­cess met­rics or vari­ables when a user clicks an exit link.

Some impor­tant instructions:

  • Put the entire block of code above the con­fig vari­ables 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 func­tions below to take over exit link tracking.
  • Set vari­ables and events as nec­es­sary. Make sure to add to s.linkTrackVars and s.linkTrackEvents appro­pri­ately. For exam­ple, to count an instance of event1 on every exit link click, set s.linkTrackVars=“events”, s.linkTrackEvents=“event1”, and s.events=“event1” in the sec­tion 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, sim­ply omit the third argu­ment in s.tl().
  • The code uses the s.linkInternalFilters list to deter­mine whether a link is internal/external. Make sure to set that cru­cial vari­able correctly.
  • As with any imple­men­ta­tion change, make sure to test this code in a devel­op­ment envi­ron­ment before deploy­ing 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 clos­ing, note that another good way to enhance your exit links auto­mat­i­cally is using VISTA or DB VISTA (which are even more auto­matic, because you don’t have to imple­ment a thing!). Omni­ture Engi­neer­ing Ser­vices can do pretty much any­thing to your exit link data after it has been col­lected so that no addi­tional devel­op­ment is required in your Site­Cat­a­lyst implementation.

(Spe­cial thanks to Andreas Dierl [@ad0815] for giv­ing some con­struc­tive feed­back on the code above!)

As always, please leave a com­ment with any ques­tions, thoughts, or sug­ges­tions that you may have! I’m also avail­able Twit­ter or LinkedIn, or by e-mailing omni­ture 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.