// Functions used for tracking web analytics.

// To track analytics, add the variable "webAnalyticsTracker" to one 
// of your app's header files, and set it to your chosen tracker function.

// This function will be called after the contentArea has been updated 
// via ajax, after a search is done, etc. 

// Note:  If your app needs to use multiple analytics trackers, you can
//        write a wrapper function that calls the functions in this file, and
//        register that function as your "webAnalyticsTracker" function (i.e.
//        as described above).  We've added one such function, "trackAnalytics"
//        as convenience for call all available analytics engines.

//
// Function to track analytics in our "standard" analytics engines.  This is
// currently Google Analytics and Unica.
//
// Note:  In the future we might drop Google Analytics from being called here
//        once Unica has been proven to provide everything Google has (Unica is
//        truly our corporate standard).
//
function trackAnalytics(url) {
	trackGoogleAnalytics(url);
	
	// Note:  Have intentionally placed Unica second since their docs
	//        imply they may incorporate a delay when tracking is called
	//        whereas (as far as I know) Google does not:
	trackUnicaAnalytics(url);								
}

// Get a query param value from the given string's parameters
// Note:  This is designed to work whether str is an actual url, or the YUI history hash
//        (which also has query-string like "parameters")
function getQParam(str, paramName, defaultValue) {
	var paramVal;
	if(str) {		
		var startPos = str.indexOf(paramName+"=");
		if(startPos > 0) {				
			var endPos = str.indexOf("&", startPos);
			if(endPos > 0) {			
				paramVal = str.substring(startPos+paramName.length+1, endPos);
			}
			else {
				paramVal = str.substring(startPos+paramName.length+1, str.length);				
			}
		}
	
		if(!paramVal && defaultValue) {
			paramVal = defaultValue;
		}
	}
	
	return paramVal;
}

//
// Function to track Unica analytics.
//
// This function makes use of the "NTPT_GLBLEXTRA" variable which is defined using
// the standard javascript suggested by Unica.  It calls the ntptEventTag()
// function to track the activity in Unica.
//
// Note:  You can optionally define a "getAnalyticsURL" function to 
//        create a qualified url (i.e. with your app-specific context), otherwise
//        the ajax url is used (which may not have your app-context on it).
//

function trackUnicaAnalytics(url) {
														
	// NTPT_GLBLEXTRA must be defined if Unica is enabled for the site:
	if((typeof(NTPT_GLBLEXTRA) != 'undefined') && NTPT_GLBLEXTRA) {
	
		// if we didn't get a url
		if(!(typeof(url) != 'undefined' && url)) {
			// Just pick up the location from the browser address line...		
			url = window.location.href;
		}

		// Call unica analytics:
		// Note:  this would typically be done at the end of a page,
		//        but with ajax we call this explicitly after we've
		//        updated the page with the ajax response and set the
		//        new title:
		var analyticsURL;	
		
		// The url submitted to analytics 
		if((typeof(getAnalyticsURL) != 'undefined') && getAnalyticsURL) {
			 // The app can customize what they want for the url,
			 // (e.g. they can/should add their app context to the front)				 				 
			 analyticsURL = getAnalyticsURL(url);
		} 
		else {
			// Or the default is the original ajax url unmodified:
			analyticsURL = url;
		}															

		if(analyticsURL.indexOf("http://") != 0) {	
			// Transfer the domain as currently displayed by the browser
			// into the returned url:
			var endDomain = window.location.href.indexOf("/", 7);						
			if(endDomain && endDomain > 0) {			
				var beginURL = window.location.href.substring(0, endDomain);		
				analyticsURL = beginURL + analyticsURL;				
			}		
		}	

		if(analyticsURL) {
			// It turns out NTPT_PGEXTRAs *override* ntptAddPairs - so reset NTPT_PGEXTRA:
			NTPT_PGEXTRA='';				
		
			// So this is our location:	
			ntptAddPair("lc", analyticsURL);
							
			// And we want to treat this *as if* it were a page view:
			ntptAddPair("pv", 1);
			
			// And we want to add the title of this page, since our urls are pretty dang ugly:
			ntptAddPair("ttl", document.title);

			// Likewise the title:	 			
			if((typeof(gippEmail) != 'undefined') && gippEmail) {			
				ntptAddPair("gippemail", gippEmail);				
			}	
			
			// And the isbn is very helpful for looking at results per book:	 			
			var isbn = getQParam(url, "isbn", undefined);
			if(isbn) {			
				ntptAddPair("isbn", isbn);				
			}				
			
			// Likewise the title:	 			
			if((typeof(booktitle) != 'undefined') && booktitle) {			
				ntptAddPair("booktitle", booktitle);				
			}	
			
			// And the author:	 			
			if((typeof(bookauthor) != 'undefined') && bookauthor) {			
				ntptAddPair("author", bookauthor);				
			}				
			
			// The "shortttitle" is meant to a be a single parameter that is 
			// user-friendly when it appears on analytics reports, e.g.
			// "<author>: <title> <edition> ed"	 			
			if((typeof(analyticstitle) != 'undefined') && analyticstitle) {			
				ntptAddPair("shorttitle", analyticstitle);				
			}				
					
			// Pass along the user if it's known, otherwise don't pass it at all 
			// (it throws off Unica reports if we do)
			// Note: "un" is the standard pair Unica defines for passing user id:			
			if((typeof(userID) != 'undefined') && userID) {
				ntptAddPair("un", userID);					
			} else if((typeof(unicaUser) != 'undefined') && unicaUser) {
				ntptAddPair("un", unicaUser);
			} 		
										
			// Pass along the subscription type if it's known, 
			// otherwise the default is LOT for "life of title"
			var unicaSubsType = 'LOT'; 
			if((typeof(subsType) != 'undefined') && subsType) {
				unicaSubsType = subsType;
			} 
			ntptAddPair("subsType", unicaSubsType);			
			
			// Pass along the campaign parameters if known
			if((typeof(elsca1) != 'undefined') && elsca1) {
				ntptAddPair("elsca1", elsca1);				
			} 
			if((typeof(elsca2) != 'undefined') && elsca2) {
				ntptAddPair("elsca2", elsca2);				
			}
			if((typeof(elsca3) != 'undefined') && elsca3) {
				ntptAddPair("elsca3", elsca3);				
			}
			if((typeof(elsca4) != 'undefined') && elsca4) {
				ntptAddPair("elsca4", elsca4);				
			}							
						
			// Track it!
			ntptEventTag();
		}		
	}														
}

//
// Function to track google analytics.
//
// This function makes use of the "_uacct" variable which is defined using
// the standard scriplet suggested by google analytics, but it calls the
// urchinTracker() method (unlike the standard scriptlet).
//
// Note:  You can optionally define a "getAnalyticsURL" function to 
//        create a qualified url (i.e. with your app-specific context), otherwise
//        the ajax url is used (which may not have your app-context on it).
//
function trackGoogleAnalytics(url) {														
	// If they've defined their google analytics UA account:
	if((typeof(_uacct) != 'undefined') && _uacct) {
	
		// if we got a url
		if(typeof(url) != 'undefined' && url) {
			// Call google analytics:
			// Note:  this would typically be done at the end of a page,
			//        but with ajax we call this explicitly after we've
			//        updated the page with the ajax response and set the
			//        new title:
			var analyticsURL;	
			
			// The url submitted to google analytics 
			if((typeof(getAnalyticsURL) != 'undefined') && getAnalyticsURL) {
				 // The app can customize what they want for the url,
				 // (e.g. they can/should add their app context to the front):				 				 
				 analyticsURL = getAnalyticsURL(url);
			} 
			else {
				// Or the default is the original ajax url unmodified:
				analyticsURL = url;
			}															
	
			if(analyticsURL) {			
				urchinTracker(analyticsURL);
			}		
		}
		else {
			// Call the tracker in the default way (where it
			// will get the url itself from the current address:			
			urchinTracker();
		}
	}														
}