From: Thomas 'PointedEars' Lahn on 15 Jan 2010 04:53 mscir wrote: > I want to speed up my pages and I'm wondering if there's any reason not > to write the google javascript into a PHP inlcude file that I load from > my own server instead of downloading from google every time. Compatibility, perhaps. > So instead of including these 2 lines at the end of every page I want to > track: > > <script type="text/javascript">var gaJsHost = (("https:" == > document.location.protocol) ? "https://ssl." : "http://www."); `document.location' is deprecated ever since. Do not use. It is the first thing I change in those copy-and-pray scripts when I see them, to var gaJsHost = ("https:" == window.location.protocol) ? "https://ssl." : "http://www."; The standards-compliant and backwards-compatible `document.URL' would also work: var gaJsHost = (document.URL.substring(0, 6) === "https:") ? "https://ssl." : "http://www."; One wonders, though, why they do not just tell their users to include the right script for the right protocol. > document.write(unescape("%3Cscript src='" + gaJsHost + > "google-analytics.com/ga.js' > type='text/javascript'%3E%3C/script%3E"));</script> Yet another piece of evidence that the cluefulness at Google about client- side scripting is minimal, if that. Even if they kept the silly protocol detection, the same could be written more efficient and legible as document.write("<script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'><\/script>"); (And if they targeted XHTML, anyone ready to use it would use a CDATA section, and do not change anything here as well. Except perhaps the document.write() as it does not work with application/xhtml+xml.) That is the second thing I change in those scripts when I see them. > <script type="text/javascript">try {var pageTracker = > _gat._getTracker("UA-NNNNNNNN-N");pageTracker._trackPageview();} > catch(err) {}</script> And then this one. Obviously Google Analytics just breaks in older browsers as their implementations (JavaScript before 1.4, JScript before 5.0) are known not to support try-catch. They could have at least used a wrapper to hide the incompatible code, such as jsx.tryThis(). > I would have a single file on my own server that includes the script > from here: > > http://www.google-analytics.com/ga.js Define that. As I understand "includes the script" with regard to PHP (`include(...)'), it would not speed up anything. For would it not be slower because first the PHP script would need to be triggered? > and these lines: > > try { > var pageTracker = _gat._getTracker("UA-NNNNNNNN-N"); > pageTracker._trackPageview(); > } > catch(err) {} Yes, you can include that. A try-catch wrapper would be in order here, too. It is not even clear if try-catch is even necessary here. Apparently they are taking precautions against their own incompetence (ga.js does not load properly), which a feature test could handle easily, like if (typeof _gat != "undefined") { var pageTracker = _gat._getTracker("UA-NNNNNNNN-N"); pageTracker._trackPageview(); } > --- news://freenews.netfront.net/ - complaints: news(a)netfront.net --- Would it be possible to get rid of that in the future, please? PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
|
Pages: 1 Prev: anti alias with opacity in IE Next: Javascript Library Development Environment |