Skip to content

Instantly share code, notes, and snippets.

@dmtintner
Last active October 21, 2022 17:49
Show Gist options
  • Save dmtintner/5af411bf919879e37d7e to your computer and use it in GitHub Desktop.
Save dmtintner/5af411bf919879e37d7e to your computer and use it in GitHub Desktop.
HackingUI.Analytics
hackingUi = hackingUi || {};
hackingUi.Analytics = {
trackEvent: function(category, action, label) {
var eventObject = {
'eventCategory': category,
'eventAction': action
},
mixpanelName = category;
if (label) {
eventObject.eventLabel = label;
mixpanelName = mixpanelName + ': ' + label;
}
if (window.ga) {
ga('send', 'event', eventObject);
}
if (window.mixpanel) {
mixpanel.track(mixpanelName);
}
},
bindToAllClicks: function() {
var _this = this;
$('body').on('click', '[data-analytics-category]', function() {
var category = $(this).data('analyticsCategory'),
label = $(this).data('analyticsLabel');
_this.trackEvent(category, 'click', label);
});
},
init: function() {
this.bindToAllClicks();
}
};
<a href="http://hackingui.com" data-analytics-category="Header" data-analytics-label="logo">HackingUI</a>
$(function() {
hackingUi.Analytics.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment