Created
August 19, 2013 22:09
-
-
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…
This file contains hidden or 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
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