Delayed loading Yandex metrics, has anyone tried to install it?

  • 0
    Hello everyone.

    Lazy loading Yandex metrics, has anyone tried to install it?
    Does the metric collect statistics normally? or not.

    Below is 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 fine and the metric will work fine on all devices.

    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 webvisor. Instead of “65509363”, insert your Yandex Metrica counter number.

    Already with 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)
        }
    });
    JavaScript Anonymous, Jan 1, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!