Created
April 15, 2015 09:18
-
-
Save panayotoff/fb27097c276d1f43270e to your computer and use it in GitHub Desktop.
Little Google Analytics tracking helper I use in some of my projects, requires jQuery.extend() method
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 tracking helper | |
// @Author : Chris Panayotov / info[at]bassta[dot]bg | |
//-------------------------------------------------------------- | |
// Usage: tracker.track({eventCategory: '', eventLabel:''}); | |
// @eventCategory : Typically the object that was interacted with (e.g. button) | |
// @eventAction : The type of interaction (e.g. click) | |
// @eventLabel : Useful for categorizing events (e.g. nav buttons) | |
// More info @ https://developers.google.com/analytics/devguides/collection/analyticsjs/events | |
//-------------------------------------------------------------- | |
var tracker = (function ($, window) { | |
var hasGA = window.ga; | |
var defaults = { | |
eventCategory: 'button', | |
eventAction: 'click', | |
eventLabel: '' | |
}; | |
function track(_trackObj) { | |
var trackObj = $.extend(defaults, _trackObj); | |
if (hasGA) { | |
window.ga('send', { | |
'hitType': 'event', | |
'eventCategory': trackObj.eventCategory, | |
'eventAction': trackObj.eventCategory, | |
'eventLabel': trackObj.eventLabel | |
}); | |
} else { | |
console.log('[Google Analytics tracker not found, failed to track]', trackObj); | |
} | |
} | |
return {track: track}; | |
})(jQuery, window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment