Google Adsense and Google Analytics make my lighthouse score very low, is there any solution for it?

Viewed 727

I have a web page with both Adsense and Analytics on it.

Just used the normal way to add the related JavaScript code.

However, the Lighthouse score is very low. And I found a significant reason is due to the integration of Google Analytics and Google Adsense.

Is there any solution/suggestion for this?

1 Answers

Try to lazy-loading your script. There are two different ways I provide to you to do this:

Using setTimeout()

function KJ_AsOT(){var a=document.getElementsByTagName("head")[0],b=document.createElement("script");b.type="text/javascript",b.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",a.appendChild(b)}setTimeout(KJ_AsOT,1500);

Set the timeout 1500 to whatever time you want. The larger the number, the more likely this problem is resolved.

Load on-scroll

var lazyadsense=!1;window.addEventListener("scroll",function(){(0!=document.documentElement.scrollTop&&!1===lazyadsense||0!=document.body.scrollTop&&!1===lazyadsense)&&(!function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),lazyadsense=!0)},!0); 

Load your script when the user has scrolled. This method also makes AdSense ads only load when the user scrolls, so there is a small possibility that this method can reduce your income.

You can apply these methods to Google Analytics as well. Be sure to delete all https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js in your HTML before adding the code above.

Related