Advertisement
SandroBabic

GA4 EC - purchase - Terme Jezerčica

May 26th, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function prettyText(x){
  2.   return x.replaceAll('+', ' ')
  3.   .replaceAll('%2C', ',')
  4.   .replaceAll('%C5%A0', '\u0160') //Š
  5.   .replaceAll('%C5%A1', '\u0161') //š
  6.   .replaceAll('%C4%8D', '\u010D') //č
  7.   .replaceAll('%C4%8C', '\u010C') //Č
  8.   .replaceAll('%C4%87', '\u0107') //ć
  9.   .replaceAll('%C4%86', '\u0106') //Ć
  10.   .replaceAll('%C7%84', '\u01C4') //DŽ
  11.   .replaceAll('%C7%86', '\u01C6') //dž
  12.   .replaceAll('%C4%91', '\u0111') //đ
  13.   .replaceAll('%C4%90', '\u0110') //Đ
  14.   .replaceAll('%C5%BE', '\u017E') //ž
  15.   .replaceAll('%C5%BD', '\u017D');//Ž
  16. }
  17.  
  18. function extractPrice(x){
  19.   var pattern = '(\"total\"):((\d*\.?\d*)+)';
  20.   const found = x.match(pattern);
  21.   var newNumber = found[2];
  22.   var p = newNumber.match(/[\d\.]+/);
  23.  
  24.   return p[0];
  25. }
  26.  
  27.  
  28. window.dataLayer = window.dataLayer || [];
  29. window.dataLayer.push({
  30.   event: 'purchase',            // name of the event. In this case, it always must be purchase
  31.   ecommerce: {
  32.     currency: '{CURRENCY}',
  33.     value: '{TOTAL}',                       // order total (price of total reservations
  34.     transaction_id: '{RESV}',           // transaction id
  35.     coupon: '{PROMOCODE}',          // if coupon was applied to the reservation, include it here          
  36.     items: [{                           // an array with all accommodation units
  37.       item_name: prettyText('{ROOMNAME}'),           // insert an actual accommodation unit name
  38.       item_id: '{ROOMID}',              // insert an actual accommodation ID
  39.       price: extractPrice('{MULTIROOM}'),                 // insert an actual product price. Number or a string. Don't include currency code
  40.       item_category: prettyText('{HOTELNAME}'),         // insert an actual accommodation unit's top-level category
  41.       item_category2: prettyText('{RATENAME}'),        // insert rate category
  42.       quantity: '1',                    // product quantity, hardcoded value
  43.       arrival_date: '{CHECKIN}',        // insert check-in date
  44.       departure_date: '{CHECKOUT}',     // insert check-out date
  45.       booked_nights: '{NIGHTS}',        // insert total number of nights
  46.       number_of_adults: '{ADULTS}',     // insert total number of adults
  47.       number_of_children: '{CHD}',      // insert total number of children    
  48.     }]
  49.   }
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement