/****************************************************
     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.

****************************

Modified by Ed Cramer, summer 2010
captthud@captthud.net
http://sites.google.com/site/captthudcodescraps/automatic-ga-link-tracker

I want to track PDF downloads with this, but I don't want to use the actual path of the PDF file
for my statistics. I'd like the script to post information about the parent page that the user
found the PDF on.

The original code used the "traditional" tracking code snippet, and I'd prefer the "asyncrhonous"
code because it's supposed to be faster. I've changed a lot of the formatting and I think
I've removed the bug fix for IE6/7.

fyi... The code commented out in Ed's version is to run any already existing onclick event handlers.

So, if a trackPageView is already defined or some other task needs to be performed on the "onclick" 
event, it prevents the the original event from firing. Only the overwritten pageTracking will be fired. 

****************************************************/

function addLinkerEvents() {
	var as = document.getElementsByTagName("a");
	var extTrack = ["jameshardiecommercial.com"]; // List of local sites that should not be treated as an outbound link. Include at least your own domain here
	var extDoc = [".exe",".dmg",".pdf",".swf",".mp4"]; //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");
		if (tmp != null) {
			continue;
		}
		if (as[i].href.indexOf('javascript') != -1 ) {
			// alert("got javascript link");
			continue;
		}
		/*
		// 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 splitResult = this.href.split("//"); //removed... Looks like it's part of the IE fix from before (Ed)
				_gaq.push(['_trackPageview', '/external/' +splitResult[1]]); //+ ";" +((tmp != null) ? tmp+";" : "");
			};
			//alert(as[i] +"  ext/" +splitResult[1])
		}
		// Tracking electronic documents - dmg, pdf, exe
		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 splitResult = this.href.split("jameshardiecommercial.com");
					// alert("0 " + splitResult[0] + " 1 " + splitResult[1])
					var parentURL = this;
					parentURL = parentURL.ownerDocument;
					parentURL = parentURL.URL; // I don't know why I can't put all of these on one line.
					// this.ownerDocument.URL would have been nice, but it doesn't work.
					// IE8 handles local variables differently, "this" isn't automatically available.
					parentURL = parentURL.split(extTrack[0]);
					var realURLcomponents = this.href.split("/"); //removed- Looks like it's part of the IE fix from before (Ed)
					/** ORIGINAL, DMB: _gaq.push(['_trackPageview', parentURL[1] + '/' + realURLcomponents[realURLcomponents.length -1]]); //+ ";" +((tmp != null) ? tmp+";" : ""); */
					// DMB 2/3/2011 _gaq.push(['_trackPageview', this.href ]);
					_gaq.push(['_trackPageview', splitResult[1] ]);
				}
			//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(":"); //removed... Looks like it's part of the IE fix from before (Ed)
				_gaq.push(['_trackPageview', '/mailto/' +splitResult[1]]); //+ ";"+((tmp != null) ? tmp+";" : "");
			}
		//alert(as[i] +"  mailto/" +splitResult[1])
		}
	} 
}

