1window.hmgTrackingDefaults = window.hmgTrackingDefaults || {}; 2 3function addEventToDataLayer(dataObject, eventConfig) { 4 const { eventTarget, eventType, addData, checkGuards, throttle, delay, requiredProps } = eventConfig; 5 6 let elements; 7 let runCallback = true; 8 if (eventTarget === document || eventTarget === window) { 9 elements = [eventTarget]; 10 } else { 11 elements = document.querySelectorAll(eventTarget); 12 } 13 14 elements.forEach(function(element) { 15 16 element.addEventListener(eventType, function(event) { 17 18 if ( throttle ){ 19 20 // throttle the event 21 if ( !runCallback ) { 22 return; 23 } 24 runCallback = false; 25 setTimeout(() => { 26 runCallback = true; 27 }, 1000); 28 } 29 30 // break if conditional callback is set and returns a falsy value 31 if (checkGuards && !checkGuards(element, event)) { 32 return; 33 } 34 // set breakpoint on every event 35 dataObject.breakpoint = getBreakpoint(); 36 37 let addDataObject = {}; 38 // check if callback is callable 39 if (typeof addData === 'function') { 40 const result = addData(element, event); 41 if (result && typeof result === 'object') { 42 addDataObject = result; 43 } 44 } 45 46 // Merge the defaultDataLayer with the dataObject and the data from the callback 47 const mergedData = { ...hmgTrackingDefaults, ...dataObject, ...addDataObject }; 48 49 // Make sure the required properties are always set. 50 // If a property doesn't exist, set it to a default value. 51 if (requiredProps) { 52 if (Array.isArray(requiredProps)) { 53 // If requiredProps is an array 54 requiredProps.forEach((property) => { 55 if (!mergedData.hasOwnProperty(property)) { 56 // default value is an empty string 57 mergedData[property] = ''; 58 } 59 }); 60 } else if (typeof requiredProps === 'object') { 61 // If requiredProps is an object 62 Object.keys(requiredProps).forEach((property) => { 63 if (!mergedData.hasOwnProperty(property)) { 64 // default value from requiredProps 65 mergedData[property] = requiredProps[property]; 66 } 67 }); 68 } 69 } 70 71 // Push the merged data into the dataLayer 72 window.dataLayer = window.dataLayer || []; 73 if ( delay ){ 74 setTimeout(() => { 75 window.dataLayer.push(mergedData); 76 }, delay); 77 } else { 78 window.dataLayer.push(mergedData); 79 } 80 }); 81 }); 82} 83 84function initializeEventTracking(eventConfigs) { 85 Object.values(eventConfigs).forEach(({ dataObject, eventConfig }) => { 86 addEventToDataLayer(dataObject, eventConfig); 87 }); 88} 89 90function getBreakpoint() { 91 if (window.matchMedia('(min-width: 1440px)').matches) { 92 return 'xl'; 93 } else if (window.matchMedia('(min-width: 1024px)').matches) { 94 return 'l'; 95 } else if (window.matchMedia('(min-width: 768px)').matches) { 96 return 'm'; 97 } else { 98 return 's'; 99 } 100} 101 102function sanitizeTitle(title, settings = {toLower: true, hyphenate: true}) { 103 if ( settings.toLower ){ 104 // Convert to lowercase 105 title = title.toLowerCase(); 106 } 107 title = title 108 // Remove leading/trailing whitespace 109 .trim() 110 // Remove non-word characters except spaces, hyphens and umlauts 111 .replace(/[^\w\s-äöüÃÃÃÃ]/g, ''); 112 if ( settings.hyphenate ){ 113 // Replace spaces, underscores, and multiple hyphens with a single hyphen 114 title = title.replace(/[\s_-]+/g, '-'); 115 } 116 // Remove leading/trailing hyphens 117 title = title.replace(/^-+|-+$/g, ''); 118 119 return title; 120} 121 122function getMainDomainName(url) { 123 try { 124 var hostname = new URL(url).hostname; 125 var parts = hostname.split('.'); 126 127 // Get the main domain (the second to last part if there are at least 3 parts, otherwise the first part) 128 var mainDomain = (parts.length > 2) ? parts[parts.length - 2] : parts[0]; 129 130 return mainDomain; 131 } catch (e) { 132 console.error('Invalid URL'); 133 return ''; 134 } 135} 136
137export { initializeEventTracking, getBreakpoint, sanitizeTitle, getMainDomainName };
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.