Advertisement
RodrigoOrnelas

DL - purchase - Shopify

Mar 7th, 2024
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 7.14 KB | Source Code | 0 0
  1. <!-- INICIO SCRIPT PARA GERAR DATALAYER →
  2.  
  3. {% comment %} Purchase data layer v2.1 - part of "Shopify GA4 Kit" by Analyzify
  4. Visit https://analyzify.app/shopify-google-analytics/ga4 for complete tutorial
  5. {% endcomment %}
  6.  
  7. {% assign template_name = template.name %}
  8.  
  9. <script type="text/javascript">
  10. window.dataLayer = window.dataLayer || [];
  11.  
  12. window.appStart = function(){
  13.   window.allPageHandle = function(){
  14.     window.dataLayer.push({
  15.       event: "ga4kit_info",
  16.       contentGroup: "{{ template_name }}",
  17.       {% if customer %}
  18.       userType: "member",
  19.       customer: {
  20.         id: "{{- checkout.customer.id | json -}}",
  21.         lastOrder: "{{- customer.last_order.created_at | date: '%B %d, %Y %I:%M%p' -}}",
  22.         orderCount: "{{- checkout.customer.orders_count | json -}}",
  23.         totalSpent: "{{- checkout.customer.total_spent | times: 0.01 | json -}}",
  24.         tags: {{- checkout.customer.tags | json -}}
  25.       }
  26.       {% else %}
  27.         userType: "visitor",
  28.       {% endif %}
  29.     });
  30.   };
  31.   allPageHandle();
  32.  
  33. {% if first_time_accessed and post_purchase_page_accessed != true %}
  34.  
  35.   var shippingPrice = "{{shipping_price | money_without_currency }}".replace(",", ".");
  36.   var totalPrice = "{{checkout.total_price | money_without_currency }}".replace(",", ".");
  37.   var taxPrice = "{{tax_price | money_without_currency }}".replace(",", ".");
  38.   var orderItemsName = [];
  39.   var orderItemsId = [];
  40.   var orderItemsCategory = [];
  41.   var orderItemsBrand = [];
  42.   var orderItemsType = [];
  43.   var orderItemsPrice = [];
  44.   var orderItemsSku = [];
  45.   var orderItemsvariantId = [];
  46.   var orderItemsQuantity = [];
  47.   var orderItemsvariantTitle = [];
  48.   var totalQuantity = 0;
  49.  
  50.   {% for line_item in checkout.line_items %}  
  51.       orderItemsName.push("{{ line_item.product.title | remove: "'" | remove: '"'}}");
  52.       orderItemsId.push("{{ line_item.product_id }}");
  53.       orderItemsPrice.push("{{ line_item.price | times: 0.01 }}");
  54.       orderItemsSku.push("{{ line_item.sku | remove: "'" | remove: '"' }}");
  55.       orderItemsQuantity.push("{{ line_item.quantity }}");
  56.       orderItemsvariantId.push("{{ line_item.variant_id }}");
  57.       orderItemsvariantTitle.push("{{ line_item.variant.title }}");
  58.       orderItemsCategory.push("{{ line_item.product.collections.last.title | remove: "'" | remove: '"' }}");
  59.       orderItemsBrand.push("{{ line_item.vendor | remove: "'" | remove: '"' }}");
  60.       orderItemsType.push("{{ line_item.product.type | remove: "'" | remove: '"' }}");
  61.       totalQuantity += {{ line_item.quantity }};
  62.   {% endfor %}
  63.  
  64.   window.dataLayer.push({  
  65.       page_type: "purchase",
  66.       event: "analyzify_purchase",
  67.       currency: "{{ shop.currency }}",
  68.       totalValue: totalPrice,
  69.       totalValueStatic: totalPrice,
  70.       currencyRate: window.Shopify.currency.rate,
  71.       shipping: shippingPrice,
  72.       tax: taxPrice,
  73.       payment_type: "{{order.transactions[0].gateway}}",
  74.       {% if order.name %}
  75.       transaction_id: "{{order.name | remove: "'" | remove: '"'}}",
  76.       {% else %}
  77.       transaction_id: "{{checkout.id | remove: "'" | remove: '"'}}",
  78.       {% endif %}
  79.       productName: orderItemsName,
  80.       productId: orderItemsId,
  81.       productBrand: orderItemsBrand,
  82.       productCategory: orderItemsCategory,
  83.       productVariantId: orderItemsvariantId,
  84.       productVariantTitle: orderItemsvariantTitle,
  85.       productSku: orderItemsSku,
  86.       productType: orderItemsSku,
  87.       productPrice: orderItemsPrice,
  88.       productQuantity: orderItemsQuantity,
  89.   });
  90.  
  91. {% endif %}
  92.  
  93. }
  94. appStart();
  95. </script>
  96.  
  97. <script>
  98.   var ecommerce = {
  99.       'transaction_id': '{{checkout.order_number  | json}}',
  100.       'affiliation': {{shop.name | json}},
  101.       'value': {{checkout.total_price | money_without_currency | replace: ',', '.' | json}},
  102.       'tax': {{checkout.tax_price | money_without_currency | replace: ',','.' | json}},
  103.       'shipping': {{checkout.shipping_price | money_without_currency | replace: ',','.' | json}},
  104.       'subtotal': {{checkout.subtotal_price | money_without_currency| replace: ',','.' | json}},
  105.       'currency': {{checkout.currency | json}},
  106.        {% for discount in checkout.discounts %}
  107.       'coupon': {{discount.code | json}},
  108.       'discount'  : {{discount.amount | money_without_currency | json}},
  109.        {% endfor %}
  110.       'email': {{checkout.email | json}},
  111.       'phone': {{checkout.phone | json}},
  112.       'first_name': {{customer_address.first_name | json}},
  113.        'fullName'  : {{checkout.billing_address.name | json}},
  114.        'firstName' : {{checkout.billing_address.first_name | json}},
  115.        'lastName'  : {{checkout.billing_address.last_name | json}},
  116.        'address1'  : {{checkout.billing_address.address1 | json}},
  117.        'address2'  : {{checkout.billing_address.address2 | json}},
  118.        'street'    : {{checkout.billing_address.street | json}},
  119.        'city'      : {{checkout.billing_address.city | json}},
  120.        'province'  : {{checkout.billing_address.province | json}},
  121.        'province_code'  : {{checkout.billing_address.province_code | json}},
  122.        'customer_id'  : {{checkout.customer_id | json}},
  123.        'zip'       : {{checkout.billing_address.zip | json}},
  124.        'country'   : {{checkout.billing_address.country | json}},
  125.        'country_code'   : {{checkout.billing_address.country_code | json}},
  126.        'phone'     : {{checkout.billing_address.phone | json}},
  127.       'items':[{% for line_item in checkout.line_items %}{
  128.                         'item_id'          :{{line_item.product.id | json}},                  
  129.                         'item_variant'    : {{line_item.variant.title | json}},            
  130.                         'item_name'      : {{line_item.product.title | json}},
  131.                         'price'           : {{line_item.product.price | money_without_currency | replace: ',', '.' | json}},
  132.                         'item_brand'      : {{line_item.product.vendor | json}},
  133.                         'item_category'   : {{line_item.product.type | json}},
  134.                         'item_list_name'  : {{line_item.collection.title | json}},
  135.                         'quantity'        : {{line_item.quantity | json}},
  136.                         'discount'        : {{discount.code | json}}
  137.                     },{% endfor %}],
  138.   };
  139. (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  140. new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  141. j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  142. 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  143. })(window,document,'script','dataLayer','GTM-M5LTPDF’);
  144.       window.dataLayer.push({
  145.         'pageType': 'purchase',
  146.         'event': 'purchase',
  147.         ecommerce
  148.         });
  149. </script>
  150.  
  151.  
  152. <!-- Google Tag Manager -->
  153. <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  154. new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  155. j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  156. 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  157. })(window,document,'script','dataLayer','GTM-M5LTPDF');</script>
  158. <!-- End Google Tag Manager -->
  159.  
  160.  
  161. <!-- FIM SCRIPT PARA GERAR DATALAYER -->
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement