Shoora
5/27/2019 - 12:12 PM

Google Analytics Event Tracking - jQuery

Google Analytics Event Tracking - jQuery

// A $( document ).ready() block.
$( document ).ready(function() {
    console.log( "ready!" );
    
    // Tracks btn clicks - link needs title tag and class="btn" to work
	$('.btn').click(function(){
		_gaq.push(['_trackEvent', 'btn clicks', 'Click', $(this).attr('title')]);
	});
	
	// Tracks PDF downloads - link needs title tag and class="pdf" to work
	$('.pdf').click(function(){
		_gaq.push(['_trackEvent', 'PDF Download', 'Click', $(this).attr('title')]);
	});
	

	///////////////////
	// _trackError: track 404 - Page not found
	if (document.title.search(/Page Not Found/i) !== -1) {
		_gaq.push(['_trackPageview', '/vpv/404/' + location.host + location.pathname + '?from=' + document.referrer]);
	}

	///////////////////
	// _trackMailTo
	$('a[href^="mailto"]').on('click', function(e) {
	    _gaq.push(['_trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
	});

	///////////////////
	// _trackOutbound
	$('a[href^="http"]:not([href*="//' + location.host + '"])').on('click', function(e) {
	    _gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
	});
    
});