How to add code after opening <body> tag in Magento?

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


General > Design > Footer > Miscellaneous HTML

This setting will add code just before closing body tag.

If you want to add code just after body tag you will need to us XML reference after_body_start

<block type="core/text_list" name="after_body_start" as="after_body_start" translate="label">
        <label>Page Top</label>
</block>

You can take reference of after_body_start and can able to add your code.

<reference name="after_body_start">
// your block and template goes here..
</reference>