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;
}
/**
* 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
*
@ -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;