Adding Google Analytics to WordPress is a painless process. There are numerous ways to do it. Today, we will follow some simple steps to get it added.
There is a plugin in the WordPress Plugin Repository that allows us to not only add the Google Analytics code, but load it in such a way that passes the Google PageSpeed Insights test.
By default, the Google Analytic’s script has a low 2 hour cache time set, which ironically triggers a flag in Google PageSpeed Insights.
The aforementioned plugin will let you add your Tracking ID, load the tracking code in the footer, and also load the script locally.
Loading the script locally bypasses the 2 hour cache limitation, removing the flag set by Google’s Page Speed test.
There is one caveat however, and that is that Google doesn’t support the local storage of the script.
The reason Google does not prefer the local serving of the script, is because it can quickly become out of date when upgrades occur.
However, the plugin that stores it, also leverages WP Cron to auto-update the script, so that concern becomes moot.