/* --- NapleStars: dataLayer product + add_to_cart + variant_change --- */ window.dataLayer = window.dataLayer || []; (function(){ function q(sel){ return document.querySelector(sel); } function qAll(sel){ return Array.prototype.slice.call(document.querySelectorAll(sel)); } function text(sel){ var el=q(sel); return el ? el.innerText.trim() : ""; } function attr(el, a){ return el ? el.getAttribute(a) || "" : ""; } function normalize(v){ return v ? v.replace(/\s+/g," ").trim() : ""; } function getProductData(){ var name = normalize(text(".product-title") || text("h1") || text(".title-campaign")); // size selected var size = normalize((q(".size-link.active") && q(".size-link.active").innerText) || (q(".size-box.active") && q(".size-box.active").innerText) || ""); // color selected: try data-color or title or alt var colorEl = q(".circle.active") || q(".color.active") || q(".swatch.active") ; var color = colorEl ? (attr(colorEl,"data-color")||attr(colorEl,"title")||normalize(colorEl.innerText)) : ""; // price var price = normalize((text(".special-price") || text("#new-price") || text(".price") || "").replace("€","").replace("EUR","")); // variant id fallback var variant = (attr(q("input[name='variant_id']"),"value") || variant || ""); var variantId = variant || (name + "-" + color + "-" + size); variantId = variantId.replace(/\s+/g,"-").toLowerCase(); return { item_name: name, item_variant: variantId, item_size: size, item_color: color, price: price }; } // push view_item on product page load (useful) try{ var pd = getProductData(); if(pd.item_name) { dataLayer.push({ event: "view_item", ecommerce: { items: [ { item_name: pd.item_name, item_id: pd.item_variant, item_variant: pd.item_variant, price: pd.price, item_size: pd.item_size, item_color: pd.item_color, quantity: 1 } ] } }); } }catch(e){ console && console.error && console.error("DL view_item err",e); } // Listen size/color changes (push variant_change) qAll(".size-link, .size-box, .swatch, .color, .circle").forEach(function(el){ el.addEventListener("click", function(){ setTimeout(function(){ // slight delay to allow DOM class changes var pd = getProductData(); dataLayer.push({ event: "variant_change", item_name: pd.item_name, item_variant: pd.item_variant, item_size: pd.item_size, item_color: pd.item_color, price: pd.price }); }, 250); }, false); }); // Add to cart button (use button[type=submit] in modal or specific class) var addSelectors = ["button.btn-shop","button.btn-add-to-cart","button[type='submit']","button:contains('Aggiungi al Carrello')"]; // safe: attach to form submit as fallback var cartButtons = qAll("button.btn-shop, button.btn-add-to-cart, button[type='submit'], .add-to-cart, .btn-add"); cartButtons.forEach(function(b){ b.addEventListener("click", function(e){ try{ var pd = getProductData(); dataLayer.push({ event: "add_to_cart", ecommerce: { currency: "EUR", value: pd.price, items: [{ item_name: pd.item_name, item_id: pd.item_variant, item_variant: pd.item_variant, price: pd.price, item_size: pd.item_size, item_color: pd.item_color, quantity: 1 }] } }); }catch(err){ console && console.error && console.error("DL add_to_cart err",err); } }, false); }); // If store shows a "Thank you" or order confirmation page with /order or /checkout/complete if(location.pathname.indexOf("order")>=0 || location.pathname.indexOf("checkout")>=0 || location.pathname.indexOf("thank-you")>=0){ try{ // extract order details (attempt) var orderTotal = (text(".order-total") || text(".total-amount") || "").replace("€",""); dataLayer.push({ event: "purchase", ecommerce: { transaction_id: "T-"+Date.now(), value: orderTotal || "", currency: "EUR" } }); }catch(e){} } })(); /* end */