6

我正在使用 magento CE 1.7。最近我从谷歌分析迁移到通用分析。

迁移后,除交易数据外,其他细节跟踪良好。

我在 head.phtml 中添加了以下脚本以进行通用分析。

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

在管理方面,我也保存了通用分析跟踪代码。

我究竟做错了什么?为什么我无法跟踪交易数据?有人可以帮忙吗?

4

3 回答 3

17

嗨我今天有同样的问题我已经写了一个解决方案,但首先删除你的 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);
}
于 2014-03-07T14:47:36.440 回答
0

重要的是要注意丹尼尔的回答 cookie 域设置不正确。

更改此行:

ga('create', '".$this->jsQuoteEscape($accountId)."', '".$hostName."');

至:

ga('create', '".$this->jsQuoteEscape($accountId)."', {'cookieDomain':'".Mage::getStoreConfig('web/cookie/cookie_domain', Mage::app()->getStore() )."'});

它从您的 Magento 配置正确插入 cookie 域,而不是使用未设置的 $hostName 值并导致空“”。

如果您在子域上使用 CDN,则节省该 cookie 流量变得更加重要。

于 2014-05-14T23:59:05.440 回答
0

我遇到过同样的问题。然后我安装了下面的扩展程序,现在一切正常。 https://magento.mdnsolutions.com/extensions/mdn-google-universal-analytics.html

希望它可以提供帮助。

于 2014-11-13T01:09:17.930 回答