///////
// gaAddons.js
// Stéphane Hamel - http://immeria.net
// You are free to modify and redistribute this script as long as you keep the reference to the authors
// If you have great ideas, please let me know so I can integrate them for futur releases!
//
// Changelog:
//  v2.3 - Apr. 2011 - Updated to use asyncronous _gaq tracker, fixed download link resolution for IE6.
//  v2.2 - Feb. 2009 - Adapted by TeamDDM for our CMS (change fileTypes regex; check domain of outbound links; parse url vars, set label for downloads; create exceptions for links that contain js functions)
//  v2.1 - Jan. 22nd 2009 - Set bUseEventForOutbout and bUserEventForDownload to toggle use of Events or Page Views
// 	v2.0 - Jan. 2009 - Use Google Analytics Events to track downloads and external links
//  v1.0 - Inspired by the work of Justin Cutroni - Google Analytics Short Cut - http://gashortcut.com/
///////

// fileTypes is the regular expression that matches hrefs to downloadable files
fileTypes = new RegExp(/(download\.php|doAction\(.*download)/i); //RegExp(/\.(docx*|xlsx*|pptx*|exe|zip|pdf|xpi)$/i);
var excludes = new RegExp(/showHide/i); //exceptions, links to exclude
var bUseEventForOutbound = true; // Set to false to use trackPageView for outbount links
var bUseEventForDownload = true; // Set to false to use trackPageView for downloads

///////
/// No need to change anything below this line
// Initialize external link and download handlers
function link_tracker_init(){
	var downloads = 0;
	var exlinks = 0;
	
	if (document.getElementsByTagName) {
		var hrefs = document.getElementsByTagName('a');
		for (var hrefCount = 0; hrefCount < hrefs.length; hrefCount++) {
			var hrefTemp = hrefs[hrefCount].href + "";
			
			if (fileTypes.test(hrefTemp) != null && fileTypes.test(hrefTemp) != false){
				startListening(hrefs[hrefCount], "click", trackDownloads);
				downloads++;
			}else if (hrefs[hrefCount].hostname != location.host){ //not an internal link
				startListening(hrefs[hrefCount], "click", trackExternalLinks);
				exlinks++;
			}
		}
	}
}

function startListening(obj, evnt, func){
    if (obj.addEventListener) 
        obj.addEventListener(evnt, func, false);
    else 
        if (obj.attachEvent) 
            obj.attachEvent("on" + evnt, func);
}

function trackDownloads(evnt){
	if (typeof(_gaq) != "object") 
        return;
	
    var elmnt = evnt.srcElement;

	if (elmnt) {
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
	}
    
	labelText = (elmnt) ? elmnt.search : this.search;
	regex = new RegExp(/file=[0-9]+_([^=]+)&/i); //grab filename if available, strip timestamp
	labelTextArray = regex.exec(labelText);
	if (labelTextArray)
		labelText = labelTextArray[1];
	else
		labelText = (elmnt) ? elmnt.name : this.name;
	labelText = location.pathname+'::'+labelText;
	//alert(evnt);
	bUseEventForDownload ? _gaq.push(['_trackEvent', "download", "click", labelText]) :  _gaq.push(['_trackPageView', "/download/" + labelText]);
}

function trackExternalLinks(evnt){
	if (typeof(_gaq) != "object") 
        return;
    var elmnt = evnt.srcElement;

	if (elmnt) {
        while (elmnt.tagName != "A") 
            elmnt = elmnt.parentNode;
	}
	
	labelText = (elmnt) ? elmnt.href : this.href;
	labelHostname = (elmnt) ? elmnt.hostname : this.hostname;
	
	if (labelHostname && !labelHostname.match(/^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/)) {
		labelHostname = ''; // if it doesn't match a domain name, then it's a javascript (IE6 fix)
	}

//grab url from javascript links
	regex = new RegExp(/(show_extlink_popup|confirm_extlink_popup)\('([^']*)'/i);
	labelTextArray = regex.exec(labelText);
	if (labelTextArray){
		labelText = labelTextArray[2] +'::'+ labelTextArray[1];
		labelHostname = 'external';
	}
	//if a real external link
	if (!excludes.test(labelText) && labelHostname && labelHostname != location.hostname){
		bUseEventForOutbound ? _gaq.push(['_trackEvent', "outbound", "click", labelText]) : _gaq.push(['_trackPageView', "/outbound/" + labelText]);
	}
}
/// EOF ///
