google-tag-managergoogle-ads-script

Google ads scripts are decreasing Google page speed index score


Is there a solution to load Google scripts after the page load, so the google page speed index didn't get affected?


Solution

  • this code put a timeout on the heavy javascript codes and if the user scroll it will also run so the content will appears faster

    var scrollCounter = false;
    var pageType = typeof signal !== 'undefined' && signal.Content.ArticleId!=null?'article':'notanarticle';
    var cds = /Mobi/.test(navigator.userAgent) && document.body.clientWidth < 737 ? 'Mobile' : /Mobi/.test(navigator.userAgent) && document.body.clientWidth > 767 ? 'Tablet' : 'Desktop';
    if (cds == 'Mobile') {
        //condition for home and section pages
          if(pageType == 'notanarticle') {
           setTimeout(function() {
                if (!scrollCounter) {
                    scrollCounter = true;
                    console.log('timeout');
                    addThirdPartyLibs();
                }
            }, 4000);   
         }//end of condition
        var activityEvents = [
            'keydown', 'touchstart', 'scroll'
        ];
        activityEvents.forEach(function(eventName) {
            document.addEventListener(eventName, function(e) {
                if (!scrollCounter) {
                    scrollCounter = true;
                    console.log(window.scrollY);
                    addThirdPartyLibs();
                }
            }, true);
        });
    } else {
        addThirdPartyLibs();
    }
    
    function addThirdPartyLibs() {
        loadGPTJs();
    
    }