DEV Community

Pascal Louwes
Pascal Louwes

Posted on • Originally published at recoveryarea.nl on

Track That!

This function tracks every event with all its attributes in the div that’s set in the caller function. Handy if you need custom tracking for your AB test.


var trackThat = function() {

    var t = {},
        r = {},
        a = window.event ? window.event.srcElement : i.target,
        c = document.getElementsByTagName(a.tagName),
        k = "";
    try {
        for (var d = 0; d < c.length; ++d) c[d] == a && (r.tag = a.tagName, r.index = d, r.text = a.hasChildNodes() ? a.childNodes[0].data : "null"); for (var l, d = 0, o = a.attributes, g = o.length; g > d; d++) l = o[d], t[l.nodeName] = l.nodeValue;
        r.attrs = t, k = JSON.stringify(r);
    } catch (i) {
        k = "error: " + i.message;
    } finally {
        return k;
    }
};

$( 'div.main-holder' ).on('click', function(e){
    console.log(trackThat(e));
});

Enter fullscreen mode Exit fullscreen mode

The post Track That! appeared first on recoveryArea.

Top comments (0)