With Google Analytic improving and embracing on page download/action tracking, it is increasingly popular to insert the Google Analytic code before the page loads, inside the tag and call it OnLoad.
With that comes the problem of executing a piece of script which will make your page load slowly if there’s a delay in loading the external ga.js file.
0.3 seconds average load time
Regardless if you place the code in the or just before the tag, some interesting information is available from Pingdom on the average load times of the external Google Analytics code.
Loads 97% slower at peak hours – in Europe
On average the load time across Europe is 0.3 seconds, which may not seem large, but with the popularity increasing and the relevantly poor internet connections in Ireland — this can be an issue.
Installing it locally
Hosting the JavaScript file locally is easy, with advise by Google themselves.
First, you’ll need to download this file http://www.google-analytics.com/ga.js and upload it to your web server. (note: this is the newer Google Analytics code)
Now replace the following piece of code on your web page or blog
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
and replace it with
document.write(unescape("%3Cscript src='" + gaJsHost + "your-website.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
Once complete, you can verify that the code is tracking by checking the tracking status in Google Analytics.
More reading
- Google Analytics docs (info on all things ga.js)
- Top methods for speeding up website by AskApache.com
- Google Analytics <a target=“_blank” href=“http://www.grokdotcom.com/2008/10/16/google_analytics_hacks/ target=” _blank”>Plugins & Hacks
