{#
GtmGaEEc42: GTM版 Google Analytics eコマース/拡張eコマース対応プラグイン
Copyright (C) 2017-2021 Freischtide Inc. All Rights Reserved.
http://freischtide.tumblr.com/
License: see LICENSE.txt
#}
{% if gtmgaeec.tid|length > 0 %}
<script type="text/javascript" id="gtmgaeec_ua_tag">
dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
var gaeecData = window.gaeecData || {};
{% if user_id is defined %}
gaeecData['gaeec_user_id'] = '{{ user_id }}';
{% endif %}
{% if gtmgaeec.eec == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_USE_EC') %}
{% if action is defined and action.action == 'purchase' and products is defined %}
{% if transaction is defined %}
gaeecData['transactionId'] = {{ transaction.id|raw }};
gaeecData['transactionTotal'] = {{ transaction.revenue|raw }};
gaeecData['transactionTax'] = {{ transaction.tax|raw }};
gaeecData['transactionShipping'] = {{ transaction.shipping|raw }};
var transactionProducts = window.transactionProducts || [];
{% for product in products %}
transactionProducts.push({
'sku': '{{ product.id }}',
'name': '{{ product.name }}',
'category': '{{ product.category }}',
'price': '{{ product.price }}',
'quantity': {{ product.quantity }}
});
{% endfor %}
gaeecData['transactionProducts'] = transactionProducts;
{% endif %}
{% endif %}
{% elseif gtmgaeec.eec == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_USE_EEC') %}
var ecommerce = window.ecommerce || {};
{% if impressions is defined and impressions|length > 0 %}
ecommerce['impressions'] = ecommerce['impressions'] || [];
{% for product in impressions %}
ecommerce['impressions'].push({
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
'category': '{{ product.category }}',
'list': '{{ product.list }}',
'position': {{ loop.index }}
});
{% endfor %}
{% endif %}
{% if action is defined %}
{% if action.action == 'detail' %}
{% if products is defined %}
ecommerce['detail'] = {
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}'
{% endif %}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
ecommerce['click'] = {
'actionField': {'list': '{{ list_name }}'},
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}'
{% endif %}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
{% endif %}
{% elseif action.action == 'add' %}
{% if products is defined %}
ecommerce['currencyCode'] = 'JPY';
ecommerce['add'] = {
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}',
{% endif %}
{% if product.variant|length > 0 %}
'variant': '{{ product.variant }}',
{% endif %}
'quantity': {{ product.quantity }},
'position': {{ loop.index }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
{% endif %}
{% elseif action.action == 'checkout' %}
{% if products is defined %}
ecommerce['checkout'] = {
'actionField': {'step': {{ checkout_step }}},
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}',
{% endif %}
{% if product.variant|length > 0 %}
'variant': '{{ product.variant }}',
{% endif %}
'quantity': {{ product.quantity }},
'position': {{ loop.index }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
{% if transaction is defined %}
ecommerce['purchase'] = {
'actionField': {
'id': {{ transaction.id|raw }},
'revenue': {{ transaction.revenue|raw }},
'tax': {{ transaction.tax|raw }},
{% if coupon_code|length > 0 %}
'coupon': '{{ coupon_code }}',
{% endif %}
'shipping': {{ transaction.shipping|raw }}
},
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}',
{% endif %}
{% if product.variant|length > 0 %}
'variant': '{{ product.variant }}',
{% endif %}
'quantity': {{ product.quantity }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
{% endif %}
{% endif %}
{% elseif action.action == 'purchase' %}
{% if products is defined %}
ecommerce['purchase'] = {
'actionField': {
'id': {{ transaction.id|raw }},
'revenue': {{ transaction.revenue|raw }},
'tax': {{ transaction.tax|raw }},
{% if coupon_code|length > 0 %}
'coupon': '{{ coupon_code }}',
{% endif %}
'shipping': {{ transaction.shipping|raw }}
},
'products': [
{% for product in products %}
{
'name': '{{ product.name }}',
'id': '{{ product.id }}',
'price': '{{ product.price }}',
{% if product.category|length > 0 %}
'category': '{{ product.category }}',
{% endif %}
{% if product.variant|length > 0 %}
'variant': '{{ product.variant }}',
{% endif %}
'quantity': {{ product.quantity }}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
{% endif %}
{% endif %}
{% endif %}
{% if promo is defined %}
ecommerce['promoView'] = {
'promotions': [
{
'id': '{{ promo.id }}',
'name': '{{ promo.name }}'
{% if promo.creative|length > 0 %}
, 'creative': '{{ promo.creative }}'
{% endif %}
{% if promo.position|length > 0 %}
, 'position': '{{ promo.position }}'
{% endif %}
}
]
};
{% endif %}
{% if event_name is defined %}
ecommerce['custom_event'] = "{{ event_name }}";
{% endif %}
{% endif %}
if (Object.keys(ecommerce).length > 0) {
gaeecData['ecommerce'] = ecommerce;
}
{% if gtmgaeec.user_timings == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_OP_WITH_USER_TIMINGS') %}
if (window.performance) {
gaeecData['gaeec_user_timings'] = Math.round(performance.now());
}
{% endif %}
gaeecData['event'] = 'gtmgaeec';
dataLayer.push(gaeecData);
</script>
{% endif %}