Google Analytics file not found

2020-07-18 06:02发布

I am trying to use Google Analytics in my site. I copied the code directly from the Google Analytics website and followed the instructions for embedding it into my HTML.

Here is my code:

<script>
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];
    e.src='//www.google-analytics.com/analytics.js'; // this is the file
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
    ga('create','not giving this out');ga('send','pageview');
</script>

When I load my page with this script, it takes a number of seconds. Without the script, it loads practically instantly. This is how I know the problem is in the Google Analytics code.

The following error message appears after the page finally loads:

Failed to load resource: net::ERR_FILE_NOT_FOUND

After some further investigation, the file not found is from e.src='//www.google-analytics.com/analytics.js' on line 5.

How can I get this file or script to work correctly?

5条回答
时光不老,我们不散
2楼-- · 2020-07-18 06:34

I found out that if you remove the // from the beginning (e.src), the request works.

<script>
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];
    e.src='www.google-analytics.com/analytics.js'; // forward slashes taken out
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
    ga('create','not giving this out');ga('send','pageview');
</script>

I hope anyone with this problem in the future finds this helpful.

查看更多
男人必须洒脱
3楼-- · 2020-07-18 06:36

Is the error happening during local development or on a server?

If local dev then it maybe because it's trying to load it from the local hard disk.

If I don't load my html file via a local webserver I get this error

GET file://www.google-analytics.com/analytics.js net::ERR_FILE_NOT_FOUND

It works fine through a server

查看更多
Deceive 欺骗
4楼-- · 2020-07-18 06:39

This looks like a modified version of the script. When I look at the tracking code information for one of my properties, I see a different script.

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

  ga('create', 'UA-xxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>

This is the default script as far as I know. It differs slightly, especially the part where it calls the analytics.js-script. I do think the // should be in the code, as it helps with the relative calls to HTTP or HTTPS, but it might be working anyway like you stated.

查看更多
够拽才男人
5楼-- · 2020-07-18 06:43

Replace //www.google-analytics.com/analytics.js for http://www.google-analytics.com/analytics.js

查看更多
一纸荒年 Trace。
6楼-- · 2020-07-18 06:44

There is interference between the program trying to read the code and ad blocking software you have on your system. Disable adblock on ie (or chrome or firefox) temporarily. You must also disable any other ad blocking software you may have running. The analytics code will be found, and function normally.

It is not advisable to modify the google analytics code.

查看更多
登录 后发表回答