Tracking all Link Clicks via jQuery for Adobe Analytics or Google Analytics

Sometimes customers ask me if they could track each and every link on every page to see where users are clicking from a most popular/least popular perspective. This blog posting will provide the necessary logic for you to track these interaction points via jQuery within either Adobe Analytics or Google Analytics.

What you will need:
– Ability to add custom logic to your tag management system
– The jQuery library file loaded in prior to the call for link tracking

Below is the code needed to track all links. Depending on your TMS, you can add this in using the below code for Adobe Analytics or Google Analytics:

Adobe Analytics

$(document).ready(function() {

$("a").each(function() {
var href = $(this).attr("href");
var target = $(this).attr("target");
var text = $(this).text();
$(this).click(function(event) { // when someone clicks these links
event.preventDefault(); // don't open the link yet

var s=s_gi('rsid');
s.linkTrackVars='eVar11,prop11';
s.prop11=text+":"+target;
s.eVar11 = "D=c11";
s.tl(true,'o',text+":"+target+":");

setTimeout(function() { // now wait 300 milliseconds...
window.open(href,(!target?"_self":target)); // ...and open the link as usual
},300);
});
});

});

That’s it! With that in place, you can automatically track each and every link thats in place and have it sent into Adobe Analytics. If you wanted to do this for Google Analytics, you would need to use the following logic:

Google Analytics

$(document).ready(function() {

	$("a").each(function() {
		var href = $(this).attr("href");
		var target = $(this).attr("target");
		var text = $(this).text();
		$(this).click(function(event) { // when someone clicks these links
			event.preventDefault(); // don't open the link yet
			_gaq.push(["_trackEvent", "Links", "Clicked", text, , false]); // create a custom event
			setTimeout(function() { // now wait 300 milliseconds...
				window.open(href,(!target?"_self":target)); // ...and open the link as usual
			},300);
		});
	});

});

The above Google Analytics logic takes the link text and places that into a track event call for Google to track accordingly.

Please note that while most do not need to track each and every link on a given page, some do have the need to and this will help in that case.

Posted in Adobe Analytics, Google Analytics, Web Analytics