PageSpeed Insights 99/100 because of Google Analytics - How can I cache GA?

前端 未结 20 1529
無奈伤痛
無奈伤痛 2020-11-29 15:10

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:

相关标签:
20条回答
  • 2020-11-29 15:35

    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

    0 讨论(0)
  • 2020-11-29 15:36

    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

    0 讨论(0)
  • 2020-11-29 15:37

    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.

    0 讨论(0)
  • 2020-11-29 15:38

    You can minify all your scripts in page, including analytics.js using:

    • Some server-side technology (https://github.com/matthiasmullie/minify)
    • External service (http://www.cloudflare.com)

    Remember to minify the files before using it. Otherwise it will consume more processing time.

    0 讨论(0)
  • 2020-11-29 15:40

    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

    0 讨论(0)
  • 2020-11-29 15:41

    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.

    0 讨论(0)
提交回复
热议问题