Shoora
9/29/2016 - 2:59 PM

Google Analytics event tracking

Google Analytics event tracking

// Universal Analytics events that include common element attributes
var gaPush = {
    p: function (element, cat, action, type) {
        var el = document.querySelectorAll(element)
        for (var i = 0; i < el.length; i++) {
            el[i].addEventListener('click', function () {
                var attr = {
                    title: eval('this.title'),
                    alt: eval('this.alt'),
                    value: eval('this.value'),
                    href: eval('this.href'),
                    text: eval('this.text')
                }
                var label = attr[type]
                ga('send', 'event', cat, action, label);
            });
        };
    }
}

// set your type to what attribute you want sent - title, alt, value, href, or text
// uncomment following line to test on any link click
// gaPush.p('a', 'hyperlink', 'Click link', 'text')