{
let isYMLoaded = false;
const loadYM = () => {
if (isYMLoaded) return;
isYMLoaded = true;
setTimeout(() => {
// Metrika code
// ...
}, 0);
}
const eventNames = 'click,scroll,touchstart,mouseenter'.split(',');
const handlerYM = () => {
eventNames.forEach((name) => document.removeEventListener(name, handlerYM));
loadYM();
}
if (!!~navigator.userAgent.indexOf('YandexMetrika')) {
// Yandex
loadYM();
} else {
// нормальный посетитель
eventNames.forEach((name) => document.addEventListener(name, handlerYM));
}
}