Is your website slow because of Google Analytics? Many sites suffer from this! The default tracking code can hurt your PageSpeed Insights score. But there’s an easy fix. We’ll show you how to optimize it for faster loading and better performance.
The Solution: Lazy Load Google Analytics
The fix is to lazy load your script. This means the script will only load when a user interacts with your website, such as by scrolling or clicking. This will not affect user experience or slow down the initial website load time caused by Google Analytics.
Once you have a piece of code to add to your site, by default it will look like this:
Adding Code to Your Website’s Head Tag
Here’s the modified code with the scroll load event. Using this code will ensure Google Analytics no longer slows down your website. Add it inside the <head>
tag and don’t forget to replace the gtag id (G-xxxxxxx) with your actual ID.
If you’re using WordPress, to add the code to your WordPress site, you can use the wp_footer
function and paste it at the end of the functions.php
file in your theme. It will look like this:
By implementing the scroll load event technique for Google Analytics, you can significantly improve your website’s page speed, boost performance scores, and deliver a better user experience overall.
Read more: How to Fix Google Adsense Ads Slowing Down Your Website