将 GTM 添加到 Magento

Adding GTM to Magento

我想将 Google 标签管理器添加到 Magento,但我不想为此使用插件,我的问题是最佳做法是什么?我读过一些关于创建模块的内容,但也包括 body 标签内的 GTM,问题是我要找到加载每个页面正文的文件。

谢谢

向每个页面添加脚本块(或您可能需要的任何其他内容)非常简单,只要您可以编辑主题文件即可。您可以在主题中编辑 page.xml 文件。

magento\app\design\frontend\\\layout\page.xml

向“所有页面”块添加一个新块。在此块中,您将找到所有头脚本和 css。示例如下:

<default translate="label" module="page">
    <label>All Pages</label>
    <block type="page/html" name="root" output="toHtml" template="page/1column.phtml">

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

            <action method="addJs"><script>prototype/prototype.js</script></action>

            <block type="page/js_cookie" name="js_cookies" template="page/js/cookie.phtml"/>

            <action method="addItem"><type>skin_js</type><name>js/app.js</name></action>

            <action method="addItem"><type>skin_js</type><name>js/script.min.js</name></action>

            <!-- Add stylesheets with media queries for use by modern browsers -->
            <action method="addItem"><type>skin_css</type><name>css/styles.css</name><params/></action>

            <!-- Sets viewport meta tag using text block -->
            <block type="core/text" name="head.viewport">
                <action method="setText"><text><![CDATA[<meta name="viewport" content="initial-scale=1.0, width=device-width, maximum-scale=1, user-scalable=no" />]]>&#10;</text></action>
            </block>

        </block>

        <!-- add GTM block here -->
        <block type="core/template" name="gtm" as="after_body_start" template="page/html/gtm.phtml" />

    </block>

    <block type="core/profiler" output="toHtml" name="core_profiler"/>
</default>

要添加的块将是这样的(请参阅上面代码中的块)

<block type="core/template" name="gtm" as="after_body_start" template="page/html/gtm.phtml" />

然后您只需要在您的主题中创建一个新文件:

magento\app\design\frontend\\\template\page\html\gtm.phtml

将您的 GTM 代码片段粘贴到 gtm.phtml 文件中。

在您的页面模板中,例如 page/1column.phtml,您会在开始正文标记之后找到 php 的片段:

<?php echo $this->getChildHtml('after_body_start') ?>

这是您的 GTM 代码将添加到所有页面的位置。