This is a quick post in response to Jeyhun’s question over at the Magento Forums.
Mage GoogleAnalytics code is placed after </head>. Google suggests inserting the asynchronous snippet just before . See http://www.google.com/support/analytics/bin/answer.py?answer=174090 for more.
A quick and dirty method is to go into your main page template files under
app/design/frontend/YOURPACKAGE/YOURTHEME/template/page
and change this portion
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>">
<head>
<?php echo $this->getChildHtml('head') ?>
</head>
<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<?php echo $this->getChildHtml('after_body_start') ?>
to this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>">
<head>
<?php echo $this->getChildHtml('head') ?>
<?php echo $this->getChildHtml('after_body_start') ?>
</head>
<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<div class="wrapper">
This has the downside that if you have any other code relying on being added after the body start using this block it will break.
The better method is to change
app/design/frontend/YOURPACKAGE/YOURTHEME/layout/googleanalytics.xml
from
<!-- Mage_GoogleAnalytics -->
<reference name="after_body_start">
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />
</reference>
to
<!-- Mage_GoogleAnalytics -->
<reference name="before_head_end">
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />
</reference>
and create this block in
app/design/frontend/YOURPACKAGE/YOURTHEME/layout/page.xml
<block type="core/text_list" name="before_head_end" as="before_head_end"/>
Add it here
<block type="page/html_head" name="head" as="head">
<block type="core/text_list" name="before_head_end" as="before_head_end"/>
...
</block>
This will get you 90% there. You still might have some additional code appear after the Google Analytics block and before the
end. To rectify this go intoapp/design/frontend/YOURPACKAGE/YOURTHEME/template/page/html/head.phtml
and move
<?php echo $this->getChildHtml() ?>
to the end of the file.
Originally published on magebase.com. Copyright © 2010 Magebase - All Rights Reserved.




Proud members of the 









Really helpful… thanks a lot !