Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?
@nanasess

EC-CUBE - Google Analytics eコマースタグの設定

ユニバーサルアナリティクス用の eコマースタグの設定です。

電車の中で書いたので、まだ動作確認していません。ご利用は自己責任でm(_ _)m

data/class/pages/shopping/LC_Page_Shopping_Complete.php

    /**
     * Page のアクション.
     *
     * @return void
     */
    public function action()
    {
        $this->arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
        $objPurchase = new SC_Helper_Purchase_Ex();
        // eコマースコンバージョンタグ用設定
        if (is_numeric($_SESSION['order_id'])) {
            $this->arrOrder = $objPurchase->getOrder($_SESSION['order_id']);
            $this->arrOrderDetail = $objPurchase->getOrderDetail($_SESSION['order_id']);
        }
    }

2.4系の場合は以下の処理でいけるはず

        // 2.4系の場合は以下 120行目付近
        $objQuery = new SC_Query();
        $arrOrder = $objQuery->select('*', 'dtb_order', 'order_id = ?', array($_SESSION['order_id']));
        $this->arrOrder = $arrOrder[0];
        $this->arrOrderDetail = $objQuery->select('*', 'dtb_order_detail', 'order_id = ?', array($_SESSION['order_id']));

data/Smarty/templates/xxxx/site_main.tpl

body 直下に配置します.
PC用、スマートフォン用それぞれのテンプレートに設定します。

        <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-X', 'auto');
          ga('require', 'ecommerce', 'ecommerce.js');
          ga('send', 'pageview');

          <!--{if $smarty.server.$PHP_SELF == "`$smarty.const.ROOT_URLPATH`shopping/complete.php" && !empty($arrOrder) 0 && !empty($arrOrderDetail)}-->
            ga('ecommerce:addTransaction', {
               'id': '<!--{$arrOrder.order_id|h}-->',              // Transaction ID. Required.
               'affiliation': '<!--{$arrSiteInfo.shop_name|h}-->', // Affiliation or store name.
               'revenue': '<!--{$arrOrder.payment_total|h}-->',    // Grand Total.
            });

            <!--{foreach from=$arrOrderDetail item=orderDetail name=orderDetail}-->
                ga('ecommerce:addItem', {
                   'id': '<!--{$arrOrder.order_id|h}-->',          // Transaction ID. Required.
                   'name': '<!--{$orderDetail.product_name|h}--><!--{if $orderDetail.classcategory_name1 != ""}--> / <!--{$orderDetail.classcategory_name1|h}--><!--{/if}--><!--{if $orderDetail.classcategory_name2 != ""}--> / <!--{$orderDetail.classcategory_name2|h}--><!--{/if}-->', // Product name. Required.
                   'sku': '<!--{$orderDetail.product_code|h}-->',  // SKU/code.
                   'price': '<!--{$orderDetail.price|h}-->',       // Unit price.
                   'quantity': '<!--{$orderDetail.quantity|h}-->'  // Quantity.
                });
            <!--{/foreach}-->
            ga('ecommerce:send');
          <!--{/if}-->
        </script>

gtags.js でEコマースを使用する場合

以下を data/Smarty/templates/xxxx/site_main.tpl に追加します

<script>
<!--{if $smarty.server.$PHP_SELF == "`$smarty.const.ROOT_URLPATH`shopping/complete.php" && !empty($arrOrder) 0 && !empty($arrOrderDetail)}-->
                gtag('event', 'purchase', {
                  "transaction_id": "<!--{$arrOrder.order_id|h}-->",
                  "affiliation": "<!--{$arrSiteInfo.shop_name|h}-->",
                  "value": <!--{$arrOrder.payment_total|h}-->,
                  "currency": "JPY",
                  "items": [
                  <!--{foreach from=$arrOrderDetail item=orderDetail name=orderDetail}-->
                  {
                     'id': '<!--{$orderDetail.product_code|h}-->',
                     'name': '<!--{$orderDetail.product_name|nl2br|strip:""|strip_tags:false|h}-->',
                     <!--{if $orderDetail.classcategory_name1 != ""}-->
                       'variant': '<!--{$orderDetail.classcategory_name1|h}--><!--{if $orderDetail.classcategory_name2 != ""}--> / <!--{$orderDetail.classcategory_name2|h}--><!--{/if}-->',
                     <!--{/if}-->
                     'price': '<!--{$orderDetail.price|h}-->',
                     'quantity': '<!--{$orderDetail.quantity|h}-->'
                  }<!--{if !$smarty.foreach.orderDetail.last}-->,<!--{/if}-->
                  <!--{/foreach}-->
                  ]
                }
            );
          <!--{/if}-->
</script>

6
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
nanasess
Emacs のアイコンを作った人です
ec-cube
日本No.1ECオープンソースのEC-CUBEのコミッターやユーザーのコミュニティです。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
6
Help us understand the problem. What is going on with this article?