/*
Name: Google Analytics Keyword Sleuth 2.0.5 - ga.js Version
Author: Michael Harrison
Created: 01/18/2008
Description: If a visitor arrives from a search engine, grab their exact search query and store it in the user defined variable - also initial search query is never overwritten.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-X");
pageTracker._trackPageview();
</script>
<script src="http://www.[yoursitehere].com/path/to/exact-keyword-tracking.js" type="text/javascript"></script>

~~~~~~
~~~~~~
Last modified by Shawn Purtell/Michael Harrison on 8/05/2009
*/

function readCookie(name) {		// function to read cookie
	var nameRegex = new RegExp("(?:;\\s|^)" + name + "=([^;]+)"), nameValue;	// match name of cookie and store value in $1
	nameValue = nameRegex.exec(document.cookie);				
	return nameValue && nameValue[1];
}

function noPercent(x) {		//function to clean up keyword information
	x = unescape(x);
	return x.replace(/\+/g, " ").replace(/(^ +| +$)/g, "");
}

function sleuth(pageTracker) {
	var cpcValue = "cpc - ", userdef, cpcregexp, ref, re, searchq;
	if (location.search.indexOf("gclid") !== -1 || location.search.indexOf("cpc") !== -1) {	
		userdef = readCookie("__utmv");
		cpcregexp = new RegExp(/^cpc - /);
		if (!userdef || !cpcregexp.exec(userdef)) {
			ref = document.referrer;
			re = new RegExp(/(?:\?|&)(?:q|p|query|encquery|qt|terms|rdata|qs|wd|text|szukaj|k|searchExpr|search_for|string|search_query|searchfor)=([^&]+)/);
			searchq = re.exec(ref);
			if (searchq) { 
				cpcValue += noPercent(searchq[1]);
			} else {
				cpcValue += "Paid Referral: "; 
				cpcValue += ref;
			}
			pageTracker._setVar(cpcValue);
		}
	}
}

sleuth(pageTracker);