Created
May 23, 2019 17:55
-
-
Save iwoodruff/02b9eeaeff16274ac318d3e4fa5d998d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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