Advertisement
emanuel1109

Customer Events Example

Oct 15th, 2023 (edited)
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. analytics.subscribe("checkout_completed", event => {
  2.  
  3.   (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  4. new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  5. j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  6. 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  7. })(window,document,'script','dataLayer','GTM-XXXXXXX');
  8.  
  9.   var totalDiscount = 0;
  10.   var shippingDiscount = event.data.checkout.discountApplications.find(discount => discount.targetType === "SHIPPING_LINE");
  11.    
  12.     if (shippingDiscount) {
  13.         shippingDiscount = "true";
  14.         totalDiscount += event.data.checkout.shippingLine.price.amount;
  15.     }
  16.     else {
  17.         shippingDiscount = "false";
  18.     }
  19.  
  20.     event.data.checkout.lineItems.forEach(lineItem => {
  21.       lineItem.discountAllocations.forEach(discountAllocation => {
  22.         totalDiscount += discountAllocation.amount.amount;
  23.       });
  24.     });
  25.  
  26.     var items = event.data.checkout.lineItems.map(item => ({
  27.       item_name: item.variant.product.title,
  28.       item_id: item.variant.product.id,
  29.       currency: item.variant.price.currencyCode,
  30.       price: item.variant.price.amount - item.discountAllocations[0].amount.amount,
  31.       price_before_discount : item.variant.price.amount,
  32.       item_category: item.variant.product.type,
  33.       item_category5: item.variant.title,
  34.       item_variant: item.variant.title,
  35.       quantity: item.quantity
  36.     }));
  37.  
  38.     async function hashString(input) {
  39.       const encoder = new TextEncoder();
  40.       const data = encoder.encode(input);
  41.       const hash = await crypto.subtle.digest('SHA-256', data);
  42.       return Array.from(new Uint8Array(hash)).map(byte => byte.toString(16).padStart(2, '0')).join('');
  43.     }
  44.  
  45.     (async () => {
  46.     const inputString = event.data.checkout.email;
  47.     const hashedEmail = await hashString(inputString);
  48.     dataLayer.push({
  49.       event: 'purchase',
  50.       email: event.data.checkout.email,
  51.       hashed_email: hashedEmail,
  52.       first_name: event.data.checkout.billingAddress.firstName,
  53.       last_name: event.data.checkout.billingAddress.lastName,
  54.       address: event.data.checkout.billingAddress.address1,
  55.       city: event.data.checkout.billingAddress.city,
  56.       state: event.data.checkout.billingAddress.provinceCode,
  57.       country: event.data.checkout.billingAddress.countryCode,
  58.       zipcode: event.data.checkout.billingAddress.zip,
  59.       phone: event.data.checkout.billingAddress.phone,
  60.       ecommerce: {
  61.           currency: event.data.checkout.currencyCode,
  62.           value: event.data.checkout.totalPrice.amount,
  63.           tax: event.data.checkout.totalTax.amount,
  64.           shipping: event.data.checkout.shippingLine.price.amount,
  65.           affiliation: 'example',
  66.           transaction_id: event.data.checkout.order.id,
  67.           coupon: event.data.checkout.discountApplications[0].title,
  68.           shipping_discount: shippingDiscount,
  69.           total_discount: totalDiscount,
  70.           items: items
  71.       }
  72.     });
  73.  
  74.   console.log(dataLayer);
  75. })();
  76.    
  77.  
  78. });
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement