Shoora
7/1/2018 - 2:39 PM

Google Analytics Track Events using Universal Analytics

Google Analytics Track Events using Universal Analytics

	var socialEvents = {
		'#header #nav-social-facebook a':['facebook', 'click', 'https://www.facebook.com/'],
		'#footer .visit a': ['facebook', 'click', 'https://www.facebook.com/'],
		'#footer .share a[href^="http://www.facebook.com/"]': ['facebook', 'share', 'https://www.facebook.com/'],
		'#footer .share a[href^="http://twitter.com/"]': ['twitter', 'share', 'https://www.twitter.com/']
	};
	
	$.each(socialEvents, function (selector, args) {
		var el = $(selector);
		if (!el.length) return;
		el.click(function (e) {
			ga.apply(null, ['send', 'social'].concat(args));
		});
	});
$.trackEvents({
	'/ #header #nav #nav-item-1 a ': ['Header', 'Nav Click', 'Section Name'],
	'/ #header #nav #nav-item-2 a ': ['Header', 'Nav Click', 'Section Name'],
	'/ #header #nav #nav-item-3 a ': ['Header', 'Nav Click', 'Section Name'],
	'/ #header #nav #nav-item-4 a ': ['Header', 'Nav Click', 'Section Name'],
	'/ #header #nav #nav-item-5 a ': ['Header', 'Nav Click', 'Section Name'],
	'/ #header #nav #nav-item-7 a ': ['Header', 'Nav Click', 'Section Name'
]});

jQuery.trackEvents = (function ($) {
	return function (events) {
		var pathname = window.location.pathname;
		
		$(function () {
			$.each(events, function (element, args_or_func) {
				var element_parts = element.match(/^(?:(\/\S*) )?(.*?)(?:!(.*))?$/);
				var event_type = element_parts[3] || 'click';
		
				if (element_parts[1] && element_parts[1] != pathname) {
					return;
				}
		
				$(document).delegate(element_parts[2], event_type, function (e) {
					var args = args_or_func;
			
					if (typeof args == 'function') {
						args = args.apply(this, Array.prototype.slice.call(arguments));
					}
			
					if (!args) {
						return;
					}
					window.ga && ga.apply(null, ['send', 'event'].concat(args));
			
					var el = $(this);
			
					if (el.is('a[href]') && el.attr('target') != '_blank' && !e.isDefaultPrevented()) {
						e.preventDefault();
						setTimeout('document.location = "' + el.attr('href') + '"', 100);
					}
				});
			});
		});
	};
})(jQuery);