(function () { 'use strict'; var KEY = 'shopmilad_torob_stats_v1'; function getStats() { try { return JSON.parse(localStorage.getItem(KEY)) || { visits: 0, cartClicks: 0, products: {} }; } catch (e) { return { visits: 0, cartClicks: 0, products: {} }; } } function saveStats(stats) { try { localStorage.setItem(KEY, JSON.stringify(stats)); } catch (e) {} } /* تشخیص ورود واقعی از ترب */ var ref = document.referrer || ''; var url = window.location.href; var fromTorob = /torob\.com/i.test(ref) || /torob/i.test(url); if (!fromTorob) return; var stats = getStats(); stats.visits++; /* شناسایی محصول فعلی */ var productId = null; var match = window.location.pathname.match(/product\/?([0-9]+)/i); if (match) { productId = match[1]; } var productName = document.title .replace(/\s+/g, ' ') .trim(); if (productId) { if (!stats.products[productId]) { stats.products[productId] = { title: productName, visits: 0, cartClicks: 0 }; } stats.products[productId].visits++; saveStats(stats); } /* تشخیص کلیک واقعی روی دکمههای خرید/سبد */ document.addEventListener('click', function (event) { var el = event.target; if (!el) return; var clickable = el.closest ? el.closest('button, a, input, [role="button"]') : el; if (!clickable) return; var text = ( clickable.innerText || clickable.value || clickable.getAttribute('aria-label') || '' ).toLowerCase(); var isCart = text.indexOf('افزودن به سبد') !== -1 || text.indexOf('افزودن به سبد خرید') !== -1 || text.indexOf('خرید') !== -1 || text.indexOf('سبد') !== -1; if (!isCart) return; var currentStats = getStats(); currentStats.cartClicks++; if (productId && currentStats.products[productId]) { currentStats.products[productId].cartClicks++; } saveStats(currentStats); }, true); })();