How to fix a problem with Google Analytics for a WordPress website, caused by LiteSpeed caching plugin.
Update September 2021: current LiteSpeed plugin version (update) no longer has the problematic option – so there’s nothing to fix and no problems. 🙂
- Noticing a Google Analytics problem
- Determining the problem cause
- Fixing the LiteSpeed caused Analytics problem
1. Noticing a Google Analytics problem
OK, I have noticed a problem, how will I solve it?
2. Determining the problem cause
The first thing I do when checking if Google Analytics works properly is going to open a page on my website, and go to Google Analytics “Realtime” reports page – to see if my visit is shown there.
This looked OK when I was logged-into the website as an admin, but if I opened a page with an incognito browser tab (not logged in), my visit didn’t show in Google Analytics.
I hadn’t thought of cross-checking this right away, until bibi from LowEndSpirit forum suggested it.
I test every update, or website change, in a staging environment before implementing them on my websites. Everything looked OK up to that point, and I hadn’t made any website changes before the problem occured.
I compared page source code of the WordPress website that had “lost Analytics,” with a plain HTML website who’s Analytics worked fine – looking for Google Analytics tag code. It showed this:
Plain HTML website: <script async src="https://www.googletagmanager.com/gtag/js?id=... WordPress website: <script async src="https://io.bikegremlin.com/wp- content/litespeed/localres/www.googletagmanager.com/gtag/js?id=
You can see a page’s source by clicking the right mouse button, then choosing the “View page source…” option, or using CTRL+U key combination.
I’m not smart enough to see the problem cause from this. However, I did suspect LiteSpeed, so I Googled: “Google Analytics LiteSpeed problem.”
It turned out that LiteSpeed’s update of February the 2nd 2021 caused problems to many people.
3. Fixing the LiteSpeed caused Analytics problem
The solution is simple:
- Go to the LiteSpeed caching plugin options.
- Choose the “Page Optimization” menu option.
- Click on the “Localisation” option.
- Delete, or comment out (using the “#” sign) all the Google script lines.
- Click on the “Save Changes” button.
Picture makes it clearer:
Your website’s list may include many more scripts, but these five are the ones causing problems with Google Analytics:
https://www.google.com/recaptcha/api.js https://www.google-analytics.com/analytics.js https://www.googletagmanager.com/gtag/js https://www.googletagmanager.com/gtm.js https://www.googletagservices.com/tag/js/gpt.js
You can also solve the problem by turning off the “Localise Resources” option, with all the pros and cons of such solution.
This is one of the things I don’t like about WordPress. In spite of all the caution & precautions, problems like these can arise. To make matters worse, they sometimes aren’t even noticed right away.
Many themes and plugins are updated relatively frequently. Some of these updates are “security critical,” or could be important. So you are bound to be updating relatively frequently, with every update posing a risk. You are damned if you update, and damned if you don’t. 🙂