Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active September 27, 2019 03:58
Show Gist options
  • Save radist2s/dc68305bd29fe48fca9b to your computer and use it in GitHub Desktop.
Save radist2s/dc68305bd29fe48fca9b to your computer and use it in GitHub Desktop.
Google Analytics event catcher
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)
})
})
}
<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