Speed Up your website, Fix Google Analytics JavaScript loading File Delay

Speed Up your website, Fix Google Analytics JavaScript loading File Delay


Speed Up your website, Fix Google Analytics JavaScript loading File Delay

Peace, mercy, and blessings of God, all website owners need to see how their websites do on the web, so they have to add their websites to the Google Analytics service to analyze their traffic. 

Speed Up your website Fix Google Analytics JavaScript loading File Delay
Fix Google Analytics JavaScript loading File Delay


Google Analytics service is a helpful tool for all websites managers, that contains a lot of important Google Analytics certified tools especially after Google upgraded Google Analytics to Google Analytics 4.

So if you have heard about it or you have used Google Analytics 4 before now, you are in the right place, but if you didn't hear about it you can find a lot of lessons and courses about Google Analytics for beginners

What is Google Analytics?

Google Analytics is a free service from Google LCC that contains a group of tools that help you to analyze your business on the internet, We are not going to explain the Google Analytics tool here, but we will just mention its importance.

What is Google Analytics
What is Google Analytics

Google Analytics tool is important to see how many users pass to your website through the google search engine and also with direct transmission from any social media platform or direct URL.

Is Google Analytics effective by website speed?

This is an important point, if you have added Google Analytics to your website, you must add some JavaScript code to your website pages.

This code must make your pages load slowly, why? the answer is:

The code that you add contains an external JavaScript file that is responsible for recording traffic on your site, that JavaScript file it's has a big file size, so it takes a long time to load.

This part is when you test your website via Google PageSpeed Insights and get pad results and you see the reason for this pad result is the JavaScript file size of the Google Analytics service.



What can I do?

Don't worry, every problem in the developing world has a way to fix.

I am on the Dev To Give site trying to get the solution to every issue face the beginners and make it easy to solve.

What is the way?

You can fix the Google Analytics JavaScript file load time delay issue with a simple and clever trick, which is a trick that delays file download after page loads and doesn't affect your page speed anymore.

How to do that?

Ok, first you should remove the previous Google Analytics code and put the new code that will I give you now.

This code:


// Lazy Load analytics start

setTimeout(function(){  
var myAnalytics = document.createElement('script');
myAnalytics.setAttribute('src','https://www.googletagmanager.com/gtag/js?id=G-ZR7PZBPEX9');
document.head.appendChild(myAnalytics);

window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-ZR7PZBPEX9');
},10000);
  
// Lazy Load analytics end

You should replace the id that is colored in red color with your Google Analytics id.

If you don't know how to get your Google Analytics id then follow these steps to get it.

1- Open https://analytics.google.com in your browser.

2- Click on the "Admin" option in the left bar like the image below.

Google Analytics Admin
Google Analytics Admin

3- Click on "Setup Assistant" like the image below.

Google Analytics Account
Google Analytics Account

4- Click on the "Tag installation" bar to open the code installation instructions .



Google Analytics Tag installation
Google Analytics Tag installation

5- Click on the website you want to add code to it.

Google Analytics websites
Google Analytics websites

This is your Google Analytics ID, you can copy and past it into your new code.

Now, if you test your page after replacing the old Google Analytics code with new code you will see a whole difference in loading speed, and your SEO rank will increase because of that.

Conclusion

This was the best way to reduce the delay in loading the JavaScript codes for Google Analytics, which causes slow sites, which in turn is something that Google hates and causes a decline in your site's SEO score.

If you have other ideas or questions feel free to put them in the worthy below, everyone is welcome.

Murad Aden

I am a web developer, I love my sons, then my code, For development posts visit Dev To Give

Post a Comment

Previous Post Next Post
X Please support us and subscribe

Click Subscribe to close the window