1/** 2 * Convert a string to title case. 3 * 4 * @param {*} str 5 * @returns {string} 6 */ 7function toTitleCase(str) { 8 return str.replace(/\w\S*/g, function (txt) { 9 return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); 10 }); 11} 12 13/** 14 * Send the event to Google Tag Manager. 15 * 16 * @param {HTMLElement} element The element that was clicked. 17 */ 18function sendGTMEvent(element) { 19 const attributeList = window.tenupDataLayer.trackingAttrs || []; 20 21 const data = attributeList.reduce((acc, attribute) => { 22 const value = element.getAttribute(attribute); 23 if (value) { 24 const key = attribute.replace('data-', ''); 25 26 // Force proper case. 27 acc[key] = toTitleCase(value); 28 } 29 return acc; 30 }, {}); 31 32 /** 33 * If the ctaText is not set, use the innerText of the element. 34 * WP_HTML_Tag_Processor is limited in what it can do, so we can't 35 * assign the data-ctaText attribute. 36 */ 37 if (!data.ctaText) { 38 data.ctaText = element.innerText.trim(); 39 } 40 41 window.dataLayer = window.dataLayer || []; 42 window.dataLayer.push(data); 43} 44 45/** 46 * Add event listeners to the document. 47 */ 48document.addEventListener('DOMContentLoaded', () => { 49 // Add event listeners to all elements with a data-event attribute. 50 document.addEventListener('click', function (event) { 51 const clickedElement = event.target; 52 const eventElement = clickedElement.closest('[data-event]'); 53 54 // Bail if we can't find an element with a data-event attribute. 55 if (!eventElement) { 56 return; 57 } 58 59 // Send the event to Google Tag Manager. 60 const dataEvent = eventElement.getAttribute('data-event'); 61 if (dataEvent) { 62 sendGTMEvent(eventElement); 63 } 64 }); 65 66 // Add event listeners to all internal links without a data-event attribute. 67 const hyperLinks = document.querySelectorAll('a:not([data-event])'); // Select only links without data-event attribute. 68 hyperLinks.forEach((link) => { 69 // Check if it's an internal link. 70 if (link.host === window.location.host) { 71 link.addEventListener('click', () => { 72 const ctaText = link.innerText; 73 const destinationLink = link.href; 74 75 window.dataLayer = window.dataLayer || []; 76 window.dataLayer.push({ 77 event: 'recirculation', 78 module: 'hyperlink', 79 ctaText, 80 destinationLink, 81 }); 82 }); 83 } 84 }); 85});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.