服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

Magento添加Google统计转换代码

时间:2016-01-15 08:23来源: 作者: 点击:
在magento添加Google analytics 统计代码一般是加在订单成功页面.上面可以用Google Analytics进行转换率统计分析非常方便好使。 具体操作如下。下面有两种一种是商品统计,一种是订单金额统

在magento添加Google analytics 统计代码一般是加在订单成功页面.上面可以用Google Analytics进行转换率统计分析非常方便好使。

具体操作如下。下面有两种一种是商品统计,一种是订单金额统计

需要修改的文件为: app/design/frontend/default/你的模板/template/checkout/success.phtml

商品统计代码如下:

将UA-XXXXXXX-X替换成你的GA号码

<?php
$order1 = Mage::getModel(‘sales/order’)->load(Mage::getSingleton(‘checkout/session’)->getLastOrderId());

$subtotal = $order1->getSubtotal();
$order_id = $order1->getId(); //the id of the order
//$order->getIncrementId();//the increment id of the order
$gtotal = $order1->getGrandTotal();//grand total of the order

//print_r($order->getBillingAddress()->getData());
$address = $order1->getBillingAddress()->getData();
$city = $address['city'];
$state = $address['region'];
$country = $address['country_id'];
?>
<script type=”text/javascript”>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'<?php echo $order_id; ?>', // order ID - required
'Aekk Website Store', // affiliation or store name
'<?php echo $gtotal;?>', // total - required
'0', // tax
'0', // shipping
'<?php echo $city;?>', // city
'<?php echo $state;?>', // state or province
'<?php echo $country;?>' // country
]);
<?php

function getCategoryName($cats){
$catlist=array();
foreach ($cats as $category_id) {
$_cat = Mage::getModel(‘catalog/category’)->load($category_id) ;
$catlist[]= $_cat->getName();
}
return end($catlist) ;
}

$order_items = $order1->getAllItems();
$itemcount=count($order_items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($order_items as $key => $item)
{
if($item->getPrice()>0){

$cat_ids=Mage::getModel(“catalog/product”)->load( $item->getProductId())->getCategoryIds();
$CategoryName=getCategoryName($cat_ids); //get 分类名称

?>
_gaq.push(['_addItem',
'<?php echo $order_id; ?>', // order ID - required
'<?php echo $item->getSku(); ?>', // SKU/code - required
'<?php echo $item->getName(); ?>', // product name
'<?php echo $CategoryName; ?>', // category or variation
'<?php echo $item->getPrice(); ?>', // unit price - required
'<?php echo $item->getQtyToInvoice(); ?>', // quantity - required
'currencyCode': '<?php echo $order1->getOrderCurrencyCode();?>' // Order Currency Code

]);
<?php
}
}
?>
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(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>

订单金额统计代码如下:

<!– Google Code for &#19979;&#21333; Conversion Page –>
<script type=”text/javascript”>
/* <![CDATA[ */
var google_conversion_id = 0000000; //转换ID
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "m_CBCOeOnwUQgbio1wM";//转换标签
var google_conversion_value = 100;//金额
/* ]]> */
</script>
<script type=”text/javascript” src=”https://www.googleadservices.com/pagead/conversion.js”>
</script>
<noscript>
<div style=”display:inline;”>
<img height=”1″ width=”1″ style=”border-style:none;” alt=”” src=”https://www.googleadservices.com/pagead/conversion/转换ID/?value=金额 &amp;label=转换标签&amp;guid=ON&amp;script=0″/>
</div>
</noscript>

(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容