Speeding up your website when using Google Analytics

02.13.09 Posted in Google Analytics by Barry

With Google Ana­lytic improv­ing and embrac­ing on page download/action track­ing, it is increas­ingly pop­u­lar to insert the Google Ana­lytic code before the page loads, inside the tag and call it OnLoad.

With that comes the prob­lem of exe­cut­ing a piece of script which will make your page load slowly if there’s a delay in load­ing the exter­nal ga.js file.

0.3 sec­onds aver­age load time

Regard­less if you place the code in the or just before the tag, some inter­est­ing infor­ma­tion is avail­able from Ping­dom on the aver­age load times of the exter­nal Google Ana­lyt­ics code.

Loads 97% slower at peak hours – in Europe


google-analytics-loading-time

On aver­age the load time across Europe is 0.3 sec­onds, which may not seem large, but with the pop­u­lar­ity increas­ing and the rel­e­vantly poor inter­net con­nec­tions in Ire­land — this can be an issue.

Installing it locally

Host­ing the JavaScript file locally is easy, with advise by Google themselves.

First, you’ll need to down­load this file http://​www​.google​-ana​lyt​ics​.com/​g​a​.js and upload it to your web server. (note: this is the newer Google Ana­lyt­ics code)

Now replace the fol­low­ing 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 com­plete, you can ver­ify that the code is track­ing by check­ing the track­ing sta­tus in Google Ana­lyt­ics.

More read­ing