/************************************
This javascript tracks clicks on individual links as Events in Google Analytics.
The link the user clicks on will be identified by the URL, Anchor Text & ID (link number).
You have to use the Google Analytics Asynchronous Tracking for this script to work:
http://code.google.com/intl/nb/apis/analytics/docs/tracking/asyncTracking.html

The script must be placed just after your standard Google Analytics script (between <head> and </head>).
The script loads as soon as the DOM has loaded using addDOMLoadEvent:
<script src="/path/to/js/adddomloadevent.min.js" type="text/javascript"></script>
<script src="/path/to/js/ga-track-link-clicks.js" type="text/javascript"></script>
</head>

The sample script is provided AS IS without warranty of any kind. 
The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. 
In no event shall I be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation.

See http://www.savio.no/blogg/a/102/ for more information.

Smacked together by Eivind Savio January 2011
************************************/
addDOMLoadEvent(function() {
/************************************
SETTINGS
************************************/
var pageName = "title";			// Name of the page you want to track. title/url/custom
var trackAnchorText = true; 	// Track link Anchor Text? true/false
var trackLinkID = true; 		// Track LinkID? true/false
var trackExternalLinks = true; // Track External/outgoing links? true/false
var trimPageName = true;		// Should pageName be trimmed for special characters? true/false

/************************************
SCRIPT
************************************/
// Set a timer for when the page starts to load
var beforeLoad = (new Date()).getTime();
// Now do all the Link Stuff
if (document.getElementsByTagName) {
    var hrefs=document.getElementsByTagName('a');
    for(var l=0; l<hrefs.length; l++) {

if (hrefs[l].protocol == "http:" || hrefs[l].protocol == "https:") {

hrefs[l].lnum=l;
hrefs[l].onmousedown=function() {

var clickedURL = document.links[this.lnum].href; // Which link (URL) were clicked
/************************************
SCRIPT - Anchor Text Handling
************************************/
if (trackAnchorText == true) {
var linkAnchorText = document.getElementsByTagName("a")[this.lnum].innerHTML;
	if (linkAnchorText == "") {
		linkAnchorText = "|Anchor Text Missing";
		} else {
			// Check if the link is using a image as "anchor" using RegEx
			var linkRegEx = new RegExp(/<[\w*img][^>]+[\w*src\s*]=\s*['\"]([^'\"]+)['\"][^>]*>/); 
			var match = linkRegEx.exec(linkAnchorText); 
				if (match == null) { // Link is a text link.
				linkAnchorText = "|" + linkAnchorText.replace(/(<([^>]+)>)/ig,""); // Strip away HTML tags in anchor text
					} else { // Link is  a image. We use Image URL as "Anchor Text".
				 linkAnchorText = "|IMG-" + match[1];
				}
		}
	} else {
	linkAnchorText = ""; // Track Anchor Text is set to false, do not track Anchor text
}
/************************************
SCRIPT - Link ID Handling
************************************/	
if (trackLinkID == true) {
	LinkID = "|"+ (this.lnum+1);
	} else {
	LinkID = ""; // Track Link ID is set to false, do not track Link ID
}
/************************************
SCRIPT - Page Name Handling
************************************/
var pageNameResult = null;
switch (pageName) {
case "title": pageNameResult = document.title; break; 		// Use Title Tag to identify page
case "": pageNameResult = document.location.href; break;	// Use URL to identify page
default: pageNameResult = pageName;							// Use Custom Text to identify page
}
// Trim pageName
if (trimPageName == true) {
pageNameResult = pageNameResult.replace(/[^\w\s\.\-\(\)\?\|]/ig,"");
} else {
	pageNameResult = pageNameResult;
}
// Calculate seconds from page started to load to the visitor clicked on a link
var timeToClick = (new Date()).getTime();
var seconds = Math.round((timeToClick-beforeLoad)/1000);
/************************************
Send data to Google Analytics
************************************/
if (trackExternalLinks != true) { 
	if (this.hostname == location.host) { // Only track Internal Links
_gaq.push(['_trackEvent','Link Clicks',pageNameResult,clickedURL+linkAnchorText+LinkID,seconds]);
				} 
				}
				else { // Track both Internal & External Links
_gaq.push(['_trackEvent','Link Clicks',pageNameResult,clickedURL+linkAnchorText+LinkID,seconds]);
				}  
        	}
    	}
	}
}
}); // Closing addDOMLoadEvent function()
