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

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