orenchuck

soda GA event + add to card

May 9th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. (function(){
  2. var sourceURL = window.location.origin + '/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. xhr.setRequestHeader('cache-control', 'no-cache');
  13. function pushCustomGAEvent(eventType, eventElementType, elementId) {
  14. if (window.preDataLayer) {
  15. window.preDataLayer.push({
  16. event: 'newEvent',
  17. newEventData: {
  18. type: eventType,
  19. elementType: eventElementType,
  20. elementId: elementId
  21. }
  22. });
  23. }
  24. }
  25. xhr.addEventListener('readystatechange', function() {
  26. if (this.readyState === 4) {
  27. pushCustomGAEvent(eventType, eventElementType, productName);
  28. window.location.href = window.location.origin + '/cart';
  29. }
  30. });
  31. xhr.send([data]);
  32. })();
Add Comment
Please, Sign In to add comment