/**
* Name:		jQuery plugin - Google Anaytics asynchronous event tracking.
* Author:	Nathan Winch
* Version:	1.0
* Description: jquery plugin to enable event tracking by adding class identifier to tracked elements.
*
*
* example usage:
*
* Add new the asynchronous ga code to the TOP of the page (so you can init the _gaq object before calling plugin).
* Note you can split the code in parts between the top and bottom of the page if you wish - calling 'ga.js' at the
* bottom may increase load times for other parts of the page.
* See google doco here: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html (Migration Instructions)
*
*
* Apply the class 'gaTracking[value]' to any element you would like to record clicks on.
* When including spaces, use '_' instead - this will then be converted to ' ' when event is triggered.  'value' is the name of the event
* that will show up in GA admin.
*
* eg. <a href="http://google.com" class="bigtext gaTracking[google]"> ... </a>
*
* on page load, call:
$('a').ga({
	target		:'gaTracking',		// (optional) target class to search for on items - default is 'gaTracking'.
	accountId	:'UA-XXXXXXXX-XX',	// whatever the account id is.
	gaObj		:_gaq 				// the ga object, this is '_gaq' by default.
});
*
*
* PAGE EXAMPLE

<html>

<head>
<script type="text/javascript">
	var _gaq = _gaq || [];
  	_gaq.push(['_setAccount', 'UA-XXXXXXXX-XX']);
  	_gaq.push(['_trackPageview']);
</script>
<script src="_js/lib/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="_js/lib/jquery.ga.1.0.js" type="text/javascript" charset="utf-8"></script>
<script>
/** can add this next bit to an external script file or also inline on page if you like.
$('a').ga({
	accountId	:'UA-XXXXXXXX-XX',
	gaObj		: _gaq
});
</script>

</head>

<body>
...

...

<script type="text/javascript">
	(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);
  	})();
</script>

</body>

</html>

*
*/

(function($) {

	$.fn.ga = function(settings) {

		var opts = $.extend({}, $.fn.ga.defaults, settings);

		return this.each(function() {
			$(this)
				.click(function() {
					if ($(this).is('[class*=' + opts.target + ']')) {
						var flRegex = new RegExp('\\b' + opts.target + '\\[(.*?)\\]', 'ig'),
							flMatch = flRegex.exec($(this).attr('class'));
						if (!flMatch) return false;
						var val = flMatch[1];

						$.fn.ga.track(val);
					}
				});
		});
	};

	$.fn.ga.track	= function(val) {
		try {
			opts.gaObj.push(['_trackEvent', val, 'clicked']);
		} catch(error) {
			debug('sorry, tracking not enabled correctly.');
		}
	}

	$.fn.ga.defaults = {
        target		: 'gaTracking',
		accountId	: '',
		gaObj		: ''
    };

	function debug(msg) {
		if (window.console && window.console.log) {
  			window.console.log(msg);
  		}

	}

})(jQuery);


