Google PageSpeed Insights flags browser caching of Google Analytics:
Leverage browser caching
Set up a proxy in your Nginx configuration file to address this issue.
Google PageSpeed Insights flags browser caching of Google Analytics:
Leverage browser caching
Set up a proxy in your Nginx configuration file to address this issue.
server { | |
### ... | |
location = /analytics.js { | |
# Proxy to google-analytics.com | |
proxy_pass https://www.google-analytics.com; | |
# Custom expires time | |
expires 1y; | |
} | |
} |
I don' think this solution is good, because browser will cache analytics.js for one year, in that time the script might be updated by google and analytics might suddenly stop working.
While I like this idea of handling this through nginx config, we technically need to download the file, give it unique name e.g. analytics.2020-09-03.js and then regularly download analytics.js from Google and check if content matches the local copy (can be done using cron job), if it doesn't update the file and then update the file name to bust cache to something like analytics.2020-09-04.js.