Thread Locked This thread is locked - replies are not allowed.



Permlink Replies: 1 - Pages: 1 - Last Post: 29-Nov-2016 19:51 Last Post By: jGromit
jGromit

Posts: 33,151
Registered: 31-Jan-2006
New Google Analytics code, and local preview
Posted: 25-Apr-2014 00:46
Google Analytics (GA) is now using a new "universal" code snippet. It looks like this (with your own property ID and domain, of course):
<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-999999-9', 'example.com');
  ga('send', 'pageview');
 
</script>
But if you preview your album locally, by simply opening index.html from your hard drive, this code causes serious problems in Chrome and in the jAlbum integrated browser, and locks up IE11 completely. The reason is found in that fifth line. When you're running locally, it ends up trying to load the GA script from:

file://www.google-analytics.com/analytics.js

This, of course, fails miserably, and not in a graceful way.

There are two ways to work around this problem. If you know for a fact that you will always be serving your pages with the http: protocol, and never with the secure https: protocol, you can change the fifth line to this:
})(window,document,'script','http://www.google-analytics.com/analytics.js','ga');
If you might be serving your pages with either protocol, this modification to line 5 will adapt to either protocol:
})(window,document,'script',('https:' == document.location.protocol ? 'https://' : 'http://') +'www.google-analytics.com/analytics.js','ga');
jGromit

Posts: 33,151
Registered: 31-Jan-2006
Re: New Google Analytics code, and local preview
Posted: 29-Nov-2016 19:51   in response to: jGromit in response to: jGromit
An update. Google Analytics finally realized that they were doing this the hard way. Now, the code they tell you to use always connects to Google with the secure https protocol. This will, of course, work properly on any page, whether the page itself is using http or https, and it doesn't cause any problems when viewing a page locally.
<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-9999999-1', 'auto');
  ga('send', 'pageview');
 
</script>
Substitute your own property ID, of course (the UA number).
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums