/**
 * Track when a user performs an action on the page, such as
 * clicking on a link, via Google Analytics.
 */
logUserAction = function(category, action, label) {
	try {
		_gaq.push(['_trackEvent', category, action, label]);
	} catch(e) {}
};

/**
 * Track when a user converts from an experiment.
 */
trackConversion = function(metric, callback) {
	try {
		jQuery.get('/ajax/track_conversion.php', {metric: metric}, function(data) {
			if (typeof(callback) == 'function') callback(data);
		});
	} catch(e) {}
};

/**
 * Track all upsell link clicks
 */
jQuery(function($) {
	$('a.upsell, a[rel=upsell]').click(function(e) {
		var href = $(this).attr('href');
		
		// track the conversion event on google analytics
		logUserAction('Upsell', $(this).attr('data-action'), $(this).attr('data-label'));
		
		// we need to wait for the AJAX request to complete because it sets a cookie
		// to prevent the same user from being counted as a conversion more than once.
		trackConversion('upsell', function() {
			if (href) {
				window.location.href = href;
			}
		});
		
		e.preventDefault();
	});
});

