LoginSignup
8
7

More than 1 year has passed since last update.

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

Last updated at Posted at 2014-09-06

Google Analytics 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用、スマートフォン用それぞれのテンプレートに設定します。

タグマネージャーでEコマースを使用する場合(おすすめ)

予め、以下クイックスタートガイドのタグを設定しておく必要があります
https://developers.google.com/tag-manager/quickstart

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

<script>
<!--{if $smarty.server.PHP_SELF == "`$smarty.const.ROOT_URLPATH`shopping/complete.php" && !empty($arrOrder) && !empty($arrOrderDetail)}-->
              dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
              dataLayer.push({
                event: "purchase",
                ecommerce: {
                  "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}-->
                  {
                     'item_id': '<!--{$orderDetail.product_code|h}-->',
                     'item_name': '<!--{$orderDetail.product_name|nl2br|strip:""|strip_tags:false|h}-->',
                     <!--{if $orderDetail.classcategory_name1 != ""}-->
                       'item_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>

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

予め、以下のグローバルサイトタグを設定しておく必要があります
https://developers.google.com/analytics/devguides/collection/app-web?hl=ja

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

<script>
<!--{if $smarty.server.PHP_SELF == "`$smarty.const.ROOT_URLPATH`shopping/complete.php" && !empty($arrOrder) && !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>

ユニバーサルアナリティクス用の設定です(旧方式)

        <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) && !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>

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7