(function () { "use strict"; var script = document.currentScript; var siteId = script && script.getAttribute("data-site-id"); if (!siteId) return; var endpoint = "https://ya-stats.ru/api/pixel/event"; var visitorKey = "ys_pixel_visitor_" + siteId; var sessionKey = "ys_pixel_session_" + siteId; var sessionTimeout = 30 * 60 * 1000; var lastPage = ""; function id(prefix) { if (window.crypto && typeof window.crypto.randomUUID === "function") return window.crypto.randomUUID(); return prefix + Math.random().toString(36).slice(2) + Date.now().toString(36); } function storageGet(storage, key) { try { return storage.getItem(key); } catch (_) { return null; } } function storageSet(storage, key, value) { try { storage.setItem(key, value); } catch (_) {} } var visitorId = storageGet(window.localStorage, visitorKey) || id("v_"); storageSet(window.localStorage, visitorKey, visitorId); var now = Date.now(); var savedSession = null; try { savedSession = JSON.parse(storageGet(window.sessionStorage, sessionKey) || "null"); } catch (_) {} var newSession = !savedSession || !savedSession.id || now - Number(savedSession.last || 0) > sessionTimeout; var sessionId = newSession ? id("s_") : savedSession.id; var sessionStartedAt = newSession ? now : Number(savedSession.started || now); storageSet(window.sessionStorage, sessionKey, JSON.stringify({ id: sessionId, last: now, started: sessionStartedAt })); function queryValue(name) { return new URLSearchParams(window.location.search).get(name) || ""; } function queryTerm() { return queryValue("utm_term") || queryValue("keyword") || queryValue("text") || queryValue("q"); } function deviceType() { var width = window.innerWidth || 1024; return width < 768 ? "mobile" : width < 1100 ? "tablet" : "desktop"; } function browserName() { var ua = navigator.userAgent || ""; if (/Edg\//.test(ua)) return "Edge"; if (/YaBrowser/.test(ua)) return "Yandex"; if (/Chrome\//.test(ua)) return "Chrome"; if (/Firefox\//.test(ua)) return "Firefox"; if (/Safari\//.test(ua)) return "Safari"; return "Other"; } function osName() { var ua = navigator.userAgent || ""; if (/Android/.test(ua)) return "Android"; if (/iPhone|iPad|iPod/.test(ua)) return "iOS"; if (/Windows/.test(ua)) return "Windows"; if (/Mac OS/.test(ua)) return "macOS"; if (/Linux/.test(ua)) return "Linux"; return "Other"; } function send(eventName, metadata) { if (!eventName || eventName.length > 80) return; var url = window.location.href.slice(0, 2000); var payload = { event_id: id("e_"), site_id: siteId, visitor_id: visitorId, session_id: sessionId, event_name: String(eventName).slice(0, 80), page_url: url, pathname: window.location.pathname, referrer: document.referrer || "", utm_source: queryValue("utm_source"), utm_medium: queryValue("utm_medium"), utm_campaign: queryValue("utm_campaign"), utm_content: queryValue("utm_content"), utm_term: queryTerm(), device_type: deviceType(), browser: browserName(), os: osName(), metadata: metadata || {} }; storageSet(window.sessionStorage, sessionKey, JSON.stringify({ id: sessionId, last: Date.now(), started: sessionStartedAt })); var serialized = JSON.stringify(payload); try { if (navigator.sendBeacon) { // text/plain keeps the beacon a simple cross-origin request and avoids a CORS preflight. var blob = new Blob([serialized], { type: "text/plain;charset=UTF-8" }); if (navigator.sendBeacon(endpoint, blob)) return; } } catch (_) {} try { fetch(endpoint, { method: "POST", headers: { "Content-Type": "text/plain;charset=UTF-8" }, body: serialized, keepalive: true }); } catch (_) {} } function pageView() { var current = window.location.href; if (current === lastPage) return; lastPage = current; send("page_view"); } if (newSession) send("session_start"); send("page_view"); lastPage = window.location.href; function sendHeartbeat() { send("session_heartbeat", { duration_seconds: Math.max(0, Math.floor((Date.now() - sessionStartedAt) / 1000)) }); } window.setInterval(function () { if (document.visibilityState !== "hidden") sendHeartbeat(); }, 15000); document.addEventListener("visibilitychange", function () { if (document.visibilityState === "hidden") sendHeartbeat(); }); document.addEventListener("click", function (event) { var target = event.target && event.target.closest ? event.target.closest("a,button,[role='button']") : null; if (!target) return; var href = target.getAttribute("href") || ""; if (/^tel:/i.test(href)) send("phone_click"); else if (/^mailto:/i.test(href)) send("email_click"); else if (/(wa\.me|api\.whatsapp\.com)/i.test(href)) send("whatsapp_click"); else if (/(t\.me|telegram\.me)/i.test(href)) send("telegram_click"); else if (target.tagName.toLowerCase() === "button") send("button_click", { label: (target.textContent || "").trim().slice(0, 120) }); else if (href && /^https?:/i.test(href) && new URL(href, window.location.href).origin !== window.location.origin) send("external_link_click"); }, { passive: true }); document.addEventListener("submit", function () { send("form_submit"); }, { passive: true }); ["pushState", "replaceState"].forEach(function (method) { var original = window.history[method]; if (!original) return; window.history[method] = function () { var result = original.apply(this, arguments); window.setTimeout(pageView, 0); return result; }; }); window.addEventListener("popstate", pageView); window.yaStatsPixel = window.yaStatsPixel || {}; window.yaStatsPixel.track = send; })();