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?
I found out that if you remove the
//
from the beginning (e.src
), the request works.I hope anyone with this problem in the future finds this helpful.
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
It works fine through a server
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.
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.
Replace //www.google-analytics.com/analytics.js for http://www.google-analytics.com/analytics.js
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.