(function (ns) { function onGtmEventClick(e) { onGtmEventNodeClick(e.currentTarget) } function onGtmEventNodeClick(node) { var $node, data, event; $node = $(node); data = parseEventAttr($node.data('gtmEvent')); if (null == data || $node.data('gtmEventProcessed')) { return; } $node.data('gtmEventProcessed', 1); setTimeout(function () { $node.data('gtmEventProcessed', null); }, 100); event = { 'event': 'gaTriggerEvent', 'gaEventCategory': data.hasOwnProperty('category') ? data.category : '', 'gaEventAction': data.hasOwnProperty('action') ? data.action : '', 'gaEventLabel': data.hasOwnProperty('label') ? data.label : '' }; if ('dataLayer' in window) { dataLayer.push(event); } } function parseEventAttr(text) { if (!text) { return; } var data, list, field, name, value, i, c; list = String(text).split(','); for (i = 0, c = list.length; i < c; ++i) { if (list[i] && (field = String(list[i]).split(':')).length >= 2) { name = field[0].replace(/^\s+|\s+$/g,''); value = field[1].replace(/^\s+|\s+$/g,''); if (!data) data = {}; data[name] = value; } } return data; } ns.gtmEvent = function () {}; ns.gtmEvent.triggerByNode = function (node) { onGtmEventNodeClick(node); }; // data-gtm-event="category:PrintResults,action:ClickButton" $(function () { $('body').on('click.gtmEvent', '[data-gtm-event]', onGtmEventClick); }); })(qs.defineNS('app'));