I was so excited when Google Analytics was released to the public. At that time, I was a regular reader of ProBlogger.net – helping bloggers earn money. Darren Rowse broke the news to the blogging community that Google Analytics was offering free accounts on a first-come-first served basis. When I finally received my invitation from Google, I immediately added all of my websites to be tracked and painstakingly followed each and every instruction -that is, every instruction except for this one:
“It [the source code] should be immediately before the [ending] body tag of your page, and should look exactly as it was provided to you.”
Server Slowdowns
Why was that important. When I learned JavaScript, I understood that most code belongs in the starting “head” tag so that you can be sure that the code is executed – and I wanted to make sure that this code was executed for statistical purposes. So, that’s exactly where I put it. In the next week, I noticed that my site’s latency was increasing; loading every page took about a second more in the beginning. I also noticed that pages on my server that did not require the Google Analytics code had very short latency. So, I changed the source code so that the Google Analytics script ran right before the ending “body” tag. And of course, loading time was significantly increased.
From this discovery we can learn an important lesson about script placement, be it a client-side script like JavaScript or a server-side script like PHP. Any scripts that are not necessary for users to “normally” access your web site need to be placed right before the ending “body” tag. Usability should take precedence over statistics. Otherwise, visitors will leave because these scripts slow things down and make it appear that the server is disconnected.
Many people commit the crime of putting this software in the tag. One prime example is (ironically) the ProBlogger site.