/* •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
   ••  Project: jQuery Google Analytics Tracker                     ••
   ••  Author:  delarueguillaume@gmail.com                          ••
   ••  WebSite : http://www.web2ajax.fr/                            ••
   ••  Date:    2010                                                ••
   ••  Version: 1.0 (05 march 2010)                                 ••
   •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••   
   
    Defer Google Analytics Tracking and can track a page with just 
    a call to the plugin.
    
---------------------------------------------------------------------- */

(function($){
	$.gaTracker = {
	
		// To store the Google Analytics Code
		code: false,
		
		// Log
		log: function(t) {
			if ( typeof console != 'undefined' ) {
				console.log(t);
			}
		},
		
		// Track View
		track: function(url) {
			var self = this ;
			if ( self.code ) {
				var pageTracker = _gat._getTracker(self.code);
				if ( typeof url != 'undefined' ) {
					self.log('Tracking GA : '+url);
					pageTracker._trackPageview(url);	
				} else {
					self.log('Tracking GA : current page');
					pageTracker._trackPageview();		
				}
			} else {
				self.log('Google Analytics Tracker is not ready') ;
			}
		},
		
		// Init GA
		init: function(code) {
			var self = this ;
			self.log('Init Async GA');
			if ( typeof code != 'undefined' && this.code === false ) this.code = code ;
			if ( ! this.code ) log('Google Analytics UA must be entered') ;
			else {
				try{
					// determine whether to include the normal or SSL version
					var gaURL = (location.href.indexOf('https') == 0 ? 'https://ssl' : 'http://www');
					gaURL += '.google-analytics.com/ga.js';
			
					// include the script
					$.getScript(gaURL, function(){
						self.track() ;
					});
				} catch(err) {
					// log any failure
					self.log('Failed to load Google Analytics:' + err);
				}
			}
		}
	};
})(jQuery);
