Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active October 12, 2015 13:38
Show Gist options
  • Select an option

  • Save bergantine/4035433 to your computer and use it in GitHub Desktop.

Select an option

Save bergantine/4035433 to your computer and use it in GitHub Desktop.
Google Analytics Event Tracking. #googleanalytics #javascript
// 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