How to Install Lazy Load Google Analytics on a Blog

How to Install Lazy Load Google Analytics on a Blog
How to Install Lazy Load Google Analytics on a Blog




How to Install Lazy Load Google Analytics on a Blog - Speeding up blog loading is a good optimization step to provide visitor comfort when loading site pages either via desktop or smartphone.

The cause of a slow blog when accessed can be caused by various things, one of which is the Google analytics tracking code which has a big impact on blog loading.

In this post, we will discuss how to install Lazy Loadthe Google Analytics code to speed up blog loading. But before entering the tutorial stage, we will discuss a little about Google Analytics.

What is Google Analytics?

Google Analytics is Google's free online traffic tracking tool that is widely used by blogs or websites to analyze or get accurate, complete information about the state of blog visitors.

Now to be able to get complete information and even monitor visitors in real time, we need to link Google Analytics to the blog and apply a global site tag gtag.jslike the one below into the blog or website html.
<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>
But it's not uncommon for bloggers or website owners to complain about a problem that some of them consider big, namely the tracking code provided by Google analytics has an impact on slow loading blogs.

The solution to this problem is that the easiest way is to delay the loading of the Google Analytics JavaScript code by installing Google Analytics Lazy Load. Then how?

How to Install Lazy Load Google Analytics on a Blog

  1. Open the Blogger Dashboard
  2. Go to Themes > Edit HTML
  3. Find and delete the previous Google Analytics Code (example code as above)
  4. Then copy and paste the code below just above the </body> tag
  5. If already then Save
<!-- Global site tag (gtag.js) - Google Analytics -->
<script>
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-XXXXXXXXX-X";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),lazyanalisis=!0)},!0)
</script>
<script>
  window.dataLayer=window.dataLayer||[];
  function gtag(){dataLayer.push(arguments)}
  gtag("js",new Date());
  gtag("config","UA-XXXXXXXXX-X")
</script>

Don't forget to replace the highlighted part with your Analytics tracking ID .

Lazy Load Google Analytics 4

If your blog or website has upgraded ID Google Analytics 4 , you can use the following GA4 lazy load script:
<script type="text/javascript">//<![CDATA[
var LazyAnalytics=false;window.addEventListener("scroll",function(){(0!=document.documentElement.scrollTop&&false===LazyAnalytics||0!=document.body.scrollTop&&false===LazyAnalytics)&&(!function(){var e=document.createElement("script");e.type="text/javascript",e.async=true,e.src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),LazyAnalytics=true)},true);
//]]></script>
<script type="text/javascript">//<![CDATA[
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
//]]></script>

How to Check Blog or Website Speed

You can check changes in your blog or website speed score using the pagespeed.web.dev or GTmetrix tools which you can use for free.

Closing

So, that was How to Install Lazy Load Google Analytics on a Blog to speed up loading a blog or website. If this article is useful, you can share and recommend it to friends who need it, if you have other questions, you can ask via comments.

Post a Comment

Welcome to Jehangir Khan's Tech Blog! We're thrilled to have you join the conversation. Your comments and feedback are valuable to us and help us create a community of tech enthusiasts who share ideas and insights. We encourage you to share your thoughts related to the post and engage in a respectful and meaningful conversation with others. Please refrain from spamming or promoting your own content in the comments section. We also ask that you avoid using inappropriate language or attacking others. Please note that all comments will be reviewed before they are posted, and we reserve the right to remove any comments that violate our policies. Let's explore the world of technology together and inspire each other to achieve our goals!

Previous Post Next Post