This commit is contained in:
dev.daminik00 2025-10-30 13:53:19 +01:00
parent 3a30f83fe5
commit bdab5f7f5d

View File

@ -17,6 +17,22 @@ async function waitForGtag(maxAttempts = 10, delayMs = 100): Promise<boolean> {
return false; 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<boolean> {
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 * Page View Tracker Component
* *
@ -75,8 +91,11 @@ export function PageViewTracker() {
// Execute GA tracking // Execute GA tracking
trackGoogleAnalytics(); trackGoogleAnalytics();
// Track page view in Yandex Metrika // Track page view in Yandex Metrika (with retry logic)
if (typeof window !== "undefined" && typeof window.ym === "function") { const trackYandexMetrika = async () => {
const isYmAvailable = await waitForYandexMetrika();
if (isYmAvailable && typeof window.ym === "function") {
const counterId = window.__YM_COUNTER_ID__; const counterId = window.__YM_COUNTER_ID__;
if (counterId) { if (counterId) {
window.ym(counterId, "hit", url); window.ym(counterId, "hit", url);
@ -91,14 +110,18 @@ export function PageViewTracker() {
console.log('🔢 Counter ID:', counterId); console.log('🔢 Counter ID:', counterId);
console.log('✅ Status: Successfully sent to Yandex Metrika'); console.log('✅ Status: Successfully sent to Yandex Metrika');
console.groupEnd(); console.groupEnd();
}
} else { } else {
console.warn( console.warn(
`%c[YM] ⚠️ Page View NOT Sent`, `%c[YM] ⚠️ Page View NOT Sent`,
'color: #FFA500; font-weight: bold', '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]); }, [pathname, searchParams]);
return null; return null;