piwik 和magento结合,piwikmagento结合
piwik 和magento结合,piwikmagento结合
1.
在magento网站中传递购物车和订单信息,可以到magento插件库下载一个插件,但是这个插件对订单的信息的抓取,是在订单付款成功返回页面,
对于一些在第三方支付成功没有返回网站的,就会漏掉订单,造成不准确,我们需要在跳转之前就抓取过来
因此:
查阅官方资料:
http://piwik.org/docs/ecommerce-analytics/
2.方法:
在magento的piwik插件购物车已经实现,我们就用这个插件
这个插件用的是老版本的方式,不过新版的piwik还是支持
找到文件路径:\app\design\frontend\default-mongo\default\template\piwikanalytics\piwik.phtml
修改内容为:
<?php //echo $this->_getOrdersTrackingCode()?>
<?php //echo $this->_getProductPageview()?>
<?php //echo $this->_getCategoryPageview()?>
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
<?php
//购物车信息跟踪
$CurrentUrl = Mage::helper('core/url')->getCurrentUrl();
if(strstr($CurrentUrl,"http://www.tomtop.com/checkout/cart?")
||($CurrentUrl=="http://www.tomtop.com/checkout/cart")
||strstr($CurrentUrl,"http://www.tomtop.com:6081/checkout/cart?")
||($CurrentUrl=="http://www.tomtop.com:6081/checkout/cart")
){
echo $this->_getEcommerceCartUpdate();
}
?>
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://piwik.tomtop.com/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="http://piwik.tomtop.com/piwik.php?idsite=1" alt="" /></p></noscript>
<!-- End Piwik Code -->
app\code\community\PiwikMage\PiwikAnalytics\Block\Piwik.php的getEcommerceCartUpdate方法修改为如下:
protected function _getEcommerceCartUpdate()
{
$cart = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
foreach ($cart as $cartitem) {
//get category name
$product_id = $cartitem->product_id;
$_product = Mage::getModel('catalog/product')->load($product_id);
$cats = $_product->getCategoryIds();
$cate_str = "[";
if (isset($cats)) {
foreach($cats as $ca_id){
$cate = Mage::getModel('catalog/category')->load($ca_id);
$category_name = $cate->getName();
$cate_str .="'".$category_name."',";
}
}
$cate_str = substr($cate_str,0,strlen($cate_str)-1);
$cate_str .= "]";
$nameofproduct = $cartitem->getName();
$nameofproduct = str_replace('"', "", $nameofproduct);
if ($cartitem->getPrice() == 0 || $cartitem->getPrice() < 0.00001):
continue;
endif;
echo "_paq.push(['addEcommerceItem','".$cartitem->getSku()."','". $nameofproduct."',".$cate_str.",".$cartitem->getPrice().",". $cartitem->getQty()."]);";
echo "\n";
}
//total in cart
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
if ($grandTotal == 0) echo ''; else
echo "_paq.push(['trackEcommerceCartUpdate',".$grandTotal."]);";
echo "\n";
//echo "_paq.push(['trackPageView']);";
//echo "\n";
}
完成上面后,购物车的修改完毕
下面就是修改在点击跳转的时候抓取订单号:
重新Checkout\controllers\OnepageController.php这个方法:
将末尾代码修改为:
$this->getOnepage()->getQuote()->save();
/**
* when there is redirect to third party, we don't want to save order yet.
* we will save the order in return action.
*/
if (isset($redirectUrl)) {
$result['redirect'] = $redirectUrl;
}
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order')->load($lastOrderId);
$increment_id = $order->getIncrementId();
$result['order_id'] = $increment_id;
// $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
// $result['order_id'] = $orderId;
$result['order'] = $this->getPiwikOrderData($order);
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
添加方法:
public function getPiwikOrderData($order){
//$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$order_product_info = array();
foreach ($order->getAllVisibleItems() as $item) {
//get category name
$product_id = $item->product_id;
$_product = Mage::getModel('catalog/product')->load($product_id);
$cats = $_product->getCategoryIds();
$cate_str = "[";
if (isset($cats)) {
foreach($cats as $ca_id){
$cate = Mage::getModel('catalog/category')->load($ca_id);
$category_name = $cate->getName();
$category_name = str_replace("'","",$category_name);
$category_name = str_replace('"','',$category_name);
$cate_str .="\"".$category_name."\",";
}
}
$cate_str = substr($cate_str,0,strlen($cate_str)-1);
$cate_str .= "]";
if ($item->getQtyOrdered()) {
$qty = number_format($item->getQtyOrdered(), 0, '.', '');
} else {
$qty = '0';
}
$order_product_info[] = array(
"sku"=> $item->getSku(),
"product_name"=> $item->getName(),
// "category_name"=> $cate_str,
"category_name"=> "",
"price"=> $item->getBasePrice(),
"qty"=> $qty
);
}
if ($order->getGrandTotal()) {
$subtotal = $order->getGrandTotal() - $order->getShippingAmount() - $order->getShippingTaxAmount();
} else {
$subtotal = '0.00';
}
$order_info = array(
"increment_id"=> $order->getIncrementId(),
"base_grand_total"=> $order->getBaseGrandTotal(),
"subtotal"=> $subtotal,
"base_tax_amount"=> $order->getBaseTaxAmount(),
"base_shipping_amount"=> $order->getBaseShippingAmount()
);
return array("order_info"=>$order_info,"order_product_info"=>$order_product_info);
}
然后找到js文件:skin\frontend\default-mongo\default\js\opcheckout.js
在730行左右找到:
var Review = Class.create();
这个js对象的方法:nextStep: function(transport){
修改为如下:
nextStep: function(transport){
if (transport && transport.responseText) {
try{
response = eval('(' + transport.responseText + ')');
}
catch (e) {
response = {};
}
if (response.redirect) {
location.href = response.redirect;
//alert(response.order_id);
order = response.order;
order_info = order.order_info;
order_product_info = order.order_product_info;
for(x in order_product_info){
x = order_product_info[x];
if(x.sku){
_paq.push(['addEcommerceItem',
x.sku,
x.product_name,
x.category_name,
parseFloat(x.price),
parseInt(x.qty)
]);
//alert(x.sku+"##"+x.product_name+"##"+x.category_name+"##"+parseFloat(x.price)+"##"+parseInt(x.qty));
}
}
_paq.push(['trackEcommerceOrder',
order_info.increment_id,
parseFloat(order_info.base_grand_total),
parseFloat(order_info.subtotal),
parseFloat(order_info.base_tax_amount),
parseFloat(order_info.base_shipping_amount)
]);
//alert(order_info.increment_id+"##"+parseFloat(order_info.base_grand_total)+"##"+parseFloat(order_info.subtotal)+"##"+parseFloat(order_info.base_tax_amount)+"##"+parseFloat(order_info.base_shipping_amount));
_paq.push(['trackPageView']);
return;
}
if (response.success) {
this.isSuccess = true;
window.location=this.successUrl;
}
else{
var msg = response.error_messages;
if (typeof(msg)=='object') {
msg = msg.join("\n");
}
alert(msg);
}
if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
response.update_section.html.evalScripts();
}
if (response.goto_section) {
checkout.gotoSection(response.goto_section);
checkout.reloadProgressBlock();
}
}
},
到此已经完成,如果线上网站使用的是varnish配置,那么刷新varnish缓存
然后去piwik查看是否传递过去了订单,如果cron处理报表,那么报表不是即时的,需要等一下。
然后通过api,抓取piwik的日志数据,就有了完整的订单号信息
然后在通过api去核对订单状态,这样,就把营销系统对流量数据的完美打通。
评论暂时关闭