Javascript Universal Analytics无法跟踪magento中的交易
我用的是MagentoCE1.7。最近我从google analytics迁移到universal analytics 迁移后,除事务数据外,其他详细信息都会被很好地跟踪 我在head.phtml中添加了以下脚本,用于universal analyticsJavascript Universal Analytics无法跟踪magento中的交易,javascript,magento,google-analytics,universal-analytics,Javascript,Magento,Google Analytics,Universal Analytics,我用的是MagentoCE1.7。最近我从google analytics迁移到universal analytics 迁移后,除事务数据外,其他详细信息都会被很好地跟踪 我在head.phtml中添加了以下脚本,用于universal analytics <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-2', 'mysite.com');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:send');
</script>
(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口,文档,“脚本”,“www.google-analytics.com/analytics.js”,“ga”);
ga(“创建”、“UA-XXXXXXX-2”、“mysite.com”);
ga(‘发送’、‘页面浏览’);
ga('require','ecommerce','ecommerce.js');
ga(“电子商务:发送”);
在管理方面,我也保存了通用分析跟踪代码
我做错了什么?为什么我不能跟踪事务数据?有人能帮上忙吗?嗨,我今天也遇到了同样的问题,我已经写了一个解决方案,但首先要删除您头脑中的自定义脚本。phtml
首先添加ga.phtml 您需要在模板文件夹中创建新文件或编辑默认文件:
MAGENTOROOT/app/design/frontend/YOURTHEME/template/googleanalytics/ga.phtml
并将其添加到将覆盖基本/默认Magento ga.phtml的文件中
<?php if (!Mage::helper('core/cookie')->isUserNotAllowSaveCookie()): ?>
<?php $accountId = Mage::getStoreConfig(Mage_GoogleAnalytics_Helper_Data::XML_PATH_ACCOUNT) ?>
<!-- BEGIN GOOGLE ANALYTICS CODEs -->
<script type="text/javascript">
//<![CDATA[
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
<?php echo $this->_getPageTrackingCode($accountId) ?>
<?php echo $this->_getOrdersTrackingCode() ?>
//]]>
</script>
<?php endif; ?>
并在代码/本地中创建新文件,该文件将覆盖代码/核心文件:
MAGENTOROOT/app/code/local/Mage/GoogleAnalytics/Block/Ga.php
在您创建的新文件中,修改这两个函数以匹配此代码:
\u getPageTrackingCode($accountId)
\u GetOrderStrackCode()
重要的是要注意Daniel的回答,cookie域设置不正确 更改此行:
ga('create', '".$this->jsQuoteEscape($accountId)."', '".$hostName."');
致:
它从您的Magento配置中正确插入cookie域,而不是使用未设置的$hostName值并导致空“”
如果您在子域上使用CDN,那么保存cookie流量就变得更加重要 我也有同样的问题。然后我安装了下面的扩展,现在一切都很好。
希望能有所帮助。您的帖子中没有交易或产品数据。您可以查看文档()以了解这应该是什么样子。然后使用magento管理面板在以下位置添加跟踪代码:系统>配置>销售|谷歌API |谷歌分析Cellent answer!祝贺只有一个问题:GoogleAnalytics/Model/Observer.php中是否也需要更新?InjectAnalyticsGoogleCheckOutlink方法仍在使用旧ga.js中的旧_gaq.push函数。非常好的答案,谢谢分享:)!工作得很有魅力。我需要GA Universal向我的Magento网站添加多个跟踪(),不得不对代码进行一些修改,但效果很好。@Dragos是的,这需要更改,但只有在使用Google Checkout处理付款时才需要。这非常有用。对于那些仍然使用Magento 1.3.x的人,我将此代码改编为旧的Magento核心模块代码,以将Magento 1.3.x的分析代码更新为Universal。Hi JaseC感谢您的输入,但出于某种原因,var_dump(Mage::getStoreConfig('web/cookie/cookie_domain',Mage::app()->getStore())返回字符串(0)”“您使用的是哪个版本的magento?我认为$hostName=$_SERVER['SERVER_NAME')会非常干净;“那是一个死链接。@乔恩,链接已经修复。干杯
protected function _getPageTrackingCode($accountId)
{
$pageName = trim($this->getPageName());
$optPageURL = '';
if ($pageName && preg_match('/^\/.*/i', $pageName)) {
$optPageURL = ", '{$this->jsQuoteEscape($pageName)}'";
}
// if you can think of better way to get the host name
// let me know in the comments.
$hostName = $_SERVER['SERVER_NAME'];
return "
ga('create', '".$this->jsQuoteEscape($accountId)."', 'auto');
ga('send', 'pageview' ".$optPageURL.");
";
}
protected function _getOrdersTrackingCode()
{
$orderIds = $this->getOrderIds();
if (empty($orderIds) || !is_array($orderIds)) {
return;
}
$collection = Mage::getResourceModel('sales/order_collection')
->addFieldToFilter('entity_id', array('in' => $orderIds))
;
$result = array("
// Transaction code...
ga('require', 'ecommerce', 'ecommerce.js');
");
foreach ($collection as $order) {
if ($order->getIsVirtual()) {
$address = $order->getBillingAddress();
} else {
$address = $order->getShippingAddress();
}
$result[] = "
ga('ecommerce:addTransaction', {
id: '".$order->getIncrementId()."', // Transaction ID
affiliation: '".$this->jsQuoteEscape(Mage::app()->getStore()->getFrontendName())."', // Affiliation or store name
revenue: '".$order->getBaseGrandTotal()."', // Grand Total
shipping: '".$order->getBaseShippingAmount()."', // Shipping cost
tax: '".$order->getBaseTaxAmount()."', // Tax
});
";
foreach ($order->getAllVisibleItems() as $item) {
$result[] = "
ga('ecommerce:addItem', {
id: '".$order->getIncrementId()."', // Transaction ID.
sku: '".$this->jsQuoteEscape($item->getSku())."', // SKU/code.
name: '".$this->jsQuoteEscape($item->getName())."', // Product name.
category: '', // Category or variation. there is no 'category' defined for the order item
price: '".$item->getBasePrice()."', // Unit price.
quantity: '".$item->getQtyOrdered()."' // Quantity.
});
";
}
$result[] = "ga('ecommerce:send');";
}
return implode("\n", $result);
}
ga('create', '".$this->jsQuoteEscape($accountId)."', '".$hostName."');
ga('create', '".$this->jsQuoteEscape($accountId)."', {'cookieDomain':'".Mage::getStoreConfig('web/cookie/cookie_domain', Mage::app()->getStore() )."'});