Skip to content

Instantly share code, notes, and snippets.

@adrianengine
Created August 19, 2013 22:09
Show Gist options
  • Save adrianengine/6274763 to your computer and use it in GitHub Desktop.
Save adrianengine/6274763 to your computer and use it in GitHub Desktop.
Event Tracking - Web Tracking (analytics.js), In this example, the addEventListener function is a utility to add event listeners across browsers. This function is used to add an event listener to the PDF download link to listen for the click event. When a click event occurs, the event is sent to Google Analytics. Source (https://developers.googl…
var downloadLink = document.getElementById('button');
addListener(downloadLink, 'click', function() {
ga('send', 'event', 'button', 'click', 'nav-buttons');
});
/**
* Utility to wrap the different behaviors between W3C-compliant browsers
* and IE when adding event handlers.
*
* @param {Object} element Object on which to attach the event listener.
* @param {string} type A string representing the event type to listen for
* (e.g. load, click, etc.).
* @param {function()} callback The function that receives the notification.
*/
function addListener(element, type, callback) {
if (element.addEventListener) element.addEventListener(type, callback);
else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
// Using jQuery Event API v1.3
$('#button').on('click', function() {
ga('send', 'event', 'button', 'click', 'nav-buttons');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment