From bdab5f7f5d608696696bf5dea564d246f58e8dac Mon Sep 17 00:00:00 2001 From: "dev.daminik00" Date: Thu, 30 Oct 2025 13:53:19 +0100 Subject: [PATCH] fix --- src/components/analytics/PageViewTracker.tsx | 59 ++++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/components/analytics/PageViewTracker.tsx b/src/components/analytics/PageViewTracker.tsx index e691ba0..461476b 100644 --- a/src/components/analytics/PageViewTracker.tsx +++ b/src/components/analytics/PageViewTracker.tsx @@ -17,6 +17,22 @@ async function waitForGtag(maxAttempts = 10, delayMs = 100): Promise { return false; } +/** + * Wait for Yandex Metrika to be loaded + * Retry mechanism with timeout to handle async script loading + */ +async function waitForYandexMetrika(maxAttempts = 10, delayMs = 100): Promise { + for (let i = 0; i < maxAttempts; i++) { + if (typeof window !== "undefined" && + typeof window.ym === "function" && + window.__YM_COUNTER_ID__) { + return true; + } + await new Promise(resolve => setTimeout(resolve, delayMs)); + } + return false; +} + /** * Page View Tracker Component * @@ -75,30 +91,37 @@ export function PageViewTracker() { // Execute GA tracking trackGoogleAnalytics(); - // Track page view in Yandex Metrika - if (typeof window !== "undefined" && typeof window.ym === "function") { - const counterId = window.__YM_COUNTER_ID__; - if (counterId) { - window.ym(counterId, "hit", url); - - // Детальное логирование - console.groupCollapsed( - `%c[YM] 📊 Page View Event Sent`, - 'color: #FF0000; font-weight: bold' - ); - console.log('🕐 Timestamp:', timestamp); - console.log('📍 URL:', url); - console.log('🔢 Counter ID:', counterId); - console.log('✅ Status: Successfully sent to Yandex Metrika'); - console.groupEnd(); + // Track page view in Yandex Metrika (with retry logic) + const trackYandexMetrika = async () => { + const isYmAvailable = await waitForYandexMetrika(); + + if (isYmAvailable && typeof window.ym === "function") { + const counterId = window.__YM_COUNTER_ID__; + if (counterId) { + window.ym(counterId, "hit", url); + + // Детальное логирование + console.groupCollapsed( + `%c[YM] 📊 Page View Event Sent`, + 'color: #FF0000; font-weight: bold' + ); + console.log('🕐 Timestamp:', timestamp); + console.log('📍 URL:', url); + console.log('🔢 Counter ID:', counterId); + console.log('✅ Status: Successfully sent to Yandex Metrika'); + console.groupEnd(); + } } else { console.warn( `%c[YM] ⚠️ Page View NOT Sent`, 'color: #FFA500; font-weight: bold', - '\nReason: Counter ID not found' + '\nReason: Yandex Metrika not available after 1 second timeout' ); } - } + }; + + // Execute YM tracking + trackYandexMetrika(); }, [pathname, searchParams]); return null;