lazyLoadScript to load the Google Analytics and Google AdSense scripts and not harm web load performance.

Here’s an example of a lazy loading JavaScript snippet that can help you to load Google Analytics and Google AdSense tags asynchronously, which can improve website loading times:

This code creates two functions, lazyLoadScript and lazyLoad. The lazyLoadScript function creates a new script element, sets its type and source URL, and adds it to the head of the HTML document. The lazyLoad function calls lazyLoadScript to load the Google Analytics and Google AdSense scripts.

Finally, the code uses an event listener to trigger the lazyLoad function when the page has finished loading. The event listener is set up to work with different browser versions.

To use this code, simply copy and paste it into the head section of your HTML document, replacing the URLs with your own Google Analytics and Google AdSense script URLs. This will ensure that these scripts are loaded asynchronously after the page has finished loading, improving the page’s loading times.

  function lazyLoadScript(url) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = url;
    script.async = true;

  function lazyLoad() {
    // Lazy load Google Analytics
    // Lazy load Google AdSense

  if (window.addEventListener) {
    window.addEventListener("load", lazyLoad, false);
  } else if (window.attachEvent) {
    window.attachEvent("onload", lazyLoad);
  } else {
    window.onload = lazyLoad;

Sources and Resources, the above was written with the help of ChatGPT and ChatGPT referenced the following sources:

J.F. Aguilar

Juan F. Aguilar is a web advisor, a Psychologist and self taught web developer, with over 18 years of experience empowering people to leverage web technologies in a smart and cost effective way. Specifcially in UX, CRO and SEO. Juan has helped over hundreds of web based projects increase their utilities, either as a private contractor or consultant. He has worked with big fast growing brands like,,, and to small and medium sizef businesses trying to maximize their lead generation and general online strategy and results. Connect with Juan on LinkedIn I also write about important social issues of our current times, that are probably very relevant for you, you can find theme here.

Published by
J.F. Aguilar

Recent Posts

Benefits of Using ChatGPT for SEO:

Content Creation: ChatGPT can generate high-quality, keyword-optimized content to improve search engine rankings. Understanding User…

2 weeks ago

The dark side of the crypto craze

To bypass expenses, hackers create "miner botnets," networks of infected computers that mine cryptocurrency without…

4 months ago

Managing Customer Expectations: The Key to Successful Web Optimization

As a web optimization professional, it is essential to manage your customers' expectations and educate…

7 months ago

Short 8 Step guide to A/B testing:

A/B testing, also known as split testing, is a powerful technique used by businesses and…

7 months ago