Last active
August 31, 2018 13:46
-
-
Save willviles/061c3ac3a7dc1201767ba0b2994cd052 to your computer and use it in GitHub Desktop.
Quick (untested) overview of how to lazy load Prebid.js ad units on scroll using node in-view library
This file contains hidden or 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
| import inView from 'in-view' | |
| class InViewAdController { | |
| /** | |
| * @func didLoadDOMContent | |
| * @desc Hook for firing after we're sure DOM content is loaded | |
| */ | |
| constructor () { | |
| this.setGlobals() | |
| this.ads = [] | |
| this.offset = -200 | |
| this.getAdUnits() | |
| this.addInViewEventListeners() | |
| } | |
| /** | |
| * @func setGlobals | |
| * @desc Ensures all globals are set safely before we use them | |
| */ | |
| setGlobals () { | |
| window.pbjs = window.pbjs || {} | |
| window.pbjs.que = window.pbjs.que || [] | |
| window.googletag = window.googletag || {} | |
| window.googletag.cmd = window.googletag.cmd || [] | |
| } | |
| /** | |
| * @func getAdUnits | |
| * @desc Gets ad units from DOM and pushes references to ads array | |
| */ | |
| getAdUnits () { | |
| let containers = [].slice.call(document.querySelectorAll('[data-in-view-ad]')) | |
| let newAds = containers.reduce((ads, container) => { | |
| const adId = container.getAttribute('data-in-view-ad') | |
| if (!adId) return ads | |
| const ad = this.getAdUnitConfig(adId) | |
| if (!ad) return ads | |
| ad.container = container | |
| return [...ads, ad] | |
| }, []) | |
| console.log(`Found ${newAds.length} in-view ads`, newAds) | |
| this.ads = [ ...this.ads, ...newAds] | |
| } | |
| /** | |
| * @func injectAdUnit | |
| * @desc Injects ad unit | |
| * @param { Object } ad | |
| */ | |
| injectAdUnit (ad) { | |
| window.googletag.cmd.push(function () { | |
| ad.slot = window.googletag.defineSlot(ad.config.slot, ad.sizes, ad.id).addService(window.googletag.pubads()) | |
| }) | |
| window.pbjs.que.push(function () { | |
| window.pbjs.addAdUnits(ad.config) | |
| }) | |
| ad.container.innerHTML = | |
| `<script type='text/javascript'> | |
| window.googletag.cmd.push(function() { | |
| window.googletag.display('${ad.id}') | |
| }); | |
| </script>` | |
| console.log(`Injected in-view ad unit ${ad.id}`, ad) | |
| } | |
| /** | |
| * @func getAdUnitConfig | |
| * @desc Return ad unit config | |
| * @param { String } adId | |
| * @return { Object } | |
| */ | |
| getAdUnitConfig (adId) { | |
| // Get the correct ad config from your ad unit config | |
| // Example ad below: | |
| return [{ | |
| id: adId, | |
| config: { | |
| slot: '/61424206/90sKidsOnly_BTF_MPU2', | |
| mediaTypes: { | |
| banner: { | |
| sizes: [ | |
| [300, 250] | |
| ] | |
| } | |
| }, | |
| bids: [{ | |
| // Bidder configs | |
| }] | |
| } | |
| }].find((ad) => ad.id === adId) | |
| } | |
| /** | |
| * @func addInViewEventListeners | |
| * @desc Adds inView event listeners for lazy loading ads | |
| */ | |
| addInViewEventListeners () { | |
| inView.offset(this.offset) | |
| this.ads.forEach(ad => { | |
| inView(`[data-in-view-ad="${ad.id}"`).once('enter', () => { | |
| this.injectAdUnit(ad) | |
| this.requestBidsForAdUnit(ad) | |
| this.setTargetingForAdUnit(ad) | |
| }) | |
| }) | |
| } | |
| requestBidsForAdUnit (ad) { | |
| window.pbjs.que.push(() => { | |
| window.pbjs.requestBids({ | |
| timeout: 1500, | |
| adUnitCodes: [ad.id] | |
| }) | |
| }) | |
| } | |
| /** | |
| * @func setTargetingForAdUnit | |
| * @desc Sets the targeting for the ad unit | |
| * @param { Object } ad | |
| */ | |
| setTargetingForAdUnit (ad) { | |
| window.googletag.cmd.push(() => { | |
| window.pbjs.que.push(() => { | |
| window.googletag.enableServices() | |
| window.pbjs.setTargetingForGPTAsync([ad.id]) | |
| window.googletag.pubads().refresh([ad.config.slot]) | |
| }) | |
| }) | |
| } | |
| } |
This file contains hidden or 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
| <div data-in-view-ad="your-config-id"> | |
| <!-- Just create an empty div and style it with the right size for the ad unit --> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment