{% if app.request.get('_route') == 'product_list' %}
{# 商品一覧 #}
<script>
dataLayer.push({ecommerce: null});
// GA4
dataLayer.push({
'event': 'view_item_list',
'ecommerce': {
'items': [
{% for Product in pagination %}
{
'item_name': '{{ Product.name }}',
'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
'price': '{{ Product.getPrice02IncTaxMin }}',
'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 %}',
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
</script>
{% elseif app.request.get('_route') == 'product_detail' %}
{# 商品詳細 #}
<script>
// Measure a view of product details. This example assumes the detail view occurs on pageload,
dataLayer.push({ecommerce: null}); // Clear the previous ecommerce object.
// GA4
dataLayer.push({
'event': 'view_item',
'ecommerce': {
'items': [{
'item_name': '{{ Product.name }}',
'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
'price': '{{ Product.getPrice02IncTaxMin }}',
'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 %}',
'quantity': '1'
}]
}
});
</script>
{% elseif app.request.get('_route') == 'cart' %}
{# カートに追加 #}
<script>
// Measure when a product is added to a shopping cart
dataLayer.push({ecommerce: null}); // Clear the previous ecommerce object.
// GA4
dataLayer.push({
'event': 'add_to_cart',
'ecommerce': {
'items': [
{% for CartIndex,Cart in Carts %}
{% for CartItem in Cart.CartItems %}
{% set ProductClass = CartItem.ProductClass %}
{% set Product = ProductClass.Product %}
{
'item_name': '{{ Product.name }}',
'item_id': '{{ Product.getCodeMin ? Product.getCodeMin : Product.id }}',
'price': '{{ Product.getPrice02IncTaxMin }}'
}{% if not loop.last %},{% endif %}
{% endfor %}
{% endfor %}
]
}
});
</script>
{% elseif app.request.get('_route') == 'shopping_complete' %}
{# 購入完了 #}
{% if Order.id %}
<script>
dataLayer.push({ecommerce: null});
// GA4
dataLayer.push({
'event': 'purchase',
'ecommerce': {
'transaction_id': '{{ Order.order_no }}',
'affiliation': '{{ BaseInfo.shop_name }}',
'value': '{{ Order.subtotal }}',
'tax': '{{ Order.tax }}',
'shipping': '{{ Order.delivery_fee_total }}',
'currency': 'JPY',
'items': [
{% for OrderItem in Order.MergedProductOrderItems %}
{
'item_name': '{{ OrderItem.product_name }}',
'item_id': '{{ OrderItem.product_code ? OrderItem.product_code : OrderItem.product.id }}',
'price': '{{ OrderItem.price_inc_tax }}',
'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 %}',
'quantity': {{ OrderItem.quantity }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
</script>
{% endif %}
{% endif %}