Magento: Place Googly Analytics code just before </head>

Magento: Place Googly Analytics code just before </head>

October 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Place Googly Analytics code just before </head>

Solving Google Analytics Placement Issues in Magento

Are you struggling to place your Google Analytics code just before the closing </head> tag in your Magento store? You’re not alone! Many developers face this common challenge, but with the right approach, it’s a straightforward fix. Let’s dive into the issue and find a solution that works for you.

The Common Approach: Layout XML Files

Most tutorials suggest using layout XML files to insert the Google Analytics code. This involves creating two specific files:

  1. A custom layout file in your theme folder (app/design/frontend/YOURPACKAGE/YOURTHEME/layout/googleanalytics.xml)

  2. Editing or referencing an existing layout file like page.xml

The idea is to add a reference to the before_head_end block and insert the Google Analytics code there.

The Existing Code Structure: What’s Wrong?

Here’s what your current code structure might look like, following the common approach:

app/design/frontend/YOURPACKAGE/YOURTHEME/layout/googleanalytics.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <!-- Mage_GoogleAnalytics -->
        <reference name="before_head_end">
            <block type="googleanalytics/ga" name="google_analytics" as="google_analytics" template="googleanalytics/ga.phtml" after="-" />
        </reference>
    </default>
</layout>

app/design/frontend/base/default/layout/page.xml

<layout version="0.1.0">
    <default translate="label" module="page">
        ...
        <block type="core/text_list" name="before_head_end" as="before_head_end" after="-" />
    </default>
    ...
</layout>

Why the Code Isn’t Working

The issue lies in how Magento processes layout files and block references. The after attribute you’ve set to - tells Magento to insert the Google Analytics code before all other content, but it doesn’t specify a particular point within the head section.

This is why you’re seeing inline JavaScript codes appearing after your Google Analytics snippet—other scripts are being inserted by default blocks that are also configured to go “after” the before_head_end block.

The Correct Approach: Using a Specific Block

To solve this, we need to use a specific block within the head section where you want to insert the Google Analytics code. This is done in your theme’s page.xml file:

  1. Locate the <block type="core/text_list" name="head.scripts" as="scripts" /> line within the default container.

  2. Add your Google Analytics block immediately after this line, like so:

<layout version="0.1.0">
    <default translate="label" module="page">
        ...
        <!-- Additional blocks go here -->
        <block type="core/text_list" name="head.scripts" as="scripts" />
        <reference name="head.scripts">
            <block type="googleanalytics/ga" after="-" />
        </reference>
    </default>
    ...
</layout>

The Final Touches

Remember to clear your Magento cache and update your Google Analytics code within the ga.phtml template file located in app/code/core/Mage/GoogleAnalytics/Block/Ga.php. This should ensure that your analytics script is placed exactly where you want it—just before the closing </head> tag.

Conclusion

Placing Google Analytics code at a specific point in Magento requires understanding how layout XML files and block references work together. By following this approach, you’ll have complete control over when and where your analytics script appears within your store’s head section.

Keep exploring LaravelCompany.com for more tutorials and solutions to common development challenges.

Extensions Themes Errors

Our network