【发布时间】: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个问题:
- 如何在默认跟踪代码之前添加我的
_setCustomVar函数? - 如何仅在产品页面上添加我的
_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>
问题在于,我在基本跟踪代码之后添加了此自定义变量,该代码默认添加在 <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 异步语法
【问题讨论】:
-
您是手动将 Google Analytics 代码添加到页眉/页脚,还是使用 magento Google Api ... siteground.com/tutorials/magento/magento_google_analytics.htm ?
-
我正在使用 Magento 核心分析模块,就像您发布的 siteground 链接中一样
标签: magento google-analytics magento-1.4