/****************************************************
     Author: Brian J Clifton
     Url: http://www.advanced-web-metrics.com/scripts
     This script is free to use as long as this info is left in
     
     Combined script for tracking external links, file downloads and mailto links
     
     All scripts presented have been tested and validated by the author and are believed to be correct
     as of the date of publication or posting. The Google Analytics software on which they depend is 
     subject to change, however; and therefore no warranty is expressed or implied that they will
     work as described in the future. Always check the most current Google Analytics documentation.

     Thanks to Nick Mikailovski (Google) for intitial discussions & Holger Tempel from webalytics.de
     for pointing out the original flaw of doing this in IE.

****************************************************/
// Only links written to the page (already in the DOM) will be tagged
// This version is for ga.js (last updated Jan 15th 2009)


function addAnalyticsLinkEvents() {
	var as = document.getElementsByTagName("a");
	var extTrack = ["odi.org.uk","websitetest"];
	// List of local sites that should not be treated as an outbound link. Include at least your own domain here
	
	var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js","mp3","wma","ppt"];
	//List of file extensions on your site. Add/edit as you require
	
	/*If you edit no further below this line, Top Content will report as follows:
		/ext/url-of-external-site
		/downloads/filename
		/mailto/email-address-clicked
	*/
	
			
	for(var i=0; i<as.length; i++) {
		var flag = 0;
		var tmp = as[i].getAttribute("onclick");

		// IE6-IE7 fix (null values error) with thanks to Julien Bissonnette for this
		if (tmp != null) {
		  tmp = String(tmp);
		  if (tmp.indexOf('urchinTracker') > -1 || tmp.indexOf('_trackPageview') > -1) continue;
    		}

		// Tracking outbound links off site - not the GATC
		for (var j=0; j<extTrack.length; j++) {					
			if (as[i].href.indexOf(extTrack[j]) == -1 && as[i].href.indexOf('google-analytics.com') == -1 ) {
				flag++;
			}
		}
		if (flag == extTrack.length && as[i].href.indexOf("mailto:") == -1){
			as[i].onclick = function(){ var hrefToLoad = this.href; var splitResult = this.href.split("//");_gaq.push(['_trackEvent','link','external', +splitResult[1]]) + ";" +((tmp != null) ? tmp+";" : "");setTimeout(function() { location.href=hrefToLoad }, 200);return false;
	};
				//alert(as[i] +"  ext/" +splitResult[1])
		}			

		// Tracking electronic documents - doc, xls, pdf, exe, zip
		for (var j=0; j<extDoc.length; j++) {
			if (as[i].href.indexOf(extTrack[0]) != -1 && as[i].href.indexOf(extDoc[j]) != -1) {
				as[i].onclick = function(){var hrefToLoad = this.href;  var splitResult = this.href.split(extTrack[0]);_gaq.push(['_trackEvent','file',extDoc[j], +splitResult[1]])+ ";"
+((tmp != null) ? tmp+";" : "");setTimeout(function() { location.href=hrefToLoad }, 200);return false;}
				//alert(as[i] +"  downloads" +splitResult[1])
				break;
			}
		}

		// added to track mailto links 23-Oct-2007
		// updated 31-Oct-2008 to remove break command - thanks to Victor Geerdink for spotting this
		if (as[i].href.indexOf("mailto:") != -1) {
			as[i].onclick = function(){ var splitResult = this.href.split(":");_gaq.push(['_trackEvent','link', 'email', +splitResult[1]])+ ";"+((tmp != null) ? tmp+";" : "");}
			//alert(as[i] +"  mailto/" +splitResult[1])
		}

	}
}

function RemoveAnalyticsLongBounces() {
	// Avinash Kaushik and other metrics evangelists believe that a true measure of bounce rate is not the relationship with navigation, but rather time. While the exact number is different depending on the source, most believe that after a visitor is on the site longer than six to ten seconds they are no longer a bounce even if they never click to another page. This function adds an event for people who are on the site for more than twenty seconds (being conservative), so they don't show up as a bounce.
	
setTimeout("_gaq.push(['_trackEvent','NoBounce', 'NoBounce', 'Over 20 seconds'])",20000);
}
function _messager_trackEvent(type, action, id) {
	_gaq.push(['_trackEvent',type, action, id]);
}


// ACTUAL TRACKING CODE
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-1177471-1']);
  _gaq.push(['_setDomainName', '.odi.org.uk']);
	// check for custom variables
	if(typeof SetCustomVariables == 'function') {
		SetCustomVariables();
	} 
  _gaq.push(['_trackPageview']);
  _gaq.push(['_trackPageLoadTime']);
  

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

// Begin: 4q.iperceptions.com GA Tracker
//try {
//    var pageTracker4Q = _gat._getTracker("UA-1678422-1");
//    pageTracker4Q._setAllowLinker(true);
//    pageTracker4Q._setAllowHash(false);
//    pageTracker4Q._trackPageview();
//} catch (err) {
//}
// End: 4q.iperceptions.com com GA Tracker 

