Advertisement
orenchuck

soda event + add to card with fetch

May 18th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 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. fetch(sourceURL, {
  9. method: 'POST',
  10. headers: {
  11. 'Content-Type': 'application/json;charset=utf-8',
  12. 'cache-control': 'no-cache'
  13. },
  14. body: data
  15. }).then(responce => {
  16. if (responce.status == 200){
  17. pushCustomGAEvent(eventType, eventElementType, productName);
  18. window.location.href = window.location.origin + '/cart';
  19. }
  20. }).catch(e => console.error(e))
  21. function pushCustomGAEvent(eventType, eventElementType, elementId) {
  22. if (window.preDataLayer) {
  23. window.preDataLayer.push({
  24. event: 'newEvent',
  25. newEventData: {
  26. type: eventType,
  27. elementType: eventElementType,
  28. elementId: elementId
  29. }
  30. });
  31. }
  32. }
  33. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement