Skip to content
Advertisement

Google Analytics – Can I put script in footer?

I want to add google analytics to our website and have read some conflicting info about where to put the script tag.

Google says to put it before the closing </ head> tag: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html

The way our site is designed, this would mean making an edit to every page. It’s not that big of a deal if I need to do this, however, our site also uses header and footer include files.

These header & footer files have html code in them and fall just inside the <body> and </body> tags once the page is loaded.

It would be so much easier to just add the script to the header or the footer file. I’d just paste it in there once and it would be serving up the code on every page.

My question is: Can I do this? Can I move the script snippet inside footer include file even though it’s not before the </head> tag as google suggests?

Am I asking for problems if i do it this way?

Thanks!!

Advertisement

Answer

You can.

The only difference is that if you put in the “footer” (i.e. just before the </body> tag), the browser will first load the DOM (i.e. everything up to the script tag) and then start to load the script. If you put it in the header, it will try to load the script either before it loads the rest of the DOM or while it is loading it (depending on the browser).

You might feel a difference only if you have a huge page with a lot of elements, but the only difference really would be that the script would be loaded later, so if someone aborts a load of the page, the script might not get executed.

Thus, Google recommends to place it in the <head> and if you can, you should, however you also can put it somewhere else if that makes life easier.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement