Magento产品详细信息页面上的自定义变量 [英] Custom variables on product details page in Magento

查看:104
本文介绍了Magento产品详细信息页面上的自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


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

 < 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]); 

根据分析文档,为了记录自定义变量,<$ c $必须在 _trackPageView
之前调用c> _setCustomVar ,但默认的GoogleAnalytics模块中不支持此功能。这个问题有两个问题:


  1. 如何添加我的 _setCustomVar 函数在缺省跟踪代码之前?

  2. 如何在产品页面上添加我的 _setCustomVar 函数?






原文:



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



显然,这段代码只能添加到产品详细信息页面,而不能添加到列表,购物车或结帐页面。所以我试着编辑 app / design / frontend / default / my_package / template / catalog / product 中的 view.phtml 文件$ c>加入以下代码片段:

 < 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 中的Analytics模块更改核心文件,但我认为解决方案可能在那里。
如何添加设置自定义变量的代码片段,使其出现在基本跟踪代码中BEFORE _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>

来自这里 注意:我使用的是Magento 1.4。 0.1和Analytics Asynchronous Syntax

解决方案

我设法通过修改默认的GoogleAnalytics模块来实现它:


$ b $ app / code / core / Mage / GoogleAnalytics / Block 复制 Ga.php 文件,然后导航至 / www / app / code / local / Mage / GoogleAnalytics / Block 并粘贴(如果不存在则创建必要的文件夹)在 _toHtml()中添加新的 Ga.php 中的

code>函数在测试后是否从Magento后端激活分析,测试当前页面是产品详细信息页面:

  $ _ product = Mage :: registry('current_product'); 
if($ _ product){
//带有_setCustom的输出代码var
} else {
//输出正常跟踪代码
}

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

  $ 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-analyti cs.com/ga.js\\';
var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga,s);
} )();
< / script>
<! - END GOOGLE ANALYTICS CODE - >
');


UPDATE: Hopefully this is a better explanation of the problem:

I'm trying to pass on the product SKU on my product details page to Google Analytics using _setCustomVar. I'm running on Magento 1.4.0.1 and my Analytics async code is inserted by the default GA module in the <head> section and it looks like this:

<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>

The custom variable I'm trying to add has this syntax:

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

According to the Analytic documentation, in order for the custom variable to be recorded, the _setCustomVar must be called BEFORE the _trackPageView, but there's no support for this in the default GoogleAnalytics module. There are 2 issues to this problem:

  1. How can I add my _setCustomVar function BEFORE the default tracking code?
  2. How can I add my _setCustomVar function ONLY on product pages?


Original post:

I'm trying to store the SKU of the product being viewed by a visitor in a Analytics Custom Variable. The syntax for this is _gaq.push(['_setCustomVar',3,'View Product','SKU12345',2]);.

Obviously this snippet of code should be added only to the product detail pages, and not to the list, cart, or checkout pages. So I've tried editing the view.phtml file in app/design/frontend/default/my_package/template/catalog/product by adding the following piece of code:

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

The problem is that the I'm adding this custom variable AFTER the basic tracking code, which is added by default in the <head> section, so it doesn't get recorded in Analytics.

I tried to avoid altering the core files with the Analytics module in app/code/core/Mage/GoogleAnalytics/Block/Ga.php, but I think the solution may lay there. How can I add the piece of code that sets the custom variable so that it appears within the basic tracking code BEFORE _gaq.push(['_trackPageview']);?

This is my async code provided by 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>

Idea from here

Note: I'm using Magento 1.4.0.1 and the Analytics Asynchronous Syntax

解决方案

I've managed to make it work by modifying the default GoogleAnalytics module:

In app/code/core/Mage/GoogleAnalytics/Block copy the Ga.php file, then navigate to /www/app/code/local/Mage/GoogleAnalytics/Block and paste it (create the necessary folders if they don't exist).

in the new Ga.php, in the _toHtml() function after testing if Analytics is activated from the Magento backend, test if the current page is a product details page:

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

The code with the custom variable set looks like this:

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

这篇关于Magento产品详细信息页面上的自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆