Created
May 30, 2013 21:41
-
-
Save tedw/5681488 to your computer and use it in GitHub Desktop.
Google Analytics event tracking using 'data-track-event' attribute on links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Google Analytics event tracking using data attribute */ | |
var analyticsTrackEvent = function() { | |
$('[data-track-event]').on('click', function () { | |
var trackEventData = $(this).data('track-event') || ''; | |
if ( trackEventData.length ) { | |
var eventArray = trackEventData.split(','); | |
// Make sure there are at least 2 values | |
if ( eventArray.length >= 2 ) { | |
// Split up the track event data into GA variables | |
var trackCategory = eventArray[0], // Required (String) | |
trackAction = eventArray[1], // Required (String) | |
trackLabel = eventArray[2], // Optional (String) | |
trackValue = eventArray[3]; // Optional (Number) | |
// Send event data to GA | |
ga('send', 'event', trackCategory, trackAction, trackLabel, trackValue); | |
console.log(trackCategory, trackAction, trackLabel, trackValue); | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment