1document.addEventListener("DOMContentLoaded", function () { 2 let debounceTimer; 3 4 function pushToDataLayer(eventName, data) { 5 clearTimeout(debounceTimer); 6 debounceTimer = setTimeout(() => { 7 window.dataLayer = window.dataLayer || []; 8 window.dataLayer.push({ event: eventName, ...data }); 9 10 console.log("DataLayer Event:", eventName, data); // Debugging 11 }, 300); // 300ms debounce to avoid spam 12 } 13 14 // Track product list views on category/shop pages 15 const itemListName = document.querySelector(".elementor h2")?.textContent.trim() || "Unknown List"; 16 17 if (window.location.pathname.includes("/product-category/") || window.location.pathname.includes("/shop/")) { 18 const itemList = []; 19 const processedItems = new Set(); // To prevent duplicate products 20 21 document.querySelectorAll(".product").forEach((product) => { 22 const productName = product.querySelector(".woocommerce-loop-product__title")?.textContent.trim() || "Unknown Product"; 23 const productPriceText = product.querySelector(".woocommerce-Price-amount")?.textContent.trim() || "0"; 24 const productPrice = parseFloat(productPriceText.replace(/[^\d.]/g, "")) || 0; 25 const productId = product.querySelector(".add_to_cart_button")?.getAttribute("data-product_id") || ""; 26 27 if (!processedItems.has(productId)) { 28 itemList.push({ 29 item_id: productId, 30 item_name: productName, 31 price: productPrice 32 }); 33 processedItems.add(productId); 34 } 35 }); 36 37 // Push product list data 38 pushToDataLayer("view_item_list", { 39 ecommerce: { 40 item_list_name: itemListName, 41 items: itemList 42 } 43 }); 44 } 45 46 // Track product variation selections on single product pages 47 function trackVariationSelection(attributeName, value) { 48 const itemName = document.querySelector(".elementor-widget-woocommerce-product-title h1")?.innerText.trim() || "Unknown Product"; 49 const itemId = document.querySelector("input[name='product_id']")?.value || "Unknown ID"; 50 const price = parseFloat( 51 document.querySelector(".elementor-widget-woocommerce-product-price p span")?.innerText.replace(/[^\d.]/g, "") 52 ) || 0.0; 53 54 pushToDataLayer("select_item", { 55 ecommerce: { 56 items: [ 57 { 58 item_id: itemId, 59 item_name: itemName, 60 currency: "INR", 61 price: price, 62 variation: { 63 attribute: attributeName, 64 value: value 65 } 66 } 67 ] 68 } 69 }); 70 } 71 72 // Dropdown change event 73 document.querySelectorAll(".variations select").forEach(function (select) { 74 select.addEventListener("change", function () { 75 trackVariationSelection(this.getAttribute("name"), this.value); 76 }); 77 }); 78 79 // Radio button change event 80 document.querySelectorAll(".variations input[type='radio']").forEach(function (radio) { 81 radio.addEventListener("change", function () { 82 trackVariationSelection(this.getAttribute("name"), this.value); 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.