SandroBabic

GA4 EC - purchase - Camping Polidor

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