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,30 +91,37 @@ 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 counterId = window.__YM_COUNTER_ID__; const isYmAvailable = await waitForYandexMetrika();
if (counterId) {
window.ym(counterId, "hit", url);
// Детальное логирование if (isYmAvailable && typeof window.ym === "function") {
console.groupCollapsed( const counterId = window.__YM_COUNTER_ID__;
`%c[YM] 📊 Page View Event Sent`, if (counterId) {
'color: #FF0000; font-weight: bold' window.ym(counterId, "hit", url);
);
console.log('🕐 Timestamp:', timestamp); // Детальное логирование
console.log('📍 URL:', url); console.groupCollapsed(
console.log('🔢 Counter ID:', counterId); `%c[YM] 📊 Page View Event Sent`,
console.log('✅ Status: Successfully sent to Yandex Metrika'); 'color: #FF0000; font-weight: bold'
console.groupEnd(); );
console.log('🕐 Timestamp:', timestamp);
console.log('📍 URL:', url);
console.log('🔢 Counter ID:', counterId);
console.log('✅ Status: Successfully sent to Yandex Metrika');
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;