Last active
May 27, 2019 12:12
-
-
Save JacobLett/99644f62804dfcb54a15 to your computer and use it in GitHub Desktop.
Google Analytics Event Tracking - jQuery
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
| // 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