Forked from kevinblake/google-analytics-data-events.js
Created
June 13, 2019 15:00
-
-
Save dobeerman7/e1433246bb2ec7c2828f8efa5866fe48 to your computer and use it in GitHub Desktop.
Fire Google Analytics Events using data attributes for label, action and category fields
This file contains 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 googleDataEvents = { | |
pageTracker: null, | |
init: function (document) { | |
document.find("a[data-ga-label],area[data-ga-label]").click(this.trackLink); | |
}, | |
trackLink: function (e) { | |
if (_gaq) { | |
e.preventDefault(); | |
var l = $(this); | |
var label = l.attr("data-ga-label"); | |
var action = l.attr("data-ga-action"); | |
var category = l.attr("data-ga-category"); | |
_gaq.push(['_trackEvent', category, action, label]); | |
if (l.attr("data-ga-nofollow") != "true") { | |
if (l.attr("target") && l.attr("target") != "") { | |
window.open(l.attr("href"), l.attr("target")); | |
} else { | |
setTimeout("document.location = '" + l.attr("href") + "'", 100); | |
} | |
} | |
} | |
} | |
}; | |
$(document).ready(function () { googleDataEvents.init($("html")); }); |
This file contains 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 href="http://www.google.com" data-ga-category="My Category" data-ga-action="My Action" data-ga-label="My Label">Link text</a> | |
<a href="http://www.google.com" data-ga-category="My Category" data-ga-action="My Action" data-ga-label="My Label" data-da-nofollow="true">Link text (don't redirect to the href)</a> | |
<a href="http://www.google.com" target="_blank" data-ga-category="My Category" data-ga-action="My Action" data-ga-label="My Label">Link text (new window)</a> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment