How to Lazy Load Google Analytics Code in Blogger
Google Analytics is a commonly used JavaScript code on websites, but it can cause a render-blocking effect during webpage loading.
Copyright: Tech4era.com |
It’s important to keep the code that tracks visitors coming to the website. However, you can improve the page speed by Lazyloading the Google Analytics code on the Blogger website. To do this, you can follow the steps given in this article.
Install Lazy Load Google Analytics code in Blogger?
#1: Go to the Blogger settings page and remove the Google Analytics property ID.
#2: Now go to the theme section and create a backup of your theme.
#3: Now click on edit HTML and search for old analytics code if any and remove it.
So generally, the script installation will look like this.
<script async='async' src='https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x'/>
<script>
//<![CDATA[
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxx-x');
//]]>
</script>
#4: Just remove the highlighted part and add the below script just above it.
<script type='text/javascript'>//<![CDATA[
var lazyanalisis=!1;window.addEventListener("scroll",function(){(0!=document.documentElement.scrollTop&&!1===lazyanalisis||0!=document.body.scrollTop&&!1===lazyanalisis)&&(!function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxxx";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),lazyanalisis=!0)},!0);
//]]></script>
#5: Here replace the tracking code (highlighted) in both of the codes and paste it just above the </body> tag.
Now save your theme and the lazy analytics code is added to your blogger website. Now check your analytics code, if it is properly working or not.
In this way, you can install google analytics code to Blogger and improve the loading speed.