I\'m on a quest to reach 100/100 on PageSpeed and i\'m almost there. I\'m trying to find a good solution to cache Google Analytics.
Here is the message I get:
try this just insert before
<script async='async' src='https://cdn.jsdelivr.net/ga-lite/latest/ga-lite.min.js'></script> <script>var galite=galite||{};galite.UA="xx-xxxxxxx-x";</script>
Please change xx-xxxxxxx-x to your code, please check to implementation here http://www.gee.web.id/2016/11/how-to-leverage-browser-caching-for-google-analitycs.html
For Nginx:
location ~ /analytics.js {
proxy_pass https://www.google-analytics.com;
expires 31536000s;
proxy_set_header Pragma "public";
proxy_set_header Cache-Control "max-age=31536000, public";
}
Then change path https://www.google-analytics.com/analytics.js to https://yoursite.com/analytics.js
Google cautions against using local copies of the analtics scripts. However if you are doing it, you will probably want to use local copies of the plugins & the debug script.
A second concern with agressive caching is that you will be getting hits from cached pages - which may have changed or have been removed from the site.
You can minify all your scripts in page, including analytics.js
using:
Remember to minify the files before using it. Otherwise it will consume more processing time.
store localy analytics.js, but it is not recommended by google: https://support.google.com/analytics/answer/1032389?hl=en
it is not recommended cause google can update script when they want, so just do a script that download analytics javascript each week and you will not have trouble !
By the way this solution prevent adblock from blocking google analytics scripts
There's a subset of Google Analytics js library called ga-lite that you can cache however you want.
The library uses Google Analytics' public REST API to send the user tracking data to Google. You can read more from the blog post about ga-lite.
Disclaimer: I am the author of this library. I struggled with this specific problem and the best result I found was to implement this solution.