vhost.conf

location ~* (analytics\.js)$ {
    alias                     /path/analytics.js;
    proxy_cache               pnc;
    proxy_cache_valid         200 304 7d;
    proxy_cache_valid         any 1m;
    proxy_cache_lock          on;
    proxy_cache_lock_timeout  5s;
    proxy_cache_use_stale     updating error timeout invalid_header http_500 http_502;
    etag                      on;
    expires                   7d;
}

crontab -e

0 0 * * 0 wget https://www.google-analytics.com/analytics.js -O /path/analytics.js

JS

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://bpplpp.com/analytics.js','ga');
  ga('create', 'UA-12345678-1', 'auto');
  ga('send', 'pageview')</script>

相关资料

Google Analytics 异步优化方案

使用Nginx将请求转发至Google Analytics实现后端统计

Ng­inx 内配置 Google An­a­lyt­ics 指南

Nginx反代Google Analytics配置