how to insert google analytics tracking code into the wordpress header manually

When inserting the google analytics tracking code manually into a WordPress blog, Google recommends that it be the last piece of code before the end of the head tag. Some WordPress themes suggest that wp_head() must be the last code before the end head tag. So, which goes last !?

Well…..The GA code can be either before or after the wp_head. It really does not matter…..If you are interested to know why, then read on.

The TwentyTen/TwentyEleven themes for example shows the following code in the header.php.

/* Always have wp_head() just before the closing
tag of your theme, or you will break many plugins, which
generally use this hook to add elements to such
as styles, scripts, and meta tags.

First, It is important to understand what the  wp_head function really does. It is an action hook for some WordPress plugins to insert custom code into the header. And these plugins could be a tracking code insertion plugin as well.

So, if you were to use a WordPress plugin to insert the tracking code instead of doing it manually, then what the plugin does is it hooks on to the wp_head and inserts the code into it. Depending on when the plugin is executed and the execution order, the code could end up either at the top, bottom, the middle or somewhere in between depending on how many plugins you have.

So, inserting the tracking code before wp_head would be as if your plugin (if you had one) attached and inserted first before any of the the other plugins executed. Putting the code after the wp_head would be as if the plugin was the last to execute and inserted the code at the bottom. Both of these cases run pretty well, without any issues.

It is also due to the fact that the Google Analytics tracking code itself is a self contained piece of code snippet which does not have any dependence on any other code on the page, so inserting it anywhere will actually work fine!

So, whether you want it before or after depends on what other plugins you have on the site that hooks onto the wp_head function and whether you want the GA code to load before or after it.

So, Why does Google recommend inserting it at the bottom of the head tag?

Google says it is for performance and speed. Putting it at the bottom is supposed to help load the page faster but the latest Google analytics tracking code fetches the script asynchronously. I am still not completely sold on why it should be at the absolute bottom of the header…and its perceived advantage in speed.

Also I suppose it is much easier and better to advise people to put the code at the end, it reduces the ambiguity in the documentation…..rather than explaining in detail how it cannot be at the very top of the head tag but can be anywhere in the head …etc. etc.

Since there is no guarantee on what other plugins you are using and how it works, I recommend that the tracking code be put before the wp_head.

By putting the code before the wp_head, if any of your other plugins fail, and the page does not fully render (say, due to blocking or an error in the plugin code)….there is a very good probability that the tracking beacon will be sent and counted. Whether this is a good thing or not, depends on you and the website or your expectation in how the stats are collected.

The advantage of putting the code after the wp_head and hence all other plugins, is that if any of them fails and the page is not rendered then the google analytics tracking code is not executed and it doesn’t count as a page view.

So, my suggestion is to place it before the wp_head  and as the asynchronous loading should not hinder the loading and displaying the rest of the page. Loading it as high as possible will also allow it to track better as even if the user closes the window/tab before the page completely loads, there is a possibility that the tracking beacon would still be sent.

Remember that we are just splitting hairs here. Putting it pretty much anywhere in the head will work just fine.

If you are trying to find the different options you have to insert the Google Analytics into a Wordpress blog, then check out the post on How to insert Google Analytics into WordPress.