Shoora
4/29/2019 - 2:08 PM

For GA event tracking.

For GA event tracking.

/**
 * Make sure you use this to track any clicks. It does more than just track().
 * @param  {Event} event    The event object for the click event.
 * @param  {Object} elem     The actual DOM element.
 * @param  {string} category 
 * @param  {string} action
 * @param  {string} label
 * @param  {string} value
 */
trackClick: function( event, elem, category, action, label, value )
{
	$elem = $( elem );

	// If this is a link, we want to prevent the following of the link.
	// https://support.google.com/analytics/answer/1136920?hl=en
	// We want to delay a little bit so we can track that this link was clicked before following.
	if ( $elem.prop( 'tagName' ).toLowerCase() == 'a' ) {

		// We don't want to prevent default and handle the link following ourselves if they are opening in new window.
		// That's because we'll have time to log the click if they open in new.
		// Meta key is the windows key, ctrl is obvious, and e.which of 2 is the middle click button.
		var newTab = false;
		if ( event.metaKey || event.ctrlKey || event.which == 2 ) {
			newTab = true;
		}
		
		if ( !newTab && $elem.attr( 'href' ) && $elem.attr( 'href' ) != '#' ) {
			setTimeout( function()
			{
				// If we want to open in new window, then do so.
				if ( $elem.hasClass( 'ga-event-newwindow' ) ) {
					window.open( $elem.attr( 'href' ) );
				}
				else {
					document.location.href = $elem.attr( 'href' );
				}
			}, 100 );
			event.preventDefault();
		}
	}
},