Magento 2 – Add Google Tag Manager Code Inside Head and Body

google-tag-managerheadmagento2

I have a question regarding magento. I would like to know where the head and body are located. I want to do this because i would like to add a Google tag manager code inside it. I can somehow not seem to find where it is located.

Thanks in advance.

Best Answer

You can add JS files like this:

duplicate the file:

app/code/Magento/Theme/view/frontend/layout/default_head_blocks.xml

to

app/code/*your_vendor*/*your_theme*/Magento_Theme/layout/default_head_blocks.xml

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <!-- Add local resources -->
        <css src="css/my-styles.css"/>

        <!-- The following two ways to add local JavaScript files are equal -->
        <script src="Magento_Catalog::js/sample1.js"/>
        <link src="js/sample.js"/>

        <!-- Add external resources -->
        <css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css" src_type="url" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" src_type="url" />
        <link src="http://fonts.googleapis.com/css?family=Montserrat" src_type="url" /> 
    </head>
</page>

for more informtation:

http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html

EDIT:

To add block in content to all pages: https://magento.stackexchange.com/a/139772/48355