Did you just run a speed check on your site only to find out the scores are falling behind and the thing that takes up most of the time is the Google tag manager code?
You are not alone. It’s the same for all of us. The tag manager and adsense codes slows down every site!
That said, there are ways to speed up your site.
You can either lazy load this script or self host it.
AD:
Need a FREE stunning WordPress website for your business? 😍
We've got you covered!❤️
Enjoy premium looking, mobile-friendly and professional WordPress site — all for FREE!🔥
Contact us now and let our expert designers create your dream website. Whatsapp Now! OR Email >> [email protected]
P.S You only need to provide the domain and one year of hosting is FREE. If you are satisfied, please give us a video testimonial. We are starting a new web design agency and looking to build a portfolio.
- Lito James
In this guide, I’ll show you exactly how to reduce unused javascript Google tag manager code.
How To Reduce Unused Javascript Google Tag Manager
Before you try any of these, if you are on Ezoic, there are options on Leap to reduce unused javascript, delay scripts and a bunch of other stuff that will take care of the tag manager code.
Check out this overview of the features of Leap.
If your site has good traffic, there’s an option to let Ezoic staff set the leap settings themselves.
But if you are not on Ezoic – don’t worry. Follow one of these and it will speed up your site!
I usually lazy load on all my sites and it takes care of it, but I mentioned self hosting just to give you a full view of the solutions.
1. Lazy Load Google Tag Manager
Lazy loading basically will stop loading scripts until a user does something on the site like scrolling or clicking on something.
⚡Also Read: Blocked Due To Other 4xx Issue Error
This will actually speed up the site and will also improve Google pagespeed scores.
That said, the only con of lazy loading gtag is that your Analytics will not count users who just load the site and exit it without any interactions.
So your bounce rates would be a bit off, but the users that do that are bots most of the time.
Now, let’s get into how to actually reduce unused javascript Google tag manager:
Editing The Code Manually
The Analytics code usually has 2 scripts (see below)
Remove the first script and paste it in a notepad or somewhere, and add this code on top of the second script.
Make sure to add your analytic tag ID instead of the XXXXs.
The code should now look like this –
Now you have the code ready, it’s time to place it on the site.
We are usually asked to place this code in the <head>, but we need to add this code right before the </body> tag of the site.
And remember the first script we removed? You need to add this part below the </head>
That’s it. The script will only be loaded when the user scrolls!
You can run the site on Pagespeed and check if it solved the issue.
⚡Also Read: Fixing Invalid Object Type For Field ‘Brand’
With A Plugin
There are a lot of WordPress plugins that can get this done. WP Rocket is a good example.
There’s an option called Delay Javascript Execution in the File Optimization tab of the plugin settings. Just check this box and it will automatically lazy load gtag, plus other site slowing scripts.
This will also help with page speed issues including Remove unused JavaScript, Reduce Script Execution time and Minimize Main thread work.
Plus, it improves total blocking time and first contentful paint times.
2. Self Hosting The Analytics Tracking Script
Another way to fix the “reduce unused javascript Google tag manager” issue is to self host it.
However, keep in mind that you are not allowed to do this for the adsense code. It’s against their TOS and they will ban/stop ads.
Now, the reason why the tag manager code slows down the site is because it’s a third party script that makes requests from an external website.
These codes make additional DNS requests which is the cause of the slow loading.
You can make this load much faster by hosting the script on your server and cutting down the requests sent receiving the files from Google’s servers every time.
Now there are two ways to do this – manually or with a plugin.
To be honest, I don’t really know how to do it manually because I am no programmer. I will only show you how to do it using a couple plugins.
Self Hosting Google Tag Manager With Rank Math
Most of us use Rank Math and fortunately for us, it has the ability to self host the gtag script.
⚡Also Read:: Rank Math vs SEO Framework
- Head over to the Rank Math dashboard and then enable the Analytics module.
- It will then ask you to connect your Google account. Do this.
- Go to Rank Math SEO > General Settings > Analytics
- Find and enable the “install analytics code
- After that, you’ll see 3 options pop up below. From this click on “Self-Hosted Analytics JS File” to enable it.
That’s it! Check out your page speed scores now and you should see an improvement.
Rank Math will update this script every 12 hours, so don’t have to worry about it anymore.
Self Hosting Google Tag Manager With MonsterInsights
This is super easy. You just have to install their performance add-on and click a button.
- Go to Insights > Addons and then install Performance.
- After it’s activated, go to Insights > Settings > Advanced > Performance.
On this tab you’ll see something like this at the bottom:
- Enable this and click on ‘Fetch and Update gtag.js file”.
That’s it! Plus, Monsterinsights will automatically fetch and update this every 24 hours.
Self Hosting Google Tag Manager With Perfmatters
Another plugin that can get it done is Perfmatters.
- Go to Perfmatters > Analytics
- Put in your analytics tracking ID in the “Tracking ID” field
- Select “Enable Local Analytics” and save changes.
That’s it. And like the other plugins, this too will update the file every 24 hours.
Final Thoughts
Hope this fixes up your site speed! Let me know in the comments if you run into any errors.
While both of these are good solutions, I think lazy loading will get you the best results because it completely eliminates the script.
But to be fair, I have only tried lazy loading with Ezoic’s Leap, so I have no idea how the scores would be for self hosting it.