Skip to content

Commit

Permalink
Fix server side rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Saigredan committed Feb 8, 2021
1 parent 47f8b5e commit 3e3559c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 66 deletions.
106 changes: 58 additions & 48 deletions src/Utils/AdManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,70 @@ export class AdManager {
targeting?: { [key: string]: googletag.NamedSize }
): Promise<googletag.Slot> {
return new Promise((resolve, reject) => {
window.Yieldbird.cmd.push(() => {
window.googletag.cmd.push(() => {
const slot = this.createSlot(adUnitPath, size, optDiv)

this.setTargeting(slot, targeting)
this.setSizeMapping(slot, sizeMapping)

if (!shouldRefreshAds) {
window.Yieldbird.setGPTTargeting([slot])
window.googletag.enableServices()
window.googletag.display(optDiv)
window.googletag.pubads().refresh([slot])
} else {
window.googletag.enableServices()
window.googletag.display(optDiv)
}

slot ? resolve(slot) : reject(new Error('Slot could not be created.'))
if (typeof window !== 'undefined') {
window.Yieldbird.cmd.push(() => {
window.googletag.cmd.push(() => {
const slot = this.createSlot(adUnitPath, size, optDiv)

this.setTargeting(slot, targeting)
this.setSizeMapping(slot, sizeMapping)

if (!shouldRefreshAds) {
window.Yieldbird.setGPTTargeting([slot])
window.googletag.enableServices()
window.googletag.display(optDiv)
window.googletag.pubads().refresh([slot])
} else {
window.googletag.enableServices()
window.googletag.display(optDiv)
}

slot
? resolve(slot)
: reject(new Error('Slot could not be created.'))
})
})
})
} else {
reject(new Error('Slot could not be created.'))
}
})
}

public static destroySlot(optDiv: string) {
window.googletag.cmd.push(() => {
const slot = window.googletag
.pubads()
.getSlots()
.find((el) => el.getSlotElementId() === optDiv)

slot && window.googletag.destroySlots([slot])
})
if (typeof window !== 'undefined') {
window.googletag.cmd.push(() => {
const slot = window.googletag
.pubads()
.getSlots()
.find((el) => el.getSlotElementId() === optDiv)

slot && window.googletag.destroySlots([slot])
})
}
}

public refreshSlot(slot: googletag.Slot, optDiv: string) {
this.adsToRefresh[optDiv] = slot

this.interval && window.clearInterval(this.interval)
this.interval = window.setTimeout(
() => {
const slots = Object.keys(this.adsToRefresh).map(
(el) => this.adsToRefresh[el]
)

if (slots.length > 0) {
window.Yieldbird.cmd.push(() => {
window.Yieldbird.refresh(slots)
this.adsToRefresh = {}
})
}
},
this.timeout,
true
)
if (typeof window !== 'undefined') {
this.adsToRefresh[optDiv] = slot

this.interval && window.clearInterval(this.interval)
this.interval = window.setTimeout(
() => {
const slots = Object.keys(this.adsToRefresh).map(
(el) => this.adsToRefresh[el]
)

if (slots.length > 0) {
window.Yieldbird.cmd.push(() => {
window.Yieldbird.refresh(slots)
this.adsToRefresh = {}
})
}
},
this.timeout,
true
)
}
}

private static createSlot(
Expand Down Expand Up @@ -107,9 +117,9 @@ export class AdManager {
slot: googletag.Slot,
targeting?: { [key: string]: googletag.NamedSize }
) {
if (targeting) {
if (slot && targeting) {
Object.keys(targeting).forEach((targetingKey: string) => {
slot && slot.setTargeting(targetingKey, targeting[targetingKey])
slot.setTargeting(targetingKey, targeting[targetingKey])
})
}
}
Expand Down
40 changes: 22 additions & 18 deletions src/Utils/headerScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@ function loadScript(scriptUrl: string) {
}

export function ensureScripts() {
window.googletag = window.googletag || {}
window.googletag.cmd = window.googletag.cmd || []
window.Yieldbird = window.Yieldbird || {}
window.Yieldbird.cmd = window.Yieldbird.cmd || []
if (typeof window !== 'undefined') {
window.googletag = window.googletag || {}
window.googletag.cmd = window.googletag.cmd || []
window.Yieldbird = window.Yieldbird || {}
window.Yieldbird.cmd = window.Yieldbird.cmd || []
}
}

export function initializeAdStack(uuid: string) {
ensureScripts()
window.yb_configuration = { lazyLoad: true }
if (typeof window !== 'undefined') {
ensureScripts()
window.yb_configuration = { lazyLoad: true }

window.googletag.cmd.push(function () {
window.googletag.pubads().disableInitialLoad()
})
window.googletag.cmd.push(function () {
window.googletag.pubads().disableInitialLoad()
})

if (Object.keys(window.googletag).length <= 1) {
loadScript(
`${document.location.protocol}//securepubads.g.doubleclick.net/tag/js/gpt.js`
)
}
if (Object.keys(window.googletag).length <= 1) {
loadScript(
`${document.location.protocol}//securepubads.g.doubleclick.net/tag/js/gpt.js`
)
}

if (Object.keys(window.Yieldbird).length <= 1) {
loadScript(
`${document.location.protocol}//jscdn.yieldbird.com/${uuid}/yb.js`
)
if (Object.keys(window.Yieldbird).length <= 1) {
loadScript(
`${document.location.protocol}//jscdn.yieldbird.com/${uuid}/yb.js`
)
}
}
}

0 comments on commit 3e3559c

Please sign in to comment.