UPDATE:希望这是对问题的更好解释:
我正在尝试使用以下方法将产品详细信息页面上的产品 SKU 传递给 Google Analytics:_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]);
根据分析文档,为了记录自定义变量,_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 中。
我试图避免使用 Analytics 模块更改核心文件app/code/core/Mage/GoogleAnalytics/Block/Ga.php
,但我认为解决方案可能就在那里。
如何添加设置自定义变量的代码片段,以便它出现在基本跟踪代码之前_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 http://www.lunametrics.com/blog/2012/08/28/20-ways-use-custom-variables/
注意:我使用 Magento 1.4.0.1 和 Analytics 异步语法