Advertisement
Guest User

Mini Rodini A/B-test

a guest
Dec 16th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function getRecommendations(url) {
  2.     const splittedUrl = url.split('/');
  3.     const slug = splittedUrl[splittedUrl.length - 1].split('?')[0];
  4.     const response = await fetch('https://depict-ai.appspot.com/recommendations', {
  5.         body: ' { "store": "mini rodini", "slug": "' + slug + '" } ',
  6.         headers: {
  7.             Accept: 'application/json',
  8.             'Content-Type': 'application/json'
  9.         },
  10.         method: 'POST'
  11.     });
  12.     const obj = await response.json();
  13.     return obj;
  14. };
  15.  
  16. function getRegion() {
  17.   return document.location.href.split('/')[3];
  18. }
  19.  
  20. function populateRecommendations(recommendations, region) {
  21.   for (let idx=0; idx<recommendations.length; idx++) {
  22.     const titleElement = document.querySelectorAll('.product-item-details p a')[idx];
  23.     titleElement.innerText = recommendations[idx]['title'].toUpperCase();
  24.  
  25.     const priceElement = document.querySelectorAll('.product-item-details .price')[idx];
  26.     priceElement.innerText = 'SEK ' + recommendations[idx]['price'];
  27.  
  28.     const imageElement = document.querySelectorAll('.product-item-photo img')[idx];
  29.     imageElement.src = recommendations[idx]['imageUrl'].replace('en-se', region);
  30.  
  31.     const linkElement1 = document.querySelectorAll('.product-item-photo')[idx];
  32.     linkElement1.href = recommendations[idx]['pageUrl'].replace('en-se', region);
  33.  
  34.     const linkElement2 = document.querySelectorAll('.product-item-details > p > a')[idx];
  35.     linkElement2.href = recommendations[idx]['pageUrl'].replace('en-se', region);
  36.   }
  37. }
  38.  
  39. const recommendationObjects = await getRecommendations(document.location.href);
  40.  
  41. const region = getRegion();
  42.  
  43. populateRecommendations(recommendationObjects['beforePurchase'], region);
  44.  
  45. document.querySelector('#product-addtocart-button').addEventListener("click", function(){
  46.   populateRecommendations(recommendationObjects['afterPurchase'], region);
  47. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement