谷歌分析-加载资源失败: http://www.Google-Analytics.com/ga.js

我注意到 Chrome 控制台上的这个错误已经有一段时间了:

enter image description here

我修改了 Google 的脚本,让它记录错误,因为它使用的是 try{} catch{},下面是我得到的结果:

enter image description here

我没有注意到 相当可观在统计上的变化,它总是起伏不定。

此外,这不仅是在我的网站,但他妈的到处。我没有发现错误报告或类似的东西。

如果我在浏览器上打开 http://www.google-analytics.com/ga.js,它会正常加载。

有人知道是什么引起的吗?

85269 次浏览

I've noticed same thing on my browser some time ago. Did you sing in to chrome using your Google account maybe? Or did you choose in any way to opt-out from collecting data on Google Analytics ?

Maybe Google remembers that option and uses it on Chrome when you are singed in..

BTW. I can normally open http://www.google-analytics.com/ga.js in browser, it just doesn't work when automatically loaded.

It was a problem with AdBlock. I disabled it and now it loads it normally.

yagudaev suggests (read answers below) that in order to keep AdBlock from blocking Google Analytics, you need to edit the snippet provided and explicitly use https:// instead of the protocol-relative URL by default. This means changing

'//www.google-analytics.com/analytics.js'

into

'https://www.google-analytics.com/analytics.js'

Example:

<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://www.google-analytics.com/analytics.js','ga');


ga('create', 'UA-XXXXX-XX', 'auto');
ga('send', 'pageview');
</script>

It could also be your hosts file, here's mine:

$ grep -ni "google-analytics.com" /etc/hosts
6203:# 127.0.0.1  ssl.google-analytics.com #[disabled = Firefox issues]
6204:127.0.0.1  www.google-analytics.com #[Google Analytics]

If it's an offline app (ie, you've defined a cache manifest) be sure to allow the network request.

See HTML5 Appcache causing problems with Google Analytics

The reason you are running into problems is because AdBlock will block this script if and only if it does not go through https. Notice the error you get it contains an http: protocol reference.

All you need to do is change the snippet to force it to go through an ssl connection by adding an explicit protocol instead of the protocol relative url that is the default.

<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://www.google-analytics.com/analytics.js','ga');


ga('create', 'UA-XXXXX-XX', 'auto');
ga('send', 'pageview');
</script>

Ensure Fiddler (or similar proxy) is not active.

This error is commonly caused due to one of the extensions installed within Chrome.
There are a few ways to debug and solve an ERR_BLOCKED_BY_CLIENT message.

  • Disable the extension.
  • Whitelist the domain.
  • Debug the issue.

I would recommend to find more detail at How to Solve ERR_BLOCKED_BY_CLIENT

2019 update

This has become very widespread now.

Solutions

  1. Ask people to unblock your website, (bad idea from personal experience)
  2. Host google analytics script locally (bad idea) because google says so HERE

Referencing the JavaScript file from Google's servers (i.e., https://www.googletagmanager.com/gtag/js) ensures that you get access to new features and product updates as they become available, giving you the most accurate data in your reports.

  1. Use Server side analytics. This is what people are doing nowadays. If you are on node.js, use a library such as analytics or universal-analytics