1/* Source and licensing information for the line(s) below can be found at https://www.cusquena.com/modules/custom/ab_datalayer_pusher/dist/js/main.js. */ 2(function ($, Drupal) { 3 $.fn.dl_ajax_push = function(data) { 4 let array_datalayer = prepare_datalayer_format_BACKEND(data.basic_properties, data); 5 window.dataLayer = window.dataLayer || []; 6 window.dataLayer.push(array_datalayer); 7 dl_info('Caught "Send by Backend" event named: '+data.name); 8 dl_info('Pushing datalayer:'); 9 dl_info(array_datalayer); 10 }; 11 const debug_mode = drupalSettings.datalayersPuehser.debug_mode; 12 dl_info("DataLayer Pusher is loaded and the debug mode is activated. (please disable it if it is a production site)"); 13 14 $(document).ready(function(){ 15 var datalayer_config = []; 16 datalayer_config = drupalSettings.datalayersPuehser.datalayer_config; 17 if(datalayer_config){ 18 dl_info('There are '+datalayer_config.length+ " datalayers structured settled using the DataLayer Pusher module.") 19 datalayer_config.forEach(element => { 20 if(element.trigger == 'Click' || element.trigger == 'Click on Link'){ 21 $('body').delegate(element.selector, 'click', function(e) { 22 if(element.trigger == 'Click on Link'){ 23 // Stop the link behavior if it is necesary 24 if($(this).data('dl-href')){ 25 let href = $(this).data('dl-href'); 26 if(!href == 'false'){ 27 e.preventDefault(); 28 } 29 }else{ 30 e.preventDefault(); 31 } 32 } 33 let array_datalayer = prepare_datalayer_format_HTML(element.basic_properties, $(this), element); 34 window.dataLayer = window.dataLayer || []; 35 window.dataLayer.push(array_datalayer); 36 dl_info('Caught "Click" event by using the selector: '+element.selector+'. named: '+element.name); 37 dl_info('Pushing datalayer:'); 38 dl_info(array_datalayer); 39 40 if(element.trigger == 'Click on Link'){ 41 // Restart the link behavior if it is necesary 42 if($(this).data('dl-href')){ 43 let href = $(this).data('dl-href'); 44 if(!href == 'false'){ 45 location.href = href; 46 } 47 }else{ 48 let href = $(this).attr('href'); 49 location.href = href; 50 } 51 } 52 53 }) 54 }else if (element.trigger == 'If it exists in the DOM'){ 55 if($(element.selector)[0]){ 56 let array_datalayer = prepare_datalayer_format_HTML(element.basic_properties, $(element.selector), element); 57 window.dataLayer = window.dataLayer || []; 58 window.dataLayer.push(array_datalayer); 59 dl_info('Caught "If it exists in the DOM" event by using the selector: '+element.selector+'. named: '+element.name); 60 dl_info('Pushing datalayer:'); 61 dl_info(array_datalayer); 62 } 63 }else if(element.trigger == 'Send by Backend' || element.trigger == 'Send by Webform Handler'){ 64 let array_datalayer = prepare_datalayer_format_BACKEND(element.basic_properties, element); 65 window.dataLayer = window.dataLayer || []; 66 window.dataLayer.push(array_datalayer); 67 dl_info('Caught "Send by Backend" event named: '+element.name); 68 dl_info('Pushing datalayer:'); 69 dl_info(array_datalayer); 70 }else{ 71 dl_alert('Datalayer pusher wrong configuration:'); 72 dl_alert(element); 73 } 74 }); 75 } 76 }) 77 78 function prepare_datalayer_format_HTML(basic_properties, dom_element, datalayer_structure_element) { 79 let datalayerArray = new Object(); 80 basic_properties = basic_properties.split(";"); 81 for (let index = 0; index < basic_properties.length; index++) { 82 const property = basic_properties[index]; 83 // I'll check if the property has a default value 84 property_with_default = property.split('='); 85 if(property_with_default.length === 2){ 86 //it has default value 87 //Chek if it has to get any contento form the element 88 sub_property_element = property_with_default[1].split('|'); 89 if(sub_property_element.length === 2){ 90 // type of gets 91 if(sub_property_element[0] == 'element'){ 92 // Elemtn 93 if(sub_property_element[1] == 'content'){ 94 // HTML content 95 datalayerArray[property_with_default[0]] = dom_element.html().trim(); 96 } else if (sub_property_element[1] == 'option_selected'){ 97 datalayerArray[property_with_default[0]] = dom_element.find(":selected").text() 98 } 99 } 100 }else{ 101 datalayerArray[property_with_default[0]] = property_with_default[1]; 102 } 103 }else{ 104 // it don't have a default value. So, we need to take it from a data attribute 105 let value_current_property = 'null';
106 //In the documentation (Readme.md file) you can see that any data attribute cannot use '_'. so, it will replace all '_" for a '-' to find the data attribute. 107 let property_formatted = property.replace('_', '-'); 108 if(dom_element.data(property_formatted)){ 109 value_current_property = dom_element.data(property_formatted) 110 } 111 if(value_current_property == 'null'){ 112 dl_alert('There is an unknown or undefined property in the HTML element:') 113 dl_alert({ 114 'Name': datalayer_structure_element.name, 115 'Selector': datalayer_structure_element.selector, 116 'Unknown or undefined property': property, 117 }) 118 } 119 datalayerArray[property] = value_current_property; 120 } 121 122 } 123 return datalayerArray; 124 } 125 function prepare_datalayer_format_BACKEND(basic_properties, datalayer_structure_element){ 126 let datalayerArray = new Object(); 127 basic_properties = basic_properties.split(";"); 128 for (let index = 0; index < basic_properties.length; index++) { 129 const property = basic_properties[index]; 130 // I'll check if the property has a default value 131 property_with_default = property.split('='); 132 if(property_with_default.length === 2){ 133 //it has default value 134 datalayerArray[property_with_default[0]] = property_with_default[1]; 135 }else{ 136 dl_alert('There is an unknown or undefined property in the HTML element:') 137 dl_alert({ 138 'Name': datalayer_structure_element.name, 139 'Selector': datalayer_structure_element.selector, 140 'Unknown or undefined property': property, 141 }) 142 datalayerArray[property] = 'null'; 143 } 144 145 } 146 return datalayerArray; 147 } 148 function dl_info(data){ 149 if(debug_mode){ 150 console.info(data) 151 } 152 } 153 function dl_alert(data){ 154 if(debug_mode){ 155 console.error(data) 156 } 157 } 158 function dl_warn(data){ 159 if(debug_mode){ 160 console.warn(data) 161 } 162 } 163})(jQuery, Drupal); 164/* Source and licensing information for the above line(s) can be found at https://www.cusquena.com/modules/custom/ab_datalayer_pusher/dist/js/main.js. */
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.