EC-CUBE2.11系でGoogle Analyticsのeコマーストラッキングを行う方法
あまり情報がなかったので、自分で試したのをメモ代わりに残しておきます。
classファイルの変更
/data/class/pages/shopping/LC_Page_Shopping_Complete.php
66行目にある
$this->arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
の下に下記を設定します。
$objPurchase = new SC_Helper_Purchase(); // 注文者データの取得 $arrOrder = $objPurchase->getOrder($_SESSION['order_id']); // 注文商品データの取得 $arrOrderDetail = $objPurchase->getOrderDetail($_SESSION['order_id']); $this->arrGAOrder = $arrOrder; // Smartyに渡す $this->arrGAOrderDetail = $arrOrderDetail; // Smartyに渡す
必ず、67行目にある
unset($_SESSION["order_id"]);
よりも前に挿入してください。
Smartyファイルの変更
/data/Smarty/templates/default/site_frame.tpl
</head>の前に下記を設定します。
注文完了した時にだけ、eコマースの処理を入れたいのでそれ以外の場合は通常のanalyticsタグが入るように設定します。
UAアカウントIDは自分のものに書き換えてください。
<!--{if $smarty.server.PHP_SELF == "`$smarty.const.ROOT_URLPATH`shopping/complete.php"}-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-********-*']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'<!--{$arrGAOrder.order_id}-->', // order ID - Required!!
'', // affiliation or store name
'<!--{$arrGAOrder.total}-->', // total - Required!!
'', // tax
'', // shipping
'<!--{$arrGAOrder.order_addr01}-->', // state or province
'<!--{$arrPref[$arrGAOrder.order_pref]}-->', // city
'', // country
]);
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
<!--{section name=cnt loop=$arrGAOrderDetail}-->
_gaq.push(['_addItem',
'<!--{$arrGAOrder.order_id}-->', // order ID - Required!!
'<!--{$arrGAOrderDetail[cnt].product_id}-->', // SKU/code - Required!!
'<!--{$arrGAOrderDetail[cnt].product_name}-->', // product name
'<!--{$arrGAOrderDetail[cnt].classcategory_name1}-->', // category or variation
'<!--{$arrGAOrderDetail[cnt].price}-->', // unit price - Required!!
'<!--{$arrGAOrderDetail[cnt].quantity}-->' // quantity - Required!!
]);
<!--{/section}-->
_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>
<!--{else}-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-********-*']);
_gaq.push(['_trackPageview']);
(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>
<!--{/if}-->
Analyticsの設定でeコマースサイトをオンにする
Google Analyticsのプロファイルから編集を選び、プロファイル設定へ行きます。
プロファイル設定のメインのプロファイル情報の編集から、
eコマースサイトの箇所で「はい、eコマースサイトです」を選択して設定を保存します。
以上で設定完了です。
2012.03.09追記
けたばを様からご指摘頂きまして、最初の条件分岐を修正しました。
けたばを様ありがとうございます!