【问题标题】:Custom variables on product details page in MagentoMagento 产品详细信息页面上的自定义变量
【发布时间】:2012-10-29 08:46:49
【问题描述】:

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

我正在尝试使用 _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个问题:

  1. 如何在默认跟踪代码之前添加我的_setCustomVar 函数?
  2. 如何仅在产品页面上添加我的_setCustomVar 功能?

原帖:

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

显然,这段代码应该只添加到产品详细信息页面,而不是列表、购物车或结帐页面。所以我尝试通过添加以下代码来编辑app/design/frontend/default/my_package/template/catalog/product 中的view.phtml 文件:

<script>
_gaq.push(['_setCustomVar',
    1,
    'View Product',
    '<?php echo $_helper->productAttribute($_product, $_product->getSku(), 'sku') ?>', 
    3]);
</script>

问题在于,我在基本跟踪代码之后添加了此自定义变量,该代码默认添加在 &lt;head&gt; 部分中,因此不会记录在 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 google-analytics magento-1.4


【解决方案1】:

这目前正在我们的一个 magento 网站上工作,如果您使用 Magento Admin Google API,那么您可以 (1) 创建一个自定义模块来扩展它或 (2) 确保(查看源代码时)@ 987654322@ 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 以获得更多帮助

【讨论】:

  • 这很酷,但我有 2 个问题:#1 您如何修改 GA 跟踪代码以包含 customVar? #2 这段代码不是显示在您的 magento 网站的每个页面上吗,例如 CMS 页面、类别页面、结帐、购物车等?
  • 通过创建扩展 GA 的自定义模块...阅读我更新的答案
  • 好的,但这不是在所有页面(包括非产品详细信息页面)上设置自定义变量吗?
  • 你不是说&lt;config&gt; XML 代码应该放在app/code/local/RWS/GoogleAnalytics/etc/config.xml 中吗?
【解决方案2】:

我已经设法通过修改默认的 GoogleAnalytics 模块使其工作:

app/code/core/Mage/GoogleAnalytics/Block 中复制Ga.php 文件,然后导航到/www/app/code/local/Mage/GoogleAnalytics/Block 并粘贴它(如果它们不存在,则创建必要的文件夹)。

在新的Ga.php_toHtml()函数中测试是否从Magento后端激活了Analytics,测试当前页面是否为产品详情页面:

$_product = Mage::registry('current_product');  
if($_product) {
        //output code with _setCustom var
    } else {
        //output normal tracking code
    }

带有自定义变量集的代码如下所示:

$this->addText('
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type=\"text/javascript\">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'UA-25272379-1\']);
_gaq.push([\'_setCustomVar\', 1, \'Product View\',\''.$_product->getSku().'\', 3]);
_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>
<!-- END GOOGLE ANALYTICS CODE -->
');

【讨论】:

    【解决方案3】:

    如果你使用谷歌分析的异步插入代码,无论你把这段代码放在哪里,它只会在页面加载完成时应用。 因此,您可以在之前或之后添加您的 _gaq.push(),只要它们内联在 HTML 代码中(非异步)

    异步代码如下:

    (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';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
    })();
    

    【讨论】:

    • 是的,我使用的是异步语法,但是我在头 sn-p 之后添加了我的 _gaq.push(),它没有将数据传输到 Analytics。我已经编辑了我的问题以包含我正在使用的异步代码。
    • 你确定周围的一切吗? _setAccount ? _setDomainName ?你在看 GA admin 的实时面板吗?如果您在之前手动添加代码:它有效吗?
    • 是的,它包括_setAccount(再次编辑问题)。我之前没有尝试添加它,因为我不知道该怎么做。
    • 禁用 GA 模块的输出,并在 firebug 的控制台(在 firefox 上)中自己执行 JS(复制过去的标签,重新排序并执行)
    • 在控制台中注意到如果我在异步代码之前添加我的_gaq.push(),我会收到一个 JS 错误 _gaq is not defined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2022-08-19
    相关资源
    最近更新 更多