app/Plugin/GtmGaEEc42/Resource/template/default/gtmgaeec_ua.twig line 1

Open in your IDE?
  1. {#
  2. GtmGaEEc42: GTM版 Google Analytics eコマース/拡張eコマース対応プラグイン
  3. Copyright (C) 2017-2021 Freischtide Inc. All Rights Reserved.
  4. http://freischtide.tumblr.com/
  5. License: see LICENSE.txt
  6. #}
  7. {% if gtmgaeec.tid|length > 0 %}
  8. <script type="text/javascript" id="gtmgaeec_ua_tag">
  9.   dataLayer = window.dataLayer || [];
  10.   dataLayer.push({ ecommerce: null });
  11.   var gaeecData = window.gaeecData || {};
  12.   {% if user_id is defined %}
  13.     gaeecData['gaeec_user_id'] = '{{ user_id }}';
  14.   {% endif %}
  15.   {% if gtmgaeec.eec == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_USE_EC') %}
  16.     {% if action is defined and action.action == 'purchase' and products is defined %}
  17.       {% if transaction is defined %}
  18.         gaeecData['transactionId'] = {{ transaction.id|raw }};
  19.         gaeecData['transactionTotal'] = {{ transaction.revenue|raw }};
  20.         gaeecData['transactionTax'] = {{ transaction.tax|raw }};
  21.         gaeecData['transactionShipping'] = {{ transaction.shipping|raw }};
  22.         var transactionProducts = window.transactionProducts || [];
  23.         {% for product in products %}
  24.           transactionProducts.push({
  25.             'sku': '{{ product.id }}',
  26.             'name': '{{ product.name }}',
  27.             'category': '{{ product.category }}',
  28.             'price': '{{ product.price }}',
  29.             'quantity': {{ product.quantity }}
  30.           });
  31.         {% endfor %}
  32.         gaeecData['transactionProducts'] = transactionProducts;
  33.       {% endif %}
  34.     {% endif %}
  35.   {% elseif gtmgaeec.eec == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_USE_EEC') %}
  36.     var ecommerce = window.ecommerce || {};
  37.     {% if impressions is defined and impressions|length > 0 %}
  38.       ecommerce['impressions'] = ecommerce['impressions'] || [];
  39.       {% for product in impressions %}
  40.         ecommerce['impressions'].push({
  41.           'name': '{{ product.name }}',
  42.           'id': '{{ product.id }}',
  43.           'price': '{{ product.price }}',
  44.           'category': '{{ product.category }}',
  45.           'list': '{{ product.list }}',
  46.           'position': {{ loop.index }}
  47.         });
  48.       {% endfor %}
  49.     {% endif %}
  50.     {% if action is defined %}
  51.       {% if action.action == 'detail' %}
  52.         {% if products is defined %}
  53.           ecommerce['detail'] = {
  54.             'products': [
  55.               {% for product in products %}
  56.                 {
  57.                   'name': '{{ product.name }}',
  58.                   'id': '{{ product.id }}',
  59.                   'price': '{{ product.price }}',
  60.                   {% if product.category|length > 0 %}
  61.                   'category': '{{ product.category }}'
  62.                   {% endif %}
  63.                 }{% if not loop.last %},{% endif %}
  64.               {% endfor %}
  65.             ]
  66.           };
  67.           ecommerce['click'] = {
  68.             'actionField': {'list': '{{ list_name }}'},
  69.             'products': [
  70.               {% for product in products %}
  71.                 {
  72.                   'name': '{{ product.name }}',
  73.                   'id': '{{ product.id }}',
  74.                   'price': '{{ product.price }}',
  75.                   {% if product.category|length > 0 %}
  76.                   'category': '{{ product.category }}'
  77.                   {% endif %}
  78.                 }{% if not loop.last %},{% endif %}
  79.               {% endfor %}
  80.             ]
  81.           };
  82.         {% endif %}
  83.       {% elseif action.action == 'add' %}
  84.         {% if products is defined %}
  85.           ecommerce['currencyCode'] = 'JPY';
  86.           ecommerce['add'] = {
  87.             'products': [
  88.               {% for product in products %}
  89.                 {
  90.                   'name': '{{ product.name }}',
  91.                   'id': '{{ product.id }}',
  92.                   'price': '{{ product.price }}',
  93.                   {% if product.category|length > 0 %}
  94.                     'category': '{{ product.category }}',
  95.                   {% endif %}
  96.                   {% if product.variant|length > 0 %}
  97.                     'variant': '{{ product.variant }}',
  98.                   {% endif %}
  99.                   'quantity': {{ product.quantity }},
  100.                   'position': {{ loop.index }}
  101.                 }{% if not loop.last %},{% endif %}
  102.               {% endfor %}
  103.             ]
  104.           };
  105.         {% endif %}
  106.       {% elseif action.action == 'checkout' %}
  107.         {% if products is defined %}
  108.           ecommerce['checkout'] = {
  109.             'actionField': {'step': {{ checkout_step }}},
  110.             'products': [
  111.               {% for product in products %}
  112.                 {
  113.                   'name': '{{ product.name }}',
  114.                   'id': '{{ product.id }}',
  115.                   'price': '{{ product.price }}',
  116.                   {% if product.category|length > 0 %}
  117.                     'category': '{{ product.category }}',
  118.                   {% endif %}
  119.                   {% if product.variant|length > 0 %}
  120.                     'variant': '{{ product.variant }}',
  121.                   {% endif %}
  122.                   'quantity': {{ product.quantity }},
  123.                   'position': {{ loop.index }}
  124.                 }{% if not loop.last %},{% endif %}
  125.               {% endfor %}
  126.             ]
  127.           };
  128.           {% if transaction is defined %}
  129.             ecommerce['purchase'] = {
  130.               'actionField': {
  131.                 'id': {{ transaction.id|raw }},
  132.                 'revenue': {{ transaction.revenue|raw }},
  133.                 'tax': {{ transaction.tax|raw }},
  134.                 {% if coupon_code|length > 0 %}
  135.                 'coupon': '{{ coupon_code }}',
  136.                 {% endif %}
  137.                 'shipping': {{ transaction.shipping|raw }}
  138.               },
  139.               'products': [
  140.                 {% for product in products %}
  141.                   {
  142.                     'name': '{{ product.name }}',
  143.                     'id': '{{ product.id }}',
  144.                     'price': '{{ product.price }}',
  145.                     {% if product.category|length > 0 %}
  146.                       'category': '{{ product.category }}',
  147.                     {% endif %}
  148.                     {% if product.variant|length > 0 %}
  149.                       'variant': '{{ product.variant }}',
  150.                     {% endif %}
  151.                     'quantity': {{ product.quantity }}
  152.                   }{% if not loop.last %},{% endif %}
  153.                 {% endfor %}
  154.               ]
  155.             };
  156.           {% endif %}
  157.         {% endif %}
  158.       {% elseif action.action == 'purchase' %}
  159.         {% if products is defined %}
  160.           ecommerce['purchase'] = {
  161.             'actionField': {
  162.               'id': {{ transaction.id|raw }},
  163.               'revenue': {{ transaction.revenue|raw }},
  164.               'tax': {{ transaction.tax|raw }},
  165.               {% if coupon_code|length > 0 %}
  166.               'coupon': '{{ coupon_code }}',
  167.               {% endif %}
  168.               'shipping': {{ transaction.shipping|raw }}
  169.             },
  170.             'products': [
  171.               {% for product in products %}
  172.                 {
  173.                   'name': '{{ product.name }}',
  174.                   'id': '{{ product.id }}',
  175.                   'price': '{{ product.price }}',
  176.                   {% if product.category|length > 0 %}
  177.                     'category': '{{ product.category }}',
  178.                   {% endif %}
  179.                   {% if product.variant|length > 0 %}
  180.                     'variant': '{{ product.variant }}',
  181.                   {% endif %}
  182.                   'quantity': {{ product.quantity }}
  183.                 }{% if not loop.last %},{% endif %}
  184.               {% endfor %}
  185.             ]
  186.           };
  187.         {% endif %}
  188.       {% endif %}
  189.     {% endif %}
  190.     {% if promo is defined %}
  191.       ecommerce['promoView'] = {
  192.         'promotions': [
  193.           {
  194.             'id': '{{ promo.id }}',
  195.             'name': '{{ promo.name }}'
  196.             {% if promo.creative|length > 0 %}
  197.             , 'creative': '{{ promo.creative }}'
  198.             {% endif %}
  199.             {% if promo.position|length > 0 %}
  200.             , 'position': '{{ promo.position }}'
  201.             {% endif %}
  202.           }
  203.         ]
  204.       };
  205.     {% endif %}
  206.     {% if event_name is defined %}
  207.       ecommerce['custom_event'] = "{{ event_name }}";
  208.     {% endif %}
  209.   {% endif %}
  210.   if (Object.keys(ecommerce).length > 0) {
  211.     gaeecData['ecommerce'] = ecommerce;
  212.   }
  213.   {% if gtmgaeec.user_timings == constant('Plugin\\GtmGaEEc42\\Entity\\GtmGaEEc::GTMGAEEC_OP_WITH_USER_TIMINGS') %}
  214.     if (window.performance) {
  215.       gaeecData['gaeec_user_timings'] = Math.round(performance.now());
  216.     }
  217.   {% endif %}
  218.   gaeecData['event'] = 'gtmgaeec';
  219.   dataLayer.push(gaeecData);
  220. </script>
  221. {% endif %}