app/template/default/Block/ecommerce.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.     </script>
  22. {% elseif app.request.get('_route') == 'product_detail' %}
  23.     {# 商品詳細 #}
  24.     <script>
  25.         // Measure a view of product details. This example assumes the detail view occurs on pageload,
  26.         dataLayer.push({ecommerce: null});  // Clear the previous ecommerce object.
  27.         // GA4
  28.         dataLayer.push({
  29.             'event': 'view_item',
  30.             'ecommerce': {
  31.                 'items': [{
  32.                     'item_name': '{{ Product.name }}',
  33.                     'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  34.                     'price': '{{ Product.getPrice02IncTaxMin }}',
  35.                     '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 %}',
  36.                     'quantity': '1'
  37.                 }]
  38.             }
  39.         });
  40.     </script>
  41. {% elseif app.request.get('_route') == 'cart' %}
  42.     {# カートに追加 #}
  43.     <script>
  44.         // Measure when a product is added to a shopping cart
  45.         dataLayer.push({ecommerce: null});  // Clear the previous ecommerce object.
  46.         // GA4
  47.         dataLayer.push({
  48.             'event': 'add_to_cart',
  49.             'ecommerce': {
  50.                 'items': [
  51.                     {% for CartIndex,Cart in Carts %}
  52.                     {% for CartItem in Cart.CartItems %}
  53.                     {% set ProductClass = CartItem.ProductClass %}
  54.                     {% set Product = ProductClass.Product %}
  55.                     {
  56.                         'item_name': '{{ Product.name }}',
  57.                         'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
  58.                         'price': '{{ Product.getPrice02IncTaxMin }}'
  59.                     }{% if not loop.last %},{% endif %}
  60.                     {% endfor %}
  61.                     {% endfor %}
  62.                 ]
  63.             }
  64.         });
  65.     </script>
  66. {% elseif app.request.get('_route') == 'shopping_complete' %}
  67.     {# 購入完了 #}
  68.     {% if Order.id %}
  69.         <script>
  70.             dataLayer.push({ecommerce: null});
  71.             // GA4
  72.             dataLayer.push({
  73.                 'event': 'purchase',
  74.                 'ecommerce': {
  75.                     'transaction_id': '{{ Order.order_no }}',
  76.                     'affiliation': '{{ BaseInfo.shop_name }}',
  77.                     'value': '{{ Order.subtotal }}',
  78.                     'tax': '{{ Order.tax }}',
  79.                     'shipping': '{{ Order.delivery_fee_total }}',
  80.                     'currency': 'JPY',
  81.                     'items': [
  82.                         {% for OrderItem in Order.MergedProductOrderItems %}
  83.                         {
  84.                             'item_name': '{{ OrderItem.product_name }}',
  85.                             'item_id': '{{ OrderItem.product_code ? OrderItem.product_code : OrderItem.product.id }}',
  86.                             'price': '{{ OrderItem.price_inc_tax }}',
  87.                             '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 %}',
  88.                             'quantity': {{ OrderItem.quantity }}
  89.                         }{% if not loop.last %},{% endif %}
  90.                         {% endfor %}
  91.                     ]
  92.                 }
  93.             });
  94.         </script>
  95.     {% endif %}
  96. {% endif %}