Last active
September 27, 2019 03:58
-
-
Save radist2s/dc68305bd29fe48fca9b to your computer and use it in GitHub Desktop.
Google Analytics event catcher
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
document.addEventListener('click', function (e) { | |
if (e.target.hasAttribute('data-ga')) { | |
catchGaEvent(e) | |
} | |
}) | |
function catchGaEvent(e) { | |
var el = e.target | |
var event = el.getAttribute('data-ga'), | |
action = el.getAttribute('data-ga-action'), | |
description = el.getAttribute('data-ga-desc') || el.getAttribute('data-ga-description') | |
if (!event || !action) { | |
return | |
} | |
var categories = String(event).split('|'), | |
actions = String(action).split('|') | |
categories.forEach(function (cat) { | |
actions.forEach(function (act) { | |
// (window._gaq = window._gaq || []).push(['_trackEvent', cat, act, description]) | |
window.ga && ga('send', 'event', cat, act, description) | |
}) | |
}) | |
} |
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 class="adv" href="external.html" target="_blank" | |
data-ga="ExternalLink" data-ga-action="Adv" data-ga-desc="External link to Adv"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment