fix
This commit is contained in:
parent
3a30f83fe5
commit
bdab5f7f5d
@ -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") {
|
||||||
|
const counterId = window.__YM_COUNTER_ID__;
|
||||||
// Детальное логирование
|
if (counterId) {
|
||||||
console.groupCollapsed(
|
window.ym(counterId, "hit", url);
|
||||||
`%c[YM] 📊 Page View Event Sent`,
|
|
||||||
'color: #FF0000; font-weight: bold'
|
// Детальное логирование
|
||||||
);
|
console.groupCollapsed(
|
||||||
console.log('🕐 Timestamp:', timestamp);
|
`%c[YM] 📊 Page View Event Sent`,
|
||||||
console.log('📍 URL:', url);
|
'color: #FF0000; font-weight: bold'
|
||||||
console.log('🔢 Counter ID:', counterId);
|
);
|
||||||
console.log('✅ Status: Successfully sent to Yandex Metrika');
|
console.log('🕐 Timestamp:', timestamp);
|
||||||
console.groupEnd();
|
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;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user