2

So on all my web pages i have this code what is used by Yandex metrika for analytics. According to google this script is slowing down my pages and needs something changing in the way it loads to not be render blocking, TTI time to input blocking, FID first input delay blocking, First contentful paint blocking.

<script data-cfasync="false" type="text/javascript">
   (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(XXXXXXXX, "init", {
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true,
        webvisor:true
   });
</script>

Googles page speed reports the following https://developers.google.com/speed/pagespeed/insights/

Yandex metrika script tag slow optimization main thread blocking reduce the impact of third party code

Yandex metrika main tag slow reduce javascript execution time script evaluation cpu time

What can i modify the Yandex metrika javascript to in order to fix this issue ?

C0nw0nk
  • 870
  • 2
  • 13
  • 29
  • 1
    Use something else, 5 second script evaluation? it must be bloated as hell / poorly written. One option to improve your score would be to wrap the calling function you show in a `setTimeout` to defer the loading until after everything important but personally I would just use another tag manager or do without with those sorts of execution times. – GrahamTheDev Dec 22 '19 at 11:30
  • @GrahamRitchie Can you please post your modification to the Javascript i provided above as an answer and then if it works i can mark it as the answer :) ? – C0nw0nk Dec 22 '19 at 13:29

4 Answers4

6

You don't have many options I am afraid as it is a third party script.

One option to improve your score and perceived load times is to wrap the call to the function in a setTimeout set long enough to delay loading the script until the essential content is loaded.

<script data-cfasync="false" type="text/javascript">
  setTimeout(function(){
       (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(XXXXXXXX, "init", {
            clickmap:true,
            trackLinks:true,
            accurateTrackBounce:true,
            webvisor:true
       });
    }, 5000); //set this as high as you can without ruining your stats.
</script>

This is definitely a workaround and I would instead advise using a different library that is less bloated if you can find one.

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • Hey thanks for the code upvoted :) last night i stumbled across the following that i posted as an answer too it did help improve the load time not sure if it gives you any ideas of other ways to improve the loading time. – C0nw0nk Dec 23 '19 at 16:21
1
<script data-cfasync="false" type="text/javascript">
(function(){
var a = function() {try{return !!window.addEventListener} catch(e) {return !1} },
b = function(b, c) {a() ? document.addEventListener("load", b, c) : document.attachEvent("onreadystatechange", b)};
b(function(){

   (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(XXXXXXXX, "init", {
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true,
        webvisor:true
   });

}, false);
})();
</script>
C0nw0nk
  • 870
  • 2
  • 13
  • 29
  • 2
    unfortunately this code have an error it calls `document.addEventListener` but should `window.addEventListener` and after fixing this issue it starts making same behavior as an original yandex metrica :( – Roman Vasilyev Oct 16 '20 at 20:15
  • Sorry for the late response on this, I never saw your comment on my answer @C0nw0nk. All this does is add the script to the page on document load, if you get any performance gains from this it means your site needs further optimisations earlier in the critical path as document load should take less than a second on a properly optimised site. Unfortunately a `setTimeout` or a different library are still the best options, unless you really want to get fancy with `performanceObserver`. – GrahamTheDev Dec 17 '20 at 10:39
0

One option is to use old Yandex Metrika code (new Ya.Metrika...). You can view the old code if you set checkbox in advanced settings of the Metrika. Old code has some limitation but it's much smaller and few times faster.

valodzka
  • 5,535
  • 4
  • 39
  • 50
0

Disabling clickmap and webvisor could help.

jiv-e
  • 483
  • 6
  • 8