Shoora
10/18/2012 - 3:08 PM

Google Analytics example

Google Analytics example

// 1. basic tracking code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-35559116-1']);
_gaq.push(['_setDomainName', 'collegecoding.com']);
_gaq.push(['_trackPageview']);

(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);
})();

// 2. how to track a custom event
_gaq.push(['_trackEvent', 'Buy Now', 'Click', selectedOption]);

// 3. how to track a custom event on an outbound link
// you need to introduce a small delay for the Analytics request to go through
$("#paypal-form").on("submit", function() {
	var selectedOption = $("[name=os0]").val();
	_gaq.push(['_trackEvent', 'Buy Now', 'Click', selectedOption]);
	
	// submit the form after a small delay
	setTimeout(function() {
		$("#paypal-form").get(0).submit();
	}, 100);

	return false;
});

// 4. how to trigger an AdWords conversion
// https://adwords.google.com/o/ConversionTracking
var image = new Image(1,1); 
image.src = "http://www.googleadservices.com/pagead/conversion/GOOGLE_CONV_ID/?label=GOOGLE_CONV_LABEL&script=0";