(() => { // Получаем имя и slug коллекции после #skip-nav function getCollectionNameAndSlug() { const skipNav = document.getElementById('skip-nav'); if (!skipNav) return null; let el = skipNav.nextElementSibling; while (el) { const link = el.querySelector && el.querySelector('a[href^="/collection/"]'); if (link) { const hrefMatch = link.getAttribute('href').match(/\/collection\/([^/]+)/); return { name: link.textContent.trim(), slug: hrefMatch ? hrefMatch[1] : null }; } el = el.nextElementSibling; } return null; } // Получаем slug документа из URL function getDocSlug() { const match = location.pathname.match(/\/doc\/([^/]+)/); return match ? match[1] : null; } let lastDocSlug = null; function sendUmamiDocumentEvent() { if (!window.umami) return; const docSlug = getDocSlug(); const collection = getCollectionNameAndSlug(); if (!docSlug || !collection) return; const { slug: collectionSlug, name: collectionName } = collection; if (docSlug !== lastDocSlug) { lastDocSlug = docSlug; umami.track('event', { event_name: 'document-view', event_data: { collectionSlug, collectionName } }); console.log('Umami event sent:', docSlug, collectionSlug, collectionName); } } function onUrlChange() { setTimeout(sendUmamiDocumentEvent, 200); // задержка, чтобы DOM успел обновиться } function waitForUmamiAndTrack(tries = 30) { if (window.umami) { sendUmamiDocumentEvent(); const origPushState = history.pushState; const origReplaceState = history.replaceState; history.pushState = function() { origPushState.apply(this, arguments); onUrlChange(); }; history.replaceState = function() { origReplaceState.apply(this, arguments); onUrlChange(); }; window.addEventListener('popstate', onUrlChange); } else if (tries > 0) { setTimeout(() => waitForUmamiAndTrack(tries - 1), 300); } else { console.warn('Umami не загрузился!'); } } waitForUmamiAndTrack(); })();