PageSpeed Insights 99/100 因为 Google Analytics - 我如何缓存 GA? [英] PageSpeed Insights 99/100 because of Google Analytics - How can I cache GA?

查看:20
本文介绍了PageSpeed Insights 99/100 因为 Google Analytics - 我如何缓存 GA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求在 PageSpeed 上达到 100/100,而且我快到了.我试图找到一个很好的解决方案来缓存 Google Analytics.

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.

这是我收到的消息:

利用浏览器缓存在静态资源的 HTTP 标头中设置到期日期或最长期限会指示浏览器从本地磁盘而不是通过网络加载先前下载的资源.为以下可缓存资源利用浏览器缓存:http://www.google-analytics.com/analytics.js(2 小时)

Leverage browser caching Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network. Leverage browser caching for the following cacheable resources: http://www.google-analytics.com/analytics.js (2 hours)

我发现的唯一解决方案是从 2012 年开始的,我认为这不是一个好的解决方案.本质上,您复制 GA 代码并自己托管.然后,您每天运行一次 cron 作业重新检查 Google,以获取最新的 GA 代码并替换它.

The only solution i've found was from 2012 and I do not think it is a good solution. Essentially you copy the GA code and host it yourself. You then run a cron job to recheck Google once a day to grab the latest GA code and replace it.

http://diywpblog.com/leverage-browser-cache-optimize-google-analytics/

我还能做些什么来在使用 Google Analytics 的同时达到 100/100?

What else can I do to reach 100/100 while also using Google Analytics?

谢谢.

推荐答案

好吧,如果 Google 在欺骗你,你可以欺骗 Google:

Well, if Google is cheating on you, you can cheat Google back:

这是 pageSpeed 的用户代理:

This is the user-agent for pageSpeed:

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.8 (KHTML, like Gecko; Google Page Speed Insights) Chrome/19.0.1084.36 Safari/536.8"

您可以插入条件以避免将分析脚本提供给 PageSpeed:

You can insert a conditional to avoid serving the analytics script to PageSpeed:

<?php if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos($_SERVER['HTTP_USER_AGENT'], 'Speed Insights') === false): ?>
// your analytics code here
<?php endif; ?>

显然,它不会带来任何真正的改进,但如果您唯一关心的是获得 100/100 的分数,这会做到.

Obviously, it won't make any real improvement, but if your only concern is getting a 100/100 score this will do it.

这篇关于PageSpeed Insights 99/100 因为 Google Analytics - 我如何缓存 GA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆