if (typeof jQuery != 'undefined') { var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i; jQuery('body').on('click', 'a', function(event) { var el = jQuery(this); var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') : ''; var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]); var trackers = ga.getAll(); var callbackcount = trackers.length; // If we have a javascript link, bail out without tracking if (href.match(/^javascript:/i)) { return true; } var category; var action = 'click'; var label; var fields = { 'nonInteraction': false }; var needs_link_handling = false; if (href.match(/^mailto\:/i)) { category = 'email'; label = href.replace(/^mailto\:/i, ''); } else if (href.match(filetypes)) { var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined; category = 'download'; action = 'click-' + extension[0].toLowerCase(); label = href; needs_link_handling = true; } else if (href.match(/^https?\:/i) && !isThisDomain) { category = 'external link'; label = href; fields.nonInteraction = true; needs_link_handling = true; } else if (href.match(/^tel\:/i)) { category = 'telephone'; label = href.replace(/^tel\:/i, ''); } else { // If we didn't match any of the link types, bail out without tracking return true; } // If the link has a target or was middle clicked, the current page // won't be unloaded, so we don't need any special link handling if ((el.attr('target') != undefined) || (event.which == 2)) { needs_link_handling = false; } // If a link will load in place of the current page (like most "normal" // links), the ga function's asynchronous javascript might not have // finished executing by the time the page goes away. Setting the // needs_link_handling function will block the default link action // from happening, and set a callback that loads it once the // ga function is finished. // TODO: what if GA servers are slow/down and callback doesn't get // called? if (needs_link_handling) { fields['hitCallback'] = function() { callbackcount -= 1; if (callbackcount == 0) { // IE doesn't apply to relative URLs in js... // ALSO jQuery migrate (purposely) doesn't report IE 11 as IE... // TODO: Relative URL check is poorly written. Base it off https://url.spec.whatwg.org/ instead var isrelativeurl = (href.search("\/\/|^\/") == -1); var isIE = jQuery.browser.msie || (/Trident\/7\./).test(navigator.userAgent); if ((jQuery('base').attr('href') !== undefined) && isIE && isrelativeurl) { var fullhref = jQuery('base').attr('href') + href; window.location = fullhref; } else { window.location = href; } } }; } // Actually send the analytics requests for (var i = 0; i < trackers.length; i++) { var trackername = trackers[i].get('name'); ga(trackername+'.send', 'event', category, action, label, fields); } // If we need the special link handling, return false to stop the default link action if (needs_link_handling) { return false; } return true; }); }