app/template/million01/Block/ecommerce_gatag.twig line 1

Open in your IDE?
  1. {% if app.request.get('_route') == 'product_list' %}
  2.     {# 商品一覧 #}
  3.     <script>
  4.         dataLayer.push({ecommerce: null});
  5.         // GA4
  6.         dataLayer.push({
  7.             'event': 'view_item_list',
  8.             'ecommerce': {
  9.                 'items': [
  10.                     {% for Product in pagination %}
  11.                     {
  12.                         'item_name': '{{ Product.name }}',
  13.                         'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  14.                         'price': '{{ Product.getPrice02IncTaxMin }}',
  15.                         'item_category': '{% for ProductCategory in Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  16.                     }{% if not loop.last %},{% endif %}
  17.                     {% endfor %}
  18.                 ]
  19.             }
  20.         });
  21.         // UA
  22.         dataLayer.push({
  23.             'ecommerce': {
  24.                 'currencyCode': 'JPY',
  25.                 'impressions': [
  26.                     {% for Product in pagination %}
  27.                     {
  28.                         'name': '{{ Product.name }}',
  29.                         'id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  30.                         'price': '{{ Product.getPrice02IncTaxMin }}',
  31.                         'category': '{% for ProductCategory in Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  32.                     }{% if not loop.last %},{% endif %}
  33.                     {% endfor %}
  34.                 ]
  35.             }
  36.         });
  37.     </script>
  38. {% elseif app.request.get('_route') == 'product_detail' %}
  39.     {# 商品詳細 #}
  40.     <script>
  41.         // Measure a view of product details. This example assumes the detail view occurs on pageload,
  42.         dataLayer.push({ecommerce: null});  // Clear the previous ecommerce object.
  43.         // GA4
  44.         dataLayer.push({
  45.             'event': 'view_item',
  46.             'ecommerce': {
  47.                 'items': [{
  48.                     'item_name': '{{ Product.name }}',
  49.                     'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  50.                     'price': '{{ Product.getPrice02IncTaxMin }}',
  51.                     'item_category': '{% for ProductCategory in Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  52.                     'quantity': '1'
  53.                 }]
  54.             }
  55.         });
  56.         // UA
  57.         dataLayer.push({
  58.             'ecommerce': {
  59.                 'detail': {
  60.                     'products': [{
  61.                         'name': '{{ Product.name }}',         // Name or ID is required.
  62.                         'id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  63.                         'price': '{{ Product.getPrice02IncTaxMin }}',
  64.                         'category': '{% for ProductCategory in Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  65.                     }]
  66.                 }
  67.             }
  68.         });
  69.     </script>
  70. {% elseif app.request.get('_route') == 'cart' %}
  71.     {# カートに追加 #}
  72.     <script>
  73.         // Measure when a product is added to a shopping cart
  74.         dataLayer.push({ecommerce: null});  // Clear the previous ecommerce object.
  75.         // GA4
  76.         dataLayer.push({
  77.             'event': 'add_to_cart',
  78.             'ecommerce': {
  79.                 'items': [
  80.                     {% for CartIndex,Cart in Carts %}
  81.                     {% for CartItem in Cart.CartItems %}
  82.                     {% set ProductClass = CartItem.ProductClass %}
  83.                     {% set Product = ProductClass.Product %}
  84.                     {
  85.                         'item_name': '{{ Product.name }}',
  86.                         'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  87.                         'price': '{{ Product.getPrice02IncTaxMin }}'
  88.                     }{% if not loop.last %},{% endif %}
  89.                     {% endfor %}
  90.                     {% endfor %}
  91.                 ]
  92.             }
  93.         });
  94.         // UA
  95.         dataLayer.push({
  96.             'event': 'addToCart',
  97.             'ecommerce': {
  98.                 'currencyCode': 'JPY',
  99.                 'add': {
  100.                     'products': [
  101.                         {% for CartIndex,Cart in Carts %}
  102.                         {% for CartItem in Cart.CartItems %}
  103.                         {% set ProductClass = CartItem.ProductClass %}
  104.                         {% set Product = ProductClass.Product %}
  105.                         {
  106.                             'name': '{{ Product.name }}',
  107.                             'id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  108.                             'price': '{{ Product.getPrice02IncTaxMin }}',
  109.                             'quantity': {{ CartItem.quantity }}
  110.                         }{% if not loop.last %},{% endif %}
  111.                         {% endfor %}
  112.                         {% endfor %}
  113.                     ]
  114.                 }
  115.             }
  116.         });
  117.     </script>
  118. {% elseif app.request.get('_route') == 'shopping_complete' %}
  119.     {# 購入完了 #}
  120.     {% if Order.id %}
  121.         <script>
  122.             dataLayer.push({ecommerce: null});
  123.             // GA4
  124.             dataLayer.push({
  125.                 'event': 'purchase',
  126.                 'ecommerce': {
  127.                     'transaction_id': '{{ Order.order_no }}',
  128.                     'affiliation': '{{ BaseInfo.shop_name }}',
  129.                     'value': '{{ Order.subtotal }}',
  130.                     'tax': '{{ Order.tax }}',
  131.                     'shipping': '{{ Order.delivery_fee_total }}',
  132.                     'currency': 'JPY',
  133.                     'items': [
  134.                         {% for OrderItem in Order.MergedProductOrderItems %}
  135.                         {
  136.                             'item_name': '{{ OrderItem.product_name }}',
  137.                             'item_id': '{{ OrderItem.product_code ? OrderItem.product_code : OrderItem.product.id }}',
  138.                             'price': '{{ OrderItem.price_inc_tax }}',
  139.                             'item_category': '{% for ProductCategory in OrderItem.Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  140.                             'quantity': {{ OrderItem.quantity }}
  141.                         }{% if not loop.last %},{% endif %}
  142.                         {% endfor %}
  143.                     ]
  144.                 }
  145.             });
  146.             // UA
  147.             dataLayer.push({
  148.                 'ecommerce': {
  149.                     'purchase': {
  150.                         'actionField': {
  151.                             'id': '{{ Order.order_no }}',
  152.                             'affiliation': '{{ BaseInfo.shop_name }}',
  153.                             'revenue': '{{ Order.subtotal }}',
  154.                             'tax': '{{ Order.tax }}',
  155.                             'shipping': '{{ Order.delivery_fee_total }}'
  156.                         },
  157.                         'products': [
  158.                             {% for OrderItem in Order.MergedProductOrderItems %}
  159.                             {
  160.                                 'name': '{{ OrderItem.product_name }}',
  161.                                 'id': '{{ OrderItem.product_code ? OrderItem.product_code : OrderItem.product.id }}',
  162.                                 'price': '{{ OrderItem.price_inc_tax }}',
  163.                                 'category': '{% for ProductCategory in OrderItem.Product.ProductCategories %}{% for Category in ProductCategory.Category.path %}{% if ( Category.parent is not null ) %}{{ Category.Parent.name }}:{{ Category.name }}{% endif %}{% endfor %}{% endfor %}',
  164.                                 'quantity': {{ OrderItem.quantity }}
  165.                             }{% if not loop.last %},{% endif %}
  166.                             {% endfor %}
  167.                         ]
  168.                     }
  169.                 }
  170.             });
  171.         </script>
  172.     {% endif %}
  173. {% endif %}