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

Creating and modifying plugins.
Post Reply
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

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

Post 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...
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post 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.
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post 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.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

So <head> scripts do NOT block parallel downloads, but <body> scripts do? I thought they all did.
=Don=
kleinerChemiker
Regular
Posts: 765
Joined: Tue Oct 17, 2006 2:36 pm
Location: Vienna/Austria
Contact:

Post 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 ;)
Post Reply