// Retrieve the affiliate parameter from the URL
const affiliateId = getQueryParam('affiliate');
// If an affiliate ID is found, store it in sessionStorage and update the URL
if (affiliateId) {
sessionStorage.setItem('affiliateId', affiliateId);
// Remove the query string from the URL without reloading the page
const newUrl = window.location.origin + window.location.pathname;
window.history.replaceState({}, document.title, newUrl);
}
// Retrieve the stored affiliate ID from sessionStorage
const storedAffiliateId = sessionStorage.getItem('affiliateId');
// Update all buy links with the affiliate ID
if (storedAffiliateId) {
const buttons = document.querySelectorAll('a[href^="https://products.nikihutchison.com/adventures-in-marketing"]');
buttons.forEach(button => {
const originalHref = button.getAttribute('href');
// Append or update the affiliate parameter in the URL
const updatedHref = `${originalHref}?affiliate=${storedAffiliateId}`;
button.setAttribute('href', updatedHref);
});
}
});