One of the biggest slags on your website (if you didn’t know it already) is Google Tag Manager. This might come as a surprise to some knowing how much of a perfectionist Google can be regarding speed. Thankfully there is a solution to fix the slowdown of Google Tag Manager by delaying its loading and here’s how to do it.
To delay the loading of the Google Tag Manager we will be using the following code from Mattias Siø Fjellvang. You can add the script to your header directly or enqueue it as a separate JS file whichever you feel works best for your site.
There are a few places you will need to change to make the code fully functional. The first one is the “YOUR-GTM-CODE-HERE” to include your Google Tag Manager ID.
The second one you can edit to your own preference is the “2000” which sets the delay for Google Tag Manager. In our example, it is set to 2000 which equals 2 seconds.
Note – A few things to note from the script are that if the user scrolls, move a mouse, or does something else on the site, the script will not be delayed so you can start collecting analytics instantly. Though if the user does nothing and leaves before 2s have passed they will not be added to your analytics statistics since the script has never loaded.
Remember that even though this code delays the Google Tag Manager you still need to ask consent for cookies from all EU-based visitors. If you don’t know “How To Add Cookie Consent PopUp/Banner In WordPress“, click the link to follow our guide.