Answer the question
In order to leave comments, you need to log in
Delayed loading of Yandex metrics, has anyone tried to set it?
Hello.
Delayed loading of Yandex metrics, has anyone tried to set it?
Does the metric collect statistics normally? or not.
Below I post an example of code that “postpones” the loading of the metric. The site will load quickly, and as soon as the user starts scrolling, after a second timeout, additional resources will start loading. The tests will be in order, and the metric will work properly on all devices.
The code:
var fired = false;
window.addEventListener('scroll', () => {
if (fired === false) {
fired = true;
setTimeout(() => {
// Сюда вставляете метрики без тегов <script>
}, 1000)
}
});
var fired = false;
window.addEventListener('scroll', () => {
if (fired === false) {
fired = true;
setTimeout(() => {
(function(m, e, t, r, i, k, a) {
m[i] = m[i] || function() {
(m[i].a = m[i].a || []).push(arguments)
};
m[i].l = 1 * new Date();
k = e.createElement(t), a = e.getElementsByTagName(t)[0], k.async = 1, k.src = r, a.parentNode.insertBefore(k, a)
})(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
ym(65509363, "init", {
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
webvisor: true
});
}, 1000)
}
});
Answer the question
In order to leave comments, you need to log in
If for WordPress, then there is already a simple plug-in for deferred Metrics "True Lazy Analytics" https://ru.wordpress.org/plugins/true-lazy-analytics/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question