嗨我今天有同样的问题我已经写了一个解决方案,但首先删除你的 head.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; ?>
覆盖默认的 GoogleAnalytics 块
这不是最好的方法,但为简单起见,如果您想要更干净的解决方案,您需要创建模块并在那里添加重写代码,我将使用它。
好的首先复制这个文件的内容:
MAGENTOROOT/app/code/core/Mage/GoogleAnalytics/Block/Ga.php
并在代码/本地创建新文件,它将覆盖代码/核心之一:
MAGENTOROOT/app/code/local/Mage/GoogleAnalytics/Block/Ga.php
在您创建的新文件中,修改这两个函数以匹配此代码:
_getPageTrackingCode($accountId)
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.");
";
}
_getOrdersTrackingCode()
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);
}