1var GoogleGA4Helper = { 2 enableDebug: false, 3 currency: 'USD', 4 Init: function (cc, debug) { 5 this.currency = cc; 6 this.enableDebug = debug; 7 }, 8 9 _getProductId: function (object) { 10 var productid = $(object).data("productid"); 11 if (productid > 0) { 12 return productid; 13 } 14 var script = $(object).attr("onclick"); 15 if (script && script.length > 0) { 16 //productid = parseInt(script.replace(/[^0-9]/g, '')); 17 var numn = script.match(/\d+/g); 18 if (numn !== null) { 19 productid = numn.map(Number)[0]; 20 if (productid > 0) { 21 return productid; 22 } 23 } 24 } 25 return 0; 26 }, 27 Initialization: function () { 28 GoogleGA4Helper.ReloadEvents(); 29 30 $(document).bind("nopAjaxCartProductAddedToCartEvent", function (e) { 31 GoogleGA4Helper.AddToCart('ga4_add_to_cart', e.productId, e.quantity); 32 e.stopPropagation(); 33 }); 34 $(document).bind("nopAjaxCartProductAddedToWishlistEvent", function (e) { 35 GoogleGA4Helper.AddToCart('ga4_add_to_wishlist', e.productId, e.quantity); 36 e.stopPropagation(); 37 }); 38 }, 39 CheckExistEvent: function (obj) { 40 return $(obj).data('googleanalytics4') == 1; 41 42 /* 43 try { 44 var events = $._data(obj.get(0), 'events').click; 45 if (typeof events === "undefined") { 46 return false; 47 } 48 return true; 49 } 50 catch (err) { 51 return false; 52 }*/ 53 }, 54 ReloadEvents: function () { 55 //productClick 56 $('.product-item[data-productid]').each(function () { 57 var productid = $(this).data("productid"); 58 if (productid > 0) { 59 if (GoogleGA4Helper.CheckExistEvent($(this).find('a'))) { 60 return; 61 } 62 $(this).find('a').data('googleanalytics4', 1); 63 $(this).find('a').click({ productid: productid }, function (event) { 64 event.preventDefault(); 65 var productid = event.data.productid; 66 var url = $(this).attr('href'); 67 //var product = GA4_productImpressions.find(x => x.productId === productid); 68 var result = $.grep(GA4_productImpressions, function (e) { return e.productId == productid; }); 69 if (result.length > 0) { 70 GoogleGA4Helper.ProductClick([result[0]], url); 71 return; 72 } 73 window.location.href = url; 74 }); 75 } 76 }); 77 //addToCart 78 //kava 79 //$('.add-to-cart-button, .product-box-add-to-cart-button').each(function () { 80 // var productid = GoogleGA4Helper._getProductId(this); 81 // if (productid > 0) { 82 // if (GoogleGA4Helper.CheckExistEvent($(this))) { 83 // return; 84 // } 85 // $(this).data('googleanalytics4', 1); 86 // $(this).click({ productid: productid }, function (event) { 87 // event.preventDefault(); 88 // GoogleGA4Helper.AddToCart('ga4_add_to_cart', event.data.productid); 89 // }); 90 // } 91 //}); 92 //endkava 93 //addToWishlist 94 $('.add-to-wishlist-button').each(function () { 95 var productid = GoogleGA4Helper._getProductId(this); 96 if (productid > 0) { 97 if (GoogleGA4Helper.CheckExistEvent($(this))) { 98 return; 99 } 100 $(this).data('googleanalytics4', 1); 101 $(this).click({ productid: productid }, function (event) { 102 event.preventDefault(); 103 GoogleGA4Helper.AddToCart('ga4_add_to_wishlist', event.data.productid); 104 }); 105 } 106 }); 107 //add-to-compare-list-button 108 /*$('.add-to-compare-list-button').each(function () { 109 var productid = GoogleGA4Helper._getProductId(this); 110 if (productid > 0) { 111 if (GoogleGA4Helper.CheckExistEvent($(this))) { 112 return; 113 } 114 $(this).data('googleanalytics4', 1); 115 $(this).click({ productid: productid }, function (event) { 116 event.preventDefault(); 117 GoogleGA4Helper.AddToCart('ga4_addToCompare', event.data.productid); 118 }); 119 } 120 }); 121 */ 122 //remove from the cart 123 $('.update-cart-button').click(function () { 124 $('input[name="removefromcart"]:checked').each(function () { 125 var cartitemid = $(this).val(); 126 if (cartitemid > 0) { 127 GoogleGA4Helper.RemoveFromCart('ga4_remove_from_cart', cartitemid); 128 } 129 }); 130 }); 131 //remove all from the cart 132 $('.clear-cart-button').click(function () { 133 $('input[name="removefromcart"]').each(function () { 134 var cartitemid = $(this).val(); 135 if (cartitemid > 0) { 136 GoogleGA4Helper.RemoveFromCart('ga4_remove_from_cart', cartitemid); 137 } 138 }); 139 }); 140 //remove from the wishlist 141 $('.update-wishlist-button').click(function () { 142 $('input[name="removefromcart"]:checked').each(function () { 143 var cartitemid = $(this).val(); 144 if (cartitemid > 0) { 145 GoogleGA4Helper.RemoveFromCart('ga4_remove_from_wishlist', cartitemid); 146 } 147 }); 148 }); 149 150 //copy from wishlist to cart 151 $('.wishlist-add-to-cart-button').click(function () { 152 $('input[name="addtocart"]:checked').each(function () { 153 var cartitemid = $(this).val(); 154 if (cartitemid > 0) { 155 var products = $.grep(GA4_wishlistproducts, function (e) { return e.cartItemId == cartitemid; }); 156 if (products.length > 0) { 157 var quantity = $('input[name="itemquantity' + cartitemid + '"]').val(); 158 GoogleGA4Helper.AddToCart('ga4_add_to_cart', products[0].productId, quantity); 159 } 160 } 161 }); 162 }); 163 }, 164 ProductClick: function (products, url) { 165 this.WriteConsole({ 'ProductClick': products }); 166 dataLayer.push({ 167 'event': 'ga4_select_item', 168 'ecommerce': { 169 'item_list_name': products[0].list, 170 'items': products 171 }, 172 'eventCallback': function () { 173 document.location = url; 174 }, 175 'eventTimeout': 2000 176 }); 177 /* if customer uses blocker*/ 178 if (typeof google_tag_manager == "undefined") { 179 document.location = url; 180 } 181 }, 182 PurchaseForADblock: function (orderid) { 183 //window.addEventListener('load', function () { 184 //customer blocks Tag Manager 185 if (!window.google_tag_manager) { 186 var img = document.createElement('img'); 187 img.setAttribute('style', 'display:none;'); 188 img.src = '/GoogleAnalytics4/Purchase?orderid='
188+ orderid; 189 document.body.appendChild(img); 190 }; 191 //}, false); 192 }, 193 AddToCart: function (eventname, productid, quantity) { 194 var products = []; 195 if (typeof GA4_productDetails !== 'undefined' && GA4_productDetails !== null) { 196 products = $.grep(GA4_productDetails, function (e) { return e.productId == productid; }); 197 } 198 if (products.length == 0 && typeof GA4_productImpressions !== 'undefined' && GA4_productImpressions !== null) { 199 products = $.grep(GA4_productImpressions, function (e) { return e.productId == productid; }); 200 } 201 if (products.length == 0 && typeof GA4_wishlistproducts !== 'undefined' && GA4_wishlistproducts !== null) { 202 products = $.grep(GA4_wishlistproducts, function (e) { return e.productId == productid; }); 203 } 204 if (products.length > 0) { 205 if (quantity == undefined) 206 quantity = $('#product_enteredQuantity_' + productid).val(); 207 if (quantity == undefined) 208 quantity = 1; 209 if (eventname == undefined) 210 eventname = 'ga4_add_to_cart'; 211 products[0].quantity = quantity; 212 213 var itemamount = (products[0].price * quantity).toFixed(2); 214 this.WriteConsole({ 'AddToCart': [eventname, products[0]] }); 215 216 //kava 217 if (eventname == 'ga4_add_to_cart') 218 dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. 219 //endkava 220 221 dataLayer.push({ 222 'event': eventname, 223 'ecommerce': { 224 'currency': this.currency, 225 'value': itemamount, 226 'items': products 227 } 228 }); 229 } 230 }, 231 RemoveFromCart: function (eventname, cartitemid, quantity) { 232 var products = $.grep(GA4_cartproducts, function (e) { return e.cartItemId == cartitemid; }); 233 if (products.length == 0) { 234 products = $.grep(GA4_wishlistproducts, function (e) { return e.cartItemId == cartitemid; }); 235 } 236 if (products.length > 0) { 237 if (quantity == undefined) 238 quantity = $('input[name="itemquantity' + cartitemid + '"]').val(); 239 if (quantity == undefined) 240 quantity = 1; 241 products[0].quantity = quantity; 242 var itemamount = (products[0].price * quantity).toFixed(2); 243 if (eventname == undefined) 244 eventname = 'ga4_remove_from_cart'; 245 this.WriteConsole({ 'RemoveFromCart': [eventname, products] }); 246 dataLayer.push({ 247 'event': eventname, 248 'ecommerce': { 249 'currency': this.currency, 250 'value': itemamount, 251 'items': products 252 } 253 }); 254 } 255 }, 256 WriteConsole: function (obj) { 257 if (this.enableDebug) { 258 str = JSON.stringify(obj); 259 //str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output. 260 console.log(str); // Logs output to dev tools console. 261 } 262 } 263};
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.