<script type="text/javascript">
dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
{% if app.request.get('_route') == 'product_list' %}
{# 商品一覧 #}
// 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 %}
]
}
});
{% elseif app.request.get('_route') == 'product_detail' %}
{# 商品詳細 #}
// 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'
}]
}
});
//add_cartはdetail.twigに直接記載
{% elseif app.request.get('_route') == 'cart' %}
{# カートを表示 #}
// Measure when a product is added to a shopping cart
// dataLayer.push({ecommerce: null}); // Clear the previous ecommerce object.
// GA4
dataLayer.push({
'event': 'view_cart',
'ecommerce': {
'currency': 'JPY',
'value': '{{ totalPrice }}',
'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 %}
]
}
});
{% elseif app.request.get('_route') == 'shopping' %}
{# 決済開始を表示 #}
// GA4
dataLayer.push({
'event': 'begin_checkout',
'ecommerce': {
'value': '{{ Order.subtotal }}',
// 'tax': '{{ Order.tax }}',
// 'shipping': '{{ Order.delivery_fee_total }}',
'currency': 'JPY',
'items': [
{% for shipping in Order.shippings %}
{% for orderItem in shipping.productOrderItems %}
{
'item_name': '{{ orderItem.productName }}',
'item_id': '{{ orderItem.product.id }}',
'price': '{{ orderItem.priceIncTax }}',
'quantity': {{ orderItem.quantity }},
'affiliation': '{{ BaseInfo.shop_name }}'
}{% if not loop.last %},{% endif %}
{% endfor %}
{% endfor %}
]
}
});
{% elseif app.request.get('_route') == 'shopping_complete' %}
{# 購入完了 #}
{% if Order.id %}
// dataLayer.push({ecommerce: null});
// GA4
dataLayer.push({
'event': 'purchase',
'ecommerce': {
'transaction_id': '{{ Order.order_no }}',
'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 }},
'affiliation': '{{ BaseInfo.shop_name }}'
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% endif %}
{% if user_id is defined %}
dataLayer.push({
user_id: "{{ user_id }}"
});
{% endif %}
{% if event_name is defined %}
dataLayer.push({
event: "{{ event_name }}"
});
{% endif %}
</script>