With a few quick adjustments, Google Analytics can be reconfigured with the
defer attribute for faster page loading and improved user experience.
Particularly if you don't need Google Tag Manager or just want to load Google Analytics resources directly, analytics.js and the companion inline snippet can be set up to load in the background and then execute after the page has otherwise rendered.
(Learn more about website loading speed with the complete guide.)
Two Parts, Two Steps
async attribute. These lines are placed in the
<head> section of each page, typically as part of a template:
Although the potential impact on loading speed of loading Google Analytics this way is relatively small compared to bigger issues like image optimization, these resources can interrupt rendering of the page. When it comes to website speed every little bit helps, so reducing their priority is an easy loading speed win.
Move The Inline Snippet
Create a new file if necessary and of course minify the result and serve the file with compression as well as a long cache expiration period
The next step is even easier. Replace the
async attribute on the analytics.js reference with
And that's it! The result still has two parts, but both the external file reference and the snippet will now load in the background at a low priority and then execute later in the loading process after the page has otherwise rendered, just before the
Questions? Need help?Brandon@FrontMend.com
Learn more about maximizing website loading speed:Complete Guide