Page 1 of 1

Google Analytics plugin - emit in footer, not <head>

Posted: Wed Nov 05, 2008 4:09 pm
by Don Chambers
Looking at the google analytics plugin, I see that is uses the frontend_header hook, and not the frontend_footer hook.... I think it should use frontend_footer. I modified my own copy of the plugin, and it works fine.

The reason for this is, according to google:

Code: Select all

Copy and paste the code segment into the bottom of your content, immediately before the </body> tag of each page you are planning to track.
I did see some contradictory information here: http://developer.yahoo.com/performance/ ... #js_bottom

Code: Select all

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page.
Not sure what document.write has to do with it, and the google analytics code does use document.write, but it seems to work fine placed in the footer hook.

I could commit the change myself, but my cvs client never seems to want to cooperate. I'm suggesting someone else make this change unless a really good reason can be provided why it should not be there. Same goes for any other plugins emitting in the <head> element that really do not need to be there.

I leave the code running for a few hours just to make sure GA is still tracking the site I made the change on, then I will report back here...

Posted: Thu Nov 06, 2008 4:48 am
by Don Chambers
My local revision of this plugin to emit in frontend_footer is working fine. I can spend countless hours trying to resolve my issues with cvs, but if someone wants to make the change for me, I highly endorse the modification unless there is something I am missing.

I do not use a lot of plugins that emit script on the page, but if there is anything else that could be code shifted to the footer, I suggest doing so.

Posted: Thu Nov 06, 2008 11:05 am
by kleinerChemiker

Posted: Thu Nov 06, 2008 2:24 pm
by Don Chambers
So, because the plugin can track other things like file downloads and external links, it has to essentially remain in the <head> because it has to be called before any of those other types of tracking code. I have never used those, so I was unaware of the problem.
Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.
Perhaps the plugin could emit in the header if those options are selected, and footer if they are not? Seems to me it would be nice to get the page displayed before trying to get the script from google in case of google delays or outages.

Posted: Thu Nov 06, 2008 3:09 pm
by garvinhicking
Hi!

Usually the JS in the head should make LESS trouble in rendering the page than having it before </body>. <head> scripts are usually loaded by the browser parallely to rendering the <body> section, stacking all required JS calls. If a <script> tag occurs within <body>, THAT usually slows rendering down, and if it's in the head, it can stilld distinctly render the body...so actually, there should only be disadvantages in placing the code into the body, if I understand it correctly...?

Regards,
Garvin

Posted: Thu Nov 06, 2008 4:33 pm
by Don Chambers
So <head> scripts do NOT block parallel downloads, but <body> scripts do? I thought they all did.

Posted: Thu Nov 06, 2008 4:45 pm
by kleinerChemiker
Don Chambers wrote:So, because the plugin can track other things like file downloads and external links, it has to essentially remain in the <head> because it has to be called before any of those other types of tracking code. I have never used those, so I was unaware of the problem.
Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.
Perhaps the plugin could emit in the header if those options are selected, and footer if they are not? Seems to me it would be nice to get the page displayed before trying to get the script from google in case of google delays or outages.
If you don't use these options, you could simply use an html nugget or copy the analytics code into your template and save cpu time ;)