PageSpeed Insights 99/100 because of Google Analytics - How can I cache GA?

前端 未结 20 1750
無奈伤痛
無奈伤痛 2020-11-29 15:10

I\'m on a quest to reach 100/100 on PageSpeed and i\'m almost there. I\'m trying to find a good solution to cache Google Analytics.

Here is the message I get:

20条回答
  •  孤独总比滥情好
    2020-11-29 15:24

    varvy.com (100/100 Google page speed insight) loads google analitycs code only if user make a scroll of the page:

    var fired = false;
    
    window.addEventListener("scroll", function(){
        if ((document.documentElement.scrollTop != 0 && fired === false) || (document.body.scrollTop != 0 && fired === false)) {
    
            (function(i,s,o,g,r,a,m{i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
            ga('create', 'UA-XXXXXXXX-X', 'auto');
            ga('send', 'pageview');
    
            fired = true;
        }
    }, true);
    

提交回复
热议问题