Skip to content

Instantly share code, notes, and snippets.

@JacobLett
Last active May 27, 2019 12:12
Show Gist options
  • Select an option

  • Save JacobLett/99644f62804dfcb54a15 to your computer and use it in GitHub Desktop.

Select an option

Save JacobLett/99644f62804dfcb54a15 to your computer and use it in GitHub Desktop.
Google Analytics Event Tracking - jQuery
// A $( document ).ready() block.
$( document ).ready(function() {
console.log( "ready!" );
// Tracks btn clicks - link needs title tag and class="btn" to work
$('.btn').click(function(){
_gaq.push(['_trackEvent', 'btn clicks', 'Click', $(this).attr('title')]);
});
// Tracks PDF downloads - link needs title tag and class="pdf" to work
$('.pdf').click(function(){
_gaq.push(['_trackEvent', 'PDF Download', 'Click', $(this).attr('title')]);
});
///////////////////
// _trackError: track 404 - Page not found
if (document.title.search(/Page Not Found/i) !== -1) {
_gaq.push(['_trackPageview', '/vpv/404/' + location.host + location.pathname + '?from=' + document.referrer]);
}
///////////////////
// _trackMailTo
$('a[href^="mailto"]').on('click', function(e) {
_gaq.push(['_trackSocial', 'email', 'send', this.href.replace(/^mailto:/i, '')]);
});
///////////////////
// _trackOutbound
$('a[href^="http"]:not([href*="//' + location.host + '"])').on('click', function(e) {
_gaq.push(['_trackEvent', 'outbound', 'click', this.href.match(/\/\/([^\/]+)/)[1]]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment