orenchuck

soda event+add to card

May 9th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  (function(){
  2.             var sourceURL = 'https://sodastream.com/cart/add.js';
  3.             var productId = '12321242611754';
  4.             var eventType = 'Adoric - ' + 'Carousel Recipe Rose Paloma A/B';
  5.             var data = JSON.stringify({id: productId, quantity: 1});
  6.             var eventElementType = 'Added to the cart';
  7.             var productName = 'Rose Paloma One Item (Soda Press Pink Grapefruit - 500ml)';
  8.             var xhr = new XMLHttpRequest();
  9.             xhr.withCredentials = true;
  10.             xhr.open('POST', sourceURL);
  11.             xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
  12.             function pushCustomGAEvent(eventType, eventElementType, elementId) {
  13.                 if (window.preDataLayer) {
  14.                     window.preDataLayer.push({
  15.                         event: 'newEvent',
  16.                         newEventData: {
  17.                             type: eventType,
  18.                             elementType: eventElementType,
  19.                             elementId: elementId
  20.                         }
  21.                     });
  22.                 }
  23.             }
  24.             function pushEcommerceEvent(productListName, productId) {
  25.                 window.preDataLayer.push({
  26.                     "event": "newEvent",
  27.                     "newEventData": {
  28.                         "ecommerce": {
  29.                             "add": {
  30.                                 "list": productListName,
  31.                                 "products": [
  32.                                     {
  33.                                         "variant_id": productId,
  34.                                         "quantity": 1
  35.                                     }
  36.                                 ]
  37.                             }
  38.                         },
  39.                         "type": "ecommerceData"
  40.                     }
  41.                 })
  42.             }
  43.             xhr.addEventListener('readystatechange', function() {
  44.                 if (this.readyState === 4) {
  45.                     pushCustomGAEvent(eventType, eventElementType, productName);
  46.                     pushEcommerceEvent(eventType, productId);
  47.                 }
  48.             });
  49.             xhr.send([data]);        
  50.         })();
Add Comment
Please, Sign In to add comment