A
A
Anton2020-10-31 15:23:47
JavaScript
Anton, 2020-10-31 15:23:47

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)
    }
});


An example of inserting a code with Yandex Metrica and an enabled web browser. Instead of “65509363”, insert your Yandex Metrica counter number.

Already with the metric:
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

1 answer(s)
J
jumandjiman, 2020-12-31
@jumandjiman

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 question

Ask a Question

731 491 924 answers to any question