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

Open in your IDE?
  1. {#
  2. GtmGaEEc42: GTM版 Google Analytics eコマース/拡張eコマース対応プラグイン
  3. Copyright (C) 2017-2022 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_ga4_tag">
  9.   dataLayer = window.dataLayer || [];
  10.   dataLayer.push({ ecommerce: null });
  11.   {% if ga4_impressions is not defined and impressions is defined and impressions|length > 0 %}
  12.     dataLayer.push({
  13.       event: "view_item_list",
  14.       ecommerce: {
  15.         items: [
  16.           {% for product in impressions %}
  17.             {
  18.               item_id: "{{ product.id }}",
  19.               item_name: "{{ product.name }}",
  20.               price: {{ product.price }},
  21.               {% if product.category|split(', ')|length > 0 %}
  22.                 {% for category in product.category|split(', ') %}
  23.                   item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  24.                 {% endfor %}
  25.               {% endif %}
  26.               {% if list_name is defined %}
  27.                 item_list_name: "{{ list_name }}",
  28.               {% endif %}
  29.               {% if product.quantity is defined and product.quantity > 0 %}
  30.                 quantity: {{ product.quantity }},
  31.               {% endif %}
  32.               index: {{ loop.index }}
  33.             }{% if not loop.last %},{% endif %}
  34.           {% endfor %}
  35.         ]
  36.       }
  37.     });
  38.   {% endif %}
  39.   {% if ga4_impressions is defined and ga4_impressions|length > 0 %}
  40.     dataLayer.push({
  41.       event: "view_item_list",
  42.       ecommerce: {
  43.         items: [
  44.           {% for product in ga4_impressions %}
  45.             {
  46.               item_id: "{{ product.id }}",
  47.               item_name: "{{ product.name }}",
  48.               price: {{ product.price }},
  49.               {% if product.category|split(', ')|length > 0 %}
  50.                 {% for category in product.category|split(', ') %}
  51.                   item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  52.                 {% endfor %}
  53.               {% endif %}
  54.               item_list_name: "Related Item",
  55.               {% if product.quantity is defined and product.quantity > 0 %}
  56.                 quantity: {{ product.quantity }},
  57.               {% endif %}
  58.               index: {{ loop.index }}
  59.             }{% if not loop.last %},{% endif %}
  60.           {% endfor %}
  61.         ]
  62.       }
  63.     });
  64.   {% endif %}
  65.   {% if action is defined %}
  66.     {% if action.action == 'detail' %}
  67.       {% if products is defined %}
  68.         dataLayer.push({
  69.           event: "view_item",
  70.           ecommerce: {
  71.             items: [
  72.               {% for product in products %}
  73.                 {
  74.                   item_name: "{{ product.name }}",
  75.                   item_id: "{{ product.id }}",
  76.                   price: {{ product.price }},
  77.                   {% if product.category|split(', ')|length > 0 %}
  78.                     {% for category in product.category|split(', ') %}
  79.                       item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  80.                     {% endfor %}
  81.                   {% endif %}
  82.                 }{% if not loop.last %},{% endif %}
  83.               {% endfor %}
  84.             ]
  85.           }
  86.         });
  87.       {% endif %}
  88.     {% elseif action.action == 'add' %}
  89.       {% if products is defined %}
  90.         dataLayer.push({
  91.           event: 'add_to_cart',
  92.           ecommerce: {
  93.             items: [
  94.               {% for product in products %}
  95.                 {
  96.                   item_name: '{{ product.name }}',
  97.                   item_id: '{{ product.id }}',
  98.                   {% if product.variant|length > 0 %}
  99.                   item_variant: '{{ product.variant }}',
  100.                   {% endif %}
  101.                   price: {{ product.price }},
  102.                   {% if product.category|split(', ')|length > 0 %}
  103.                     {% for category in product.category|split(', ') %}
  104.                       item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  105.                     {% endfor %}
  106.                   {% endif %}
  107.                   quantity: {{ product.quantity }},
  108.                   index: {{ loop.index - 1 }}
  109.                 }{% if not loop.last %},{% endif %}
  110.               {% endfor %}
  111.             ]
  112.           }
  113.         });
  114.       {% endif %}
  115.     {% elseif action.action == 'checkout' %}
  116.       {% if products is defined %}
  117.         dataLayer.push({
  118.           event: '{% if checkout_step == 1 %}begin_checkout{% elseif checkout_step == 2 %}confirmation{% endif %}',
  119.           ecommerce: {
  120.             items: [
  121.               {% for product in products %}
  122.                 {
  123.                   item_name: '{{ product.name }}',
  124.                   item_id: '{{ product.id }}',
  125.                   {% if product.variant|length > 0 %}
  126.                   item_variant: '{{ product.variant }}',
  127.                   {% endif %}
  128.                   price: {{ product.price }},
  129.                   {% if product.category|split(', ')|length > 0 %}
  130.                     {% for category in product.category|split(', ') %}
  131.                       item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  132.                     {% endfor %}
  133.                   {% endif %}
  134.                   quantity: {{ product.quantity }},
  135.                   index: {{ loop.index }}
  136.                 }{% if not loop.last %},{% endif %}
  137.               {% endfor %}
  138.             ]
  139.           }
  140.         });
  141.         {% if transaction is defined %}
  142.           dataLayer.push({
  143.             event: 'purchase',
  144.             ecommerce: {
  145.               transaction_id: {{ transaction.id|raw }},
  146.               value: {{ transaction.revenue|raw }},
  147.               tax: {{ transaction.tax|raw }},
  148.               {% if coupon_code|length > 0 %}
  149.                 coupon: '{{ coupon_code }}',
  150.               {% endif %}
  151.               shipping: {{ transaction.shipping|raw }},
  152.               currency: "JPY",
  153.               items: [
  154.                 {% for product in products %}
  155.                   {
  156.                     item_name: '{{ product.name }}',
  157.                     item_id: '{{ product.id }}',
  158.                     {% if product.variant|length > 0 %}
  159.                     item_variant: '{{ product.variant }}',
  160.                     {% endif %}
  161.                     price: {{ product.price }},
  162.                     {% if product.category|split(', ')|length > 0 %}
  163.                       {% for category in product.category|split(', ') %}
  164.                         item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  165.                       {% endfor %}
  166.                     {% endif %}
  167.                     {% if product.discount is defined and product.discount > 0 %}
  168.                       {% if coupon_code is defined and coupon_code|length > 0 %}
  169.                         coupon: '{{ coupon_code }}',
  170.                       {% endif %}
  171.                       discount: {{ product.discount }},
  172.                     {% endif %}
  173.                     quantity: {{ product.quantity }}
  174.                   }{% if not loop.last %},{% endif %}
  175.                 {% endfor %}
  176.               ]
  177.             }
  178.           });
  179.         {% endif %}
  180.       {% endif %}
  181.     {% elseif action.action == 'purchase' %}
  182.       {% if products is defined %}
  183.         dataLayer.push({
  184.           event: 'purchase',
  185.           ecommerce: {
  186.             transaction_id: {{ transaction.id|raw }},
  187.             value: {{ transaction.revenue|raw }},
  188.             tax: {{ transaction.tax|raw }},
  189.             {% if coupon_code|length > 0 %}
  190.               coupon: '{{ coupon_code }}',
  191.             {% endif %}
  192.             shipping: {{ transaction.shipping|raw }},
  193.             currency: "JPY",
  194.             items: [
  195.               {% for product in products %}
  196.                 {
  197.                   item_name: '{{ product.name }}',
  198.                   item_id: '{{ product.id }}',
  199.                   {% if product.variant|length > 0 %}
  200.                   item_variant: '{{ product.variant }}',
  201.                   {% endif %}
  202.                   price: {{ product.price }},
  203.                   {% if product.category|split(', ')|length > 0 %}
  204.                     {% for category in product.category|split(', ') %}
  205.                       item_category{{loop.index == 1 ? '' : loop.index}}: "{{ category }}",
  206.                     {% endfor %}
  207.                   {% endif %}
  208.                   {% if product.discount is defined and product.discount > 0 %}
  209.                     {% if coupon_code is defined and coupon_code|length > 0 %}
  210.                       coupon: '{{ coupon_code }}',
  211.                     {% endif %}
  212.                     discount: {{ product.discount }},
  213.                   {% endif %}
  214.                   quantity: {{ product.quantity }}
  215.                 }{% if not loop.last %},{% endif %}
  216.               {% endfor %}
  217.             ]
  218.           }
  219.         });
  220.       {% endif %}
  221.     {% endif %}
  222.   {% endif %}
  223.   {% if promo is defined %}
  224.     dataLayer.push({
  225.       event: "view_promotion",
  226.       ecommerce: {
  227.         items: [{
  228.           item_name: "{{ promo.name }}",
  229.           item_id: "{{ promo.id }}"
  230.           {% if promo.creative|length > 0 %}
  231.           , creative_name: "{{ promo.creative }}"
  232.           {% endif %}
  233.           {% if promo.position|length > 0 %}
  234.           , index: {{ promo.position }}
  235.           {% endif %}
  236.         }]
  237.       }
  238.     });
  239.   {% endif %}
  240.   {% if user_id is defined %}
  241.     dataLayer.push({
  242.       user_id: "{{ user_id }}"
  243.     });
  244.   {% endif %}
  245.   {% if event_name is defined %}
  246.     dataLayer.push({
  247.       event: "{{ event_name }}"
  248.     });
  249.   {% endif %}
  250. </script>
  251. {% endif %}