It’s very common to get this warning when you run speed tests for your WordPress website:
This error is referring to Google Analytics “analytics.js” file. And as it a 3rd party script, you cannot add expires headers to your .htaccess file or your Nginx configuration file.
How to fix for your WordPress?
Generally, you can fix this issue or any similar one by hosting the script locally on your server. But this particular script needs to be updated regularly when Google updates it. So you can host the script and run a cron job to update it.
Fortunately, there is a lightweight plugin that does everything for you, just install, configure and forget about the Google Analytics Leverage Browser Caching warning.
Complete Analytics Optimization Suite (CAOS) is a plugin that inserts the Analytics tracking code into the header or footer, saves the analytics.js file locally and keeps it updated using wp_cron().
When you install Complete Analytics Optimization Suite (CAOS), go to Settings -> Optimize Analytics
Enter in your Google Analytics Tracking ID and position for your tracking code. That’s all!
Now test your newly added tracking code by visiting your website in a new incognito window (Google Chrome) or Private window (Firefox) by going to your Google Analytics account -> Real Time -> Overview.
You will need to deactivate other Google Analytics plugins or remove any other Google Analytics tracking codes you added to avoid inaccurate reporting.
Complete Analytics Optimization Suite (CAOS) is a cool plugin that helps you to add the Google Analytics code wherever you want (in the footer for example) and also get rid of the Leverage Browser Caching warning in your website speed tests.