{#
GtmGaEEc42: GTM版 Google Analytics eコマース/拡張eコマース対応プラグイン
Copyright (C) 2017-2022 Freischtide Inc. All Rights Reserved.
http://freischtide.tumblr.com/
License: see LICENSE.txt
#}
{% if gtmgaeec.tid|length > 0 %}
<script type="text/javascript" id="gtmgaeec_ga4_tag">
dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
{% if ga4_impressions is not defined and impressions is defined and impressions|length > 0 %}
dataLayer.push({
event: "view_item_list",
ecommerce: {
items: [
{% for product in impressions %}
{
item_id: "{{ product.id }}",
item_name: "{{ product.name }}",
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
{% if list_name is defined %}
item_list_name: "{{ list_name }}",
{% endif %}
{% if product.quantity is defined and product.quantity > 0 %}
quantity: {{ product.quantity }},
{% endif %}
index: {{ loop.index }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% if ga4_impressions is defined and ga4_impressions|length > 0 %}
dataLayer.push({
event: "view_item_list",
ecommerce: {
items: [
{% for product in ga4_impressions %}
{
item_id: "{{ product.id }}",
item_name: "{{ product.name }}",
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
item_list_name: "Related Item",
{% if product.quantity is defined and product.quantity > 0 %}
quantity: {{ product.quantity }},
{% endif %}
index: {{ loop.index }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% if action is defined %}
{% if action.action == 'detail' %}
{% if products is defined %}
dataLayer.push({
event: "view_item",
ecommerce: {
items: [
{% for product in products %}
{
item_name: "{{ product.name }}",
item_id: "{{ product.id }}",
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% elseif action.action == 'add' %}
{% if products is defined %}
dataLayer.push({
event: 'add_to_cart',
ecommerce: {
items: [
{% for product in products %}
{
item_name: '{{ product.name }}',
item_id: '{{ product.id }}',
{% if product.variant|length > 0 %}
item_variant: '{{ product.variant }}',
{% endif %}
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
quantity: {{ product.quantity }},
index: {{ loop.index - 1 }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% elseif action.action == 'checkout' %}
{% if products is defined %}
dataLayer.push({
event: '{% if checkout_step == 1 %}begin_checkout{% elseif checkout_step == 2 %}confirmation{% endif %}',
ecommerce: {
items: [
{% for product in products %}
{
item_name: '{{ product.name }}',
item_id: '{{ product.id }}',
{% if product.variant|length > 0 %}
item_variant: '{{ product.variant }}',
{% endif %}
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
quantity: {{ product.quantity }},
index: {{ loop.index }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% if transaction is defined %}
dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: {{ transaction.id|raw }},
value: {{ transaction.revenue|raw }},
tax: {{ transaction.tax|raw }},
{% if coupon_code|length > 0 %}
coupon: '{{ coupon_code }}',
{% endif %}
shipping: {{ transaction.shipping|raw }},
currency: "JPY",
items: [
{% for product in products %}
{
item_name: '{{ product.name }}',
item_id: '{{ product.id }}',
{% if product.variant|length > 0 %}
item_variant: '{{ product.variant }}',
{% endif %}
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
{% if product.discount is defined and product.discount > 0 %}
{% if coupon_code is defined and coupon_code|length > 0 %}
coupon: '{{ coupon_code }}',
{% endif %}
discount: {{ product.discount }},
{% endif %}
quantity: {{ product.quantity }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% endif %}
{% elseif action.action == 'purchase' %}
{% if products is defined %}
dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: {{ transaction.id|raw }},
value: {{ transaction.revenue|raw }},
tax: {{ transaction.tax|raw }},
{% if coupon_code|length > 0 %}
coupon: '{{ coupon_code }}',
{% endif %}
shipping: {{ transaction.shipping|raw }},
currency: "JPY",
items: [
{% for product in products %}
{
item_name: '{{ product.name }}',
item_id: '{{ product.id }}',
{% if product.variant|length > 0 %}
item_variant: '{{ product.variant }}',
{% endif %}
price: {{ product.price }},
{% if product.category|split(', ')|length > 0 %}
{% for category in product.category|split(', ') %}
item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
{% endfor %}
{% endif %}
{% if product.discount is defined and product.discount > 0 %}
{% if coupon_code is defined and coupon_code|length > 0 %}
coupon: '{{ coupon_code }}',
{% endif %}
discount: {{ product.discount }},
{% endif %}
quantity: {{ product.quantity }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
});
{% endif %}
{% endif %}
{% endif %}
{% if promo is defined %}
dataLayer.push({
event: "view_promotion",
ecommerce: {
items: [{
item_name: "{{ promo.name }}",
item_id: "{{ promo.id }}"
{% if promo.creative|length > 0 %}
, creative_name: "{{ promo.creative }}"
{% endif %}
{% if promo.position|length > 0 %}
, index: {{ promo.position }}
{% 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>
{% endif %}