Last active
October 12, 2015 13:38
-
-
Save bergantine/4035433 to your computer and use it in GitHub Desktop.
Google Analytics Event Tracking. #googleanalytics #javascript
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
| // requires jQuery | |
| // insert after _gaq is declared but before you talk to Google | |
| // track all clicks on links as events | |
| $('a').click(function(e) { | |
| var debug = false; // if set to true will log instead of calling google | |
| if (this.hostname && this.hostname !== location.hostname) { | |
| var eCat = 'External Link'; | |
| } else { | |
| var eCat = 'Internal Link'; | |
| } | |
| var eAction = 'Click'; | |
| var t = $(this); | |
| var eLabel = t.attr('href'); | |
| if (t.attr('id')) { | |
| eLabel = window.location + '#' + t.attr('id') + ' -> ' + eLabel; | |
| } else { | |
| eLabel = window.location + ' -> ' + eLabel; | |
| } | |
| if (debug == true) { | |
| e.preventDefault(); | |
| console.log(eCat, eAction, eLabel) | |
| } else { | |
| _gaq.push(['_trackEvent', eCat, eAction, eLabel]); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment