Skip to content

Instantly share code, notes, and snippets.

@iwoodruff
Created May 23, 2019 17:55
Show Gist options
  • Save iwoodruff/02b9eeaeff16274ac318d3e4fa5d998d to your computer and use it in GitHub Desktop.
Save iwoodruff/02b9eeaeff16274ac318d3e4fa5d998d to your computer and use it in GitHub Desktop.
let headline = '';
function createAdBodyWithMacros(macros) {
return `
<div>
<div class="head">
<span id="title">${headline || (macros.title ? macros.title : 'Default Headline')}</span>
</div>
<div class="ads_container"></div>
<div class="footer">
<a href="">Privacy Policy</a>
</div>
</div>
`;
}
const clickedAds = {};
function createAdDiv(adData) {
const adDiv = document.createElement('div');
// Low Fare Ninjas is running a special test – we need to do something different for only them
if (adData.name === 'Low Fare Ninjas') {
adDiv.innerHTML = `<h3 class="ad_card" style="color: cyan !important;">Best Deal: ${adData.name}</h3>`;
} else {
adDiv.innerHTML = `<h4 class="ad_card">${adData.name}</h4>`;
}
clickedAds[adData.name] = false;
return adDiv;
}
function showAdvertiserDeeplink(deepLink) {
const hasACookie = document.cookie.length;
if (hasACookie) {
window.open(deepLink, '_blank');
} else {
window.location.href = deepLink;
}
}
let i = 0;
function pricewireIsAnAdvertiser(ads) {
for (i++; i < ads.length) {
if (ads[i].name === 'Pricewire') {
return true;
}
}
}
function render(options) {
// Pricewire has requested that whenever they show, we also display their affiliated partner, Hotcars
options.ads = options.ads || [];
if (pricewireIsAnAdvertiser(options.ads)) {
options.ads.push({
name: 'Hotcars',
deep_link: 'https://hotcars.net/travel/landing/',
});
}
const advertisementBody = document.createElement('div');
// assemble HTML skeleton with headline macros
headline = options.headerText || headline;
advertisementBody.innerHTML = createAdBodyWithMacros(options.macros);
document.body.appendChild(advertisementBody);
// create HTML for each advertiser & set event bindings
const adsContainer = advertisementBody.getElementsByClassName('ads_container')[0];
options.ads.forEach((adData) => {
const adDiv = createAdDiv(adData);
// on click, travel to an advertiser deeplink
adDiv.addEventListener('click', () => {
if (!clickedAds[adData.name]) { // important! don't go to the same advertiser twice
clickedAds[adData.name] = true;
showAdvertiserDeeplink(adData.deep_ink);
}
});
adsContainer.appendChild(adDiv);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment