function getSnowplowDuid(cookieName) { cookieName = cookieName || '_sp_'; var matcher = new RegExp(cookieName + 'id\\.[a-f0-9]+=([^;]+);?'); var match = document.cookie.match(matcher); if (match && match[1]) { return match[1].split('.')[0]; } else { return false; } } var Recommendations = { getRecommendations: function () { let snowplowDuid = getSnowplowDuid() const url = 'https://snowplow.woolford.io:7443/recommendations/' + snowplowDuid; let recommendations = document.getElementById("recommendations") let list = document.createElement("ul") fetch(url).then(response => response.json().then(data => ({ data: data, status: response.status }) ).then(res => { // recommendations returned? if (res.status === 200 && res.data.length > 0) { // append the recommendations to the list res.data.forEach(recommendation => { // don't append the current page to the list of recommendations if (recommendation.pageTitle !== document.title){ let item = document.createElement("li") const recommendationNode = document.createElement("a") recommendationNode.text = recommendation.pageTitle recommendationNode.href = recommendation.pageUrl item.appendChild(recommendationNode) list.appendChild(item) } }) } else { // If there are no recommendations, remove the recommendations section from the page recommendations.remove() } })) recommendations.appendChild(list) } } document.addEventListener('DOMContentLoaded', Recommendations.getRecommendations);