let lastAddToCart = 0;
document.addEventListener('click', function(e) {
const btn = e.target.closest('button');
if (!btn) return;
const text = (btn.textContent || '').toLowerCase();
if (!text.includes('купити') && !text.includes('в кошик')) return;
const now = Date.now();
if (now - lastAddToCart < 800) return;
lastAddToCart = now;
const name = document.querySelector('h1')?.textContent?.trim();
const priceEl = document.querySelector('[class*="price"]');
let priceText = (priceEl?.textContent || '')
.replace(/[^\d.,]/g, '')
.replace(',', '.');
const price = +(priceText.match(/^\d*\.?\d+/)?.[0] || 0);
const productId = document.querySelector('[data-product-id]')?.dataset.productId || name;
if (!name || price <= 0) return;
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'add_to_cart',
ecommerce: {
currency: 'UAH',
value: price,
items: [{
item_id: productId,
item_name: name,
price,
quantity: 1
}]
}
});
console.log('🔥 ADD_TO_CART OK');
});