gpt4 book ai didi

magento - Magento 产品详细信息页面上的自定义变量

转载 作者:行者123 更新时间:2023-12-03 15:48:56 25 4
gpt4 key购买 nike

更新:
希望这是对问题的更好解释:

我正在尝试使用 _setCustomVar 将我的产品详细信息页面上的产品 SKU 传递给 Google Analytics .
我在 Magento 1.4.0.1 上运行,我的 Analytics 异步代码由 <head> 中的默认 GA 模块插入。部分,它看起来像这样:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

我尝试添加的自定义变量具有以下语法:
_gaq.push(['_setCustomVar',1,'View Product','<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>',3]);

根据分析文档,为了记录自定义变量, _setCustomVar必须在 _trackPageView 之前调用,
但在默认的 GoogleAnalytics 模块中不支持此功能。这个问题有2个问题:
  • 如何添加我的 _setCustomVar默认跟踪代码之前的功能?
  • 如何添加我的 _setCustomVar仅在产品页面上起作用?


  • 原帖:

    我正在尝试将访问者正在查看的产品的 SKU 存储在 Analytics 自定义变量中。其语法为 _gaq.push(['_setCustomVar',3,'View Product','SKU12345',2]); .

    显然,这段代码应该只添加到产品详细信息页面,而不是列表、购物车或结帐页面。所以我尝试编辑 view.phtml文件在 app/design/frontend/default/my_package/template/catalog/product通过添加以下代码:
    <script>
    _gaq.push(['_setCustomVar',
    1,
    'View Product',
    '<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>',
    3]);
    </script>

    问题是我在基本跟踪代码之后添加了这个自定义变量,它默认添加到 <head> 中。部分,因此它不会记录在 Analytics 中。

    我试图避免使用 app/code/core/Mage/GoogleAnalytics/Block/Ga.php 中的 Analytics 模块更改核心文件,但我认为解决方案可能就在那里。
    如何添加设置自定义变量的代码,使其在 _gaq.push(['_trackPageview']); 之前出现在基本跟踪代码中?

    这是 Analytics 提供的我的异步代码:
    <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
    _gaq.push(['_trackPageview']);

    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

    </script>

    来自 here 的想法

    注意:我正在使用 Magento 1.4.0.1 和 Analytics 异步语法

    最佳答案

    这目前正在我们的一个 magento 站点上运行,如果您使用 Magento Admin Google API,那么您可以 (1) 创建自定义模块来扩展它或 (2) 确保(查看源代码时)<script>_gaq.push(['_setCustomVar... javascript 低于 var _gaq = _gaq || [];代码块

    <script type="text/javascript">
    //<![CDATA[
    var _gaq = _gaq || [];

    _gaq.push(['_setAccount', 'UA-xxxxxxx-3']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_setCustomVar', '1', 'awe2', '<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>', '1']);

    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

    //]]>
    </script>

    创建自定义模块

    在 app/code/local/MageIgniter/GoogleAnalytics/etc/config.xml
      <config>
    <modules>
    <MageIgniter_GoogleAnalytics>
    <version>0.1.0</version>
    </MageIgniter_GoogleAnalytics>
    </modules>
    <global>
    <blocks>
    <googleanalytics>
    <rewrite>
    <ga>MageIgniter_GoogleAnalytics_Block_Ga</ga>
    </rewrite>
    </googleanalytics>
    </blocks>
    </global>
    </config>

    在/app/code/local/MageIgniter/GoogleAnalytics/Block/Ga.php 中创建
        class MageIgniter_GoogleAnalytics_Block_Ga extends Mage_GoogleAnalytics_Block_Ga
    {

    private $remId = NULL;
    private $conversionId = NULL;
    private $conversionLabel = NULL;


    public function getPageName()
    {
    return $this->_getData('page_name');
    }


    protected function _getPageTrackingCode($accountId)
    {
    $pageName = trim($this->getPageName());
    $optPageURL = '';
    if ($pageName && preg_match('/^\/.*/i', $pageName)) {
    $optPageURL = ", '{$this->jsQuoteEscape($pageName)}'";
    }
    return "
    _gaq.push(['_setAccount', '{$this->jsQuoteEscape($accountId)}']);
    _gaq.push(['_trackPageview'{$optPageURL}]);
    " . $this->getProductSku();
    }

    ......

    public function getProductSku(){
    if($product = Mage::registry('current_product')){
    return sprintf("_gaq.push(['_setCustomVar', '%s', '%s', '%s', '%s']);",
    1,
    'Sku',
    $product->getSku(),
    1
    ) . "\n";
    }

    return '';
    }

    ........
    }

    有关更多帮助,请参阅/app/code/core/Mage/GoogleAnalytics/Block/Ga.php

    关于magento - Magento 产品详细信息页面上的自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13118321/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com