STAGING not indexed production at recoveryarea.nl

javascript · · 1 min read

Track That!

A pocket-sized click introspection helper that JSON-stringifies the source element, its index, text, and every attribute — useful as A/B test custom tracking.

JSON output of the click introspection helper showing tag, index, text, and attributes of a clicked button

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));
});