Place Google Analytics code in HTML head tag in Maganto



Magento provides the Google API management through the admin panel, means you can add the Google Analytics code through the admin panel without editing the code files. But there is some problem with the Google Analytics code, If we are going to add the Google Analytics through the Maganto admin then it displays the Google Analytics code under the of the page HTML, whereas Google prefers it to add before ending of the HTML tag.

Magento provides the Google API management through the admin panel, means you can add the Google Analytics code through the admin panel without editing the code files. But there is some problem with the Google Analytics code, If we are going to add the Google Analytics through the Maganto admin then it displays the Google Analytics code under the <body> of the page HTML, whereas Google prefers it to add before ending of the HTML </head> tag. Click Here for the tutorial. This is very easy and you can move your Goggle Analytics code from the HTML <body> section to HTML </head> tag by following steps below:

Step 1. Open the googleanalytics.xml file in the editor of your choice for this file navigate to the app/design/frontend/default/your_theme_name/layout/googleanalytics.xml, If it is not here then navigate to the app/design/frontend/base/default/layout/googleanalytics.xml

Step 2. Search for the code below :

<!-- Mage_GoogleAnalytics -->
<reference name="after_body_start">
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />
</reference>

Step 3. Replace this code from the below code:

<!-- Mage_GoogleAnalytics -->
<reference name="ga_in_head">
<block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />
</reference>

Step 4. Now open the page.xml file in the editor of your choice for this file navigate to the app/design/frontend/default/your_theme_name/layout/page.xml, If it is not here then navigate to the app/design/frontend/base/default/layout/page.xml.

Step 5. Search for the code below :

<block type="page/html_head" name="head" as="head">

Step 6. Add this code block in it

<block type="core/text_list" name="ga_in_head" as="ga_in_head"/>

Step 7. it will look like as

<block type="page/html_head" name="head" as="head">
<block type="core/text_list" name="ga_in_head" as="ga_in_head"/>
...
</block>

You have done, Refresh all your cache and check it, now your Google Analytics code will display into the <head></head> tag.