How to Add Code After Opening Tag in Magento

javascriptmagento-1.9

I have a recently found a couple of tags that recommend install the code directly beneath the opening tag. This is the recommended install location for LiveEngage Tag and Google Tagmanager.

You can add code easily before the closing tag under General > Design > Footer > Miscellaneous HTML

How can we achieve the same functionality immediately after the opening tag?

Best Answer

I would recommend the following approach for adding third party scripts in Magento:

1) Open your theme layout update file(preferably local.xml)

2) Add the following xml code:

<default>
    <reference name="after_body_start">
        <block type="core/template" name="after_body_start_script" template="page/html/scripts/after_body_start.phtml" />
    </reference>
    <reference name="before_body_end">
        <block type="core/template" name="before_body_end_script" template="page/html/scripts/before_body_end.phtml" />
    </reference>
</default>

3) Now you can add the relevant scripts to the the files page/html/scripts/after_body_start.phtml
or
page/html/scripts/before_body_end.phtml