Last active
September 27, 2019 09:21
-
-
Save crsnbrt/9427752d41070fe25ea2 to your computer and use it in GitHub Desktop.
Google Analytics Event Helper
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
//ga click helper | |
$('[data-tracking]').on('click', function(){ | |
var track_string = $(this).data('tracking').toLowerCase(); | |
if(!track_string){return false;} | |
var track_arr = track_string.split(","); | |
if(track_arr.length < 2){return false;} | |
var category = track_arr[0]; | |
var action = track_arr[1]; | |
var label = track_arr[2]; | |
var value = track_arr[3]; | |
track(category,action,label,value); | |
}); | |
//ga send helper | |
function track(category, action, label, value){ | |
ga('send', { | |
'hitType': 'event', | |
'eventCategory': category || "", | |
'eventAction': action || "", | |
'eventLabel': label || "", | |
'eventValue': value || "" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use add the data-tracking attribute to anything to be tracked on the click event.
example:
data-tracking="category,action,label,value"