1$(function () { 2 // **** DOM Variables that may need to be updated *** 3 // DOM element for product grid list item 4 var product_grid_item = $('.product-container'), 5 // DOM element for recently viewed item 6 recent_product_item = $('.recent_product'), 7 // DOM element for shopping cart rows 8 shopping_cart_row = $('form[name="store_update"] > .list_container tr'), 9 // DOM element for checkout product rows 10 checkout_item_row = $('.main-content .list_container:nth-child(7) tr'), 11 // DOM element for purchase product rows 12 purchase_item_row = $('.main-content .list_container:nth-child(5) tr'), 13 // DOM element for order number on purchase confirmation screen 14 order_number = $('#pageContent .row:nth-child(6) .col-md-6:last-child tr:first-child td:last-child'), 15 // DOM element for address on purchase confirmation screen - to get zip code value 16 order_address = $(".main-content section > table:nth-child(3) > tbody > tr:first-child td:first-child"); 17 18 // Current URL used for item_list_id of product grid list 19 var current_url = window.location.href; 20 21 console.log('fuuuuuuuuuuuunctioning'); 22 console.log(current_url); 23 24 25 // View cart 26 if (window.location.href.indexOf("viewcart") != -1) { 27 var products = [], 28 prices = []; 29 30 shopping_cart_row.each(function () { 31 if ($(this).find('td:first-child').text() !== 'Delete') { 32 var product_name = $(this).find("td:nth-child(3)").text(), 33 product_name = product_name.replaceAll(`"`, ``), 34 product_price = $(this).find("td:nth-child(6)").text(), 35 product_price = product_price.slice(2), 36 product_price = product_price.replace(/\s+/g, ''), 37 number_price = Number(product_price), 38 product_sku = $(this).find("td:nth-child(2)").text(), 39 product_qty = $(this).find("td:nth-child(5) input").val(), 40 product_data = { 41 item_name: product_name, 42 item_id: product_sku, 43 currency: "USD", 44 price: product_price, 45 quantity: product_qty 46 }; 47 prices.push(number_price); 48 products.push(product_data); 49 50 } 51 }); 52 53 var cart_total = prices.reduce(add,0), 54 cart_total = cart_total.toFixed(2); 55 56 function add(accumulator, a) { 57 return accumulator + a; 58 } 59 60 window.dataLayer.push({ ecommerce: null }); 61 window.dataLayer.push({ 62 event: "view_cart", 63 ecommerce: { 64 currency: "USD", 65 value: cart_total, 66 items: products 67 } 68 }); 69 70 71 72 73 } 74 75 // begin_checkout 76 $('.cart_checkout_button').on('click',function(){ 77 console.log('click checkout'); 78 var products = [], 79 prices = []; 80 81 shopping_cart_row.each(function () { 82 if ($(this).find('td:first-child').text() !== 'Delete') { 83 var product_name = $(this).find("td:nth-child(3)").text(), 84 product_name = product_name.replaceAll(`"`, ``), 85 product_price = $(this).find("td:nth-child(6)").text(), 86 product_price = product_price.slice(2), 87 product_price = product_price.replace(/\s+/g, ''), 88 number_price = Number(product_price), 89 product_sku = $(this).find("td:nth-child(2)").text(), 90 product_qty = $(this).find("td:nth-child(5) input").val(), 91 product_data = { 92 item_name: product_name, 93 item_id: product_sku, 94 currency: "USD", 95 price: product_price, 96 quantity: product_qty 97 }; 98 prices.push(number_price); 99 products.push(product_data); 100 101 } 102 }); 103 104 var cart_total = prices.reduce(add,0), 105 cart_total = cart_total.toFixed(2); 106 107 function add(accumulator, a) { 108 return accumulator + a; 109 } 110 111window.dataLayer.push({ ecommerce: null }); 112window.dataLayer.push({ 113 event: "begin_checkout", 114 ecommerce: { 115 currency: "USD", 116 value: cart_total, 117 items: products 118 } 119 }); 120}); 121 122 // Only run this if the product grid is being displayed 123 if(product_grid_item.length){ 124 // Variable set to hold all items 125 var items = [], 126 // Get categories from the breadcrumbs 127 item_category = $('.breadcrumb li:first-child').text(), 128 item_category = item_category.slice(1,-1), 129 item_category2 = $('.breadcrumb li:nth-child(2)').text(), 130 item_category2 = item_category2.slice(1,-1), 131 item_category3 = $('.breadcrumb li:last-child').text(); 132 // Cycle through all the products for each item's data 133 product_grid_item.each(function(){ 134 var item_name = $(this).find('.product_link').text(), 135 item_name = item_name.replaceAll(`"`, ``), 136 item_sku = $(this).find('.product_sku').text(), 137 item_sku = item_sku.slice(7), 138 item_price = $(this).find('.product_price').text(), 139 item_price = item_price.replace(/\$/g, '').replace(/\s/g,'').replace('Startingfrom',''), 140 number_price = Number(item_price), 141 item = { 142 item_id: item_sku, 143 item_name: item_name, 144 item_category: item_category3, 145 item_category2: item_category2, 146 item_list_name: item_category3, 147 item_list_id: current_url, 148 price: number_price, 149 quantity: 1 150 }; 151 // Send item data to the 'items' array 152 items.push(item); 153 154 //*** select_item Event when item is clicked into 155 $(this).find('a').on('click',function(){ 156 window.dataLayer.push({ ecommerce: null }); 157 window.dataLayer.push({ 158 event: "select_item", 159 ecommerce: { 160 item_list_name: item_category3, 161 item_list_id: current_url, 162 items:[ 163 { 164 item_id: item_sku, 165 item_name: item_name, 166 item_category: item_category3, 167 item_category2: item_category2, 168 item_list_name: item_category3, 169 item_list_id: current_url, 170 price: number_price, 171 quantity: 1 172 } 173 ], 174 } 175 }); 176 177 }) 178 179 //*** add_to_cart Event when item is added to cart from product list 180 $(this).find('.add_to_cart_button').on('click',function(){ 181 window.dataLayer.push({ ecommerce: null }); 182 window.dataLayer.push({ 183 event: "add_to_cart", 184 ecommerce: { 185 currency: "USD", 186 value: number_price, 187 items:[ 188 { 189 item_id: item_sku, 190 item_name: item_name, 191 item_category: item_category3, 192 item_category2: item_category2, 193 item_list_name: item_category3, 194 item_list_id: current_url, 195 currency: "USD", 196 price: number_price, 197 quantity: 1 198 } 199 ], 200 } 201 }); 202 203 // After item is added to cart, check to see if it was a duplicate 204 // This function waits for an element to be added to the DOM 205 function waitForElm(selector) { 206 return new Promise(resolve => { 207 if (document.querySelector(selector)) { 208 return resolve(document.querySelector(selector)); 209 } 210 211 const observer = new MutationObserver(mutations => { 212 if (document.querySelector(selector)) { 213 resolve(document.querySelector(selector)); 214 observer.disconnect(); 215 } 216 }); 217 218 observer.observe(document.body, { 219 childList: true, 220 subtree: true 221 }); 222 }); 223 } 224 225 // Use function to check for the add to cart popup 226 waitForElm('.answers').then((elm) => { 227 $('.answers').on('click', function(){ 228 var popupText = $('.continue_to_shop').text(); 229 // If item is a duplicate or out of stock, run the remove from cart event 230 if(popupText == 'Unable to add duplicate items to your cart.' || popupText == 'This product is out of stock.'){ 231 window.dataLayer.push({ ecommerce: null }); 232 window.dataLayer.push({ 233 event: "remove_from_cart", 234 ecommerce: { 235 currency: "USD", 236 value: number_price, 237 items:[ 238 { 239 item_id: item_sku, 240 item_name: item_name, 241 item_category: item_category3, 242 item_category2: item_category2, 243 item_list_name: item_category3, 244 item_list_id: current_url, 245 price: number_price, 246 quantity: 1 247 } 248 ], 249 } 250 }); 251 } 252 }); 253 }); 254 255 256 257 }); 258 }); 259 260 // Uncomment this if you'd like to see items printed to the console 261 // console.log(items); 262 263 // *** view_item_list Event on product list *** 264 window.dataLayer.push({ ecommerce: null }); 265 window.dataLayer.push({ 266 event: "view_item_list", 267 ecommerce: { 268 item_list_name: item_category3, 269 item_list_id: current_url, 270 items: items, 271 } 272 }); 273 274 } 275 276 // Only run this if Recently Viewed products are being displayed 277 if(recent_product_item.length){ 278 var recent_items = []; 279 recent_product_item.each(function(){ 280 var item_name = $(this).find('p a').text().replaceAll(`"`,``), 281 item_price = $(this).find('.product_price').text(), 282 item_price = item_price.replace(/\$/g, '').replace(/\s/g,''), 283 item = { 284 item_name: item_name, 285 price: item_price, 286 currency: "USD", 287 item_list_name: "Recently Viewed", 288 item_list_id: current_url, 289 quantity: 1 290 }; 291 // Send item data to the 'items' array 292 recent_items.push(item); 293 294 //*** select_item Event when Recently Viewed item is clicked into 295 $(this).find('a').on('click',function(){ 296 window.dataLayer.push({ ecommerce: null }); 297 window.dataLayer.push({ 298 event: "select_item", 299 ecommerce: { 300 item_list_name: "Recently Viewed", 301 item_list_id: current_url, 302 items:[ 303 { 304 item_name: item_name, 305 price: item_price, 306 currency: "USD", 307 item_list_name: "Recently Viewed", 308 item_list_id: current_url, 309 quantity: 1 310 } 311 ], 312 } 313 }); 314 }); 315 }); 316 // *** view_item_list Event for Recently Viewed list *** 317 window.dataLayer.push({ ecommerce: null }); 318 window.dataLayer.push({ 319 event: "view_item_list", 320 ecommerce: { 321 item_list_name: "Recently Viewed", 322 item_list_id: current_url, 323 items: recent_items, 324 } 325 }); 326 } 327 328 329 330 331// // Receipt of Purchase 332 if (window.location.href.indexOf("mode=receipt") != -1 || window.location.href.indexOf("test-order") != -1) { 333 334 var products = [], 335 //order = order_number.text(); 336 order = $(".main-content section > table:nth-child(3) > tbody > tr:nth-child(3) > td:first-child tr:first-child td:last-child").text(); 337 338 purchase_item_row.each(function () { 339 if ($(this).find('td:nth-child(2)').text() !== 'Description') { 340 var product_name = $(this).find("td:nth-child(2)").text(), 341 product_name = product_name.replace(`"`,''), 342 product_price = $(this).find("td:nth-child(4)").text(), 343 product_price = product_price.slice(2), 344 product_price = product_price.replace(/\s+/g, ''), 345 product_sku = $(this).find("td:first-child").text(), 346 product_qty = $(this).find("td:nth-child(3)").text(), 347 product_data = { 348 item_name: product_name, 349 item_id: product_sku, 350 currency: "USD", 351 price: product_price, 352 quantity: product_qty 353 }; 354 products.push(product_data); 355 } 356 }); 357 358 // Get price data from DOM (TOTAL PRICE) 359 var price = $(".cart_totals tr:last-child td:last-child").text(), 360 price = price.replace(/\s+/g, ''), 361 price = price.replace('$', ''); 362 363 // Get zip code data from DOM 364 var address = order_address.text(), 365 zip = address.slice(address.length - 5); 366 367 window.dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object. 368 window.dataLayer.push({ 369 event: "purchase", 370 ecommerce: { 371 transaction_id: order, 372 value: price, 373 //tax: 'NA', 374 //shipping: 'NA', 375 currency: "USD", 376 //coupon: "NA", 377 items: products 378 } 379 }); 380 381 382 var tracking_pixel = '<img height="1" width="1" style="border-style:none;" alt="" src="https://insight.adsrvr.org/track/pxl/?adv=bal5gqu&ct=0:uyv1t9h&fmt=3&v=' + price + '&orderid='
382+ order + '&td1=' + zip + '"/>'; 383 384 jQuery('body').append(tracking_pixel); 385 386 } 387 388});
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.