(function () { console.log('[view_item] script start'); const scripts = document.querySelectorAll('script[type="application/ld+json"]'); console.log('[view_item] ld+json scripts found:', scripts.length); if (!scripts.length) return; function isProduct(obj) { if (!obj) return false; const t = obj['@type']; return t === 'Product' || (Array.isArray(t) && t.includes('Product')); } let product = null; for (const s of scripts) { let json; try { json = JSON.parse(s.textContent.trim()); } catch (e) { console.warn('[view_item] JSON parse error', e); continue; } const arr = Array.isArray(json) ? json : [json]; for (const obj of arr) { if (isProduct(obj)) { product = obj; break; } if (obj && Array.isArray(obj['@graph'])) { const p = obj['@graph'].find(isProduct); if (p) { product = p; break; } } } if (product) break; } console.log('[view_item] product found:', product); if (!product) return; const name = product.name || document.title || ''; const sku = product.sku || product.productID || product.mpn || location.pathname; const offersRaw = product.offers || {}; const offer = Array.isArray(offersRaw) ? (offersRaw[0] || {}) : offersRaw; const currency = 'EUR'; const priceStr = (offer.price ?? '0').toString().replace(/,/g, ''); const price = Number(priceStr) || 0; console.log('[view_item] parsed values', { name, sku, price, currency }); window.dataLayer = window.dataLayer || []; window.dataLayer.push({ ecommerce: null }); const eventObj = { event: "view_item", ecommerce: { currency: currency, value: price, items: [{ item_id: String(sku), item_name: String(name), price: price, quantity: 1 }] } }; console.log('[view_item] pushing to dataLayer', eventObj); window.dataLayer.push(eventObj); })();