2012-10-29 103 views
2

UPDATE: 希望这是问题的一个更好的解释:自定义变量

我想对产品SKU通过我的产品的详细信息页面,谷歌Analytics(分析)使用_setCustomVar上。 我在Magento的1.4.0.1运行和我的分析异步代码是由<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, 但有默认的Google分析模块在此不支持。此问题有两个问题:

  1. 如何在默认跟踪代码之前添加我的_setCustomVar函数?
  2. 我如何可以加我_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>部分,因此它不会记录在Google Analytics中。

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

这是通过分析我提供异步代码:

<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和分析异步语法

+0

您手动添加谷歌Analytics(分析)代码,您的页眉/页脚,或者你使用谷歌的Magento阿比... http://www.siteground.com/tutorials/magento/magento_google_analytics.htm ? –

+0

我使用了Magento的核心分析模块就像你贴 – Bogdan

回答

1

我已经成功地使其通过修改默认的Google分析模块的工作:

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

Ga.php

,在测试后_toHtml()功能,如果Analytics(分析)从Magento的后台激活,测试,如果当前页面是产品详细信息页:

$_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 --> 
'); 
0

谷歌分析,无论身在何处,你把这个代码的asynchrnous插入代码,它只会在页面完成加载应用。 所以,你可以的前后,只要他们在HTML代码(不是异步)

异步代码看起来就像是在线添加_gaq.push()

(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); 
})(); 
+0

是的,我使用的是异步语法的siteground链接,但我已经添加了我的'_gaq.push()'头段后,它不是获取数据分析。 我编辑了我的问题以包含我正在使用的异步代码。 – Bogdan

+0

你确定所有在附近吗? _setAccount? _setDomainName?你在观看GA管理员的实时面板吗?如果您手动添加代码BEFORE:它工作吗? –

+0

是的,它包含'_setAccount'(再次编辑问题)。我之前没有尝试过添加它,因为我不知道该怎么做。 – Bogdan

2

这目前正在我们的一个Magento的网站,如果你使用的是Magento管理谷歌的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> 

以创建自定义模块

在应用程序/代码

/本地/ MageIgniter/Google分析的/ 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> 

创建/应用/代码/本地/ MageIgniter/Google分析/砌块/ 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更多的帮助

+0

这很酷,但我有两个问题:#1你是如何修改GA跟踪代码来包含customVar的? #2这个代码不是显示在你的magento网站的每一页上,例如CMS页面,分类页面,结账,购物车等? – Bogdan

+0

通过创建扩展GA的自定义模块...阅读我更新的答案 –

+0

好的,但不是在所有页面上设置自定义变量(包括非产品详细信息)? – Bogdan