Last active
October 17, 2021 03:37
-
-
Save VishalTaj/1656b1a4eed0a8d75323cc8cfff23f16 to your computer and use it in GitHub Desktop.
Next.js GPT
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
import { useEffect, useState } from 'react' | |
import { adDevice } from "./lib/dfp/const" | |
export default function AdSlot (props) { | |
const { id, customStyle } = props | |
const [showAdd, setShowAdd] = useState(false) | |
useEffect(() => { | |
if ((window.screen.width < 768 && adDevice[id] !== "d") || | |
(window.screen.width >= 768 && adDevice[id] !== "m")) | |
setShowAdd(true) | |
return () => {} | |
}, []) | |
return ( | |
showAdd ? <div id={id} style={customStyle} /> : <></> | |
) | |
} |
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
export const dfpConst = { | |
prefix: 'AD_PREFIX', | |
slot_id: '1234567' | |
} | |
export const adDevice = { | |
"div-gpt-ad-1234567890123-0": "d", //Billboard | |
"div-gpt-ad-0987654321234-0": "m", | |
} |
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
import { dfpConst, adDevice } from './const' | |
export const removeSlot = function () { | |
const { googletag } = window | |
googletag.cmd.push(function () { | |
googletag.destroySlots() | |
}) | |
} | |
export const defineSlot = function (name, id, sizes = [], params = {}) { | |
if (window) { | |
const { googletag, lotaudsList, screen } = window | |
const { dfpTargetingParams } = params | |
if (screen.width < 768 && adDevice[id] === "d") { return; } | |
if (screen.width >= 768 && adDevice[id] === "m") { return; } | |
const render = () => { | |
googletag.defineSlot(`/${dfpConst.slot_id}/${dfpConst.prefix}_${name}`, sizes, id).addService(googletag.pubads()) | |
googletag.pubads().setTargeting('lotauds', lotaudsList).setTargeting('section', dfpTargetingParams.section).setTargeting('pos', dfpTargetingParams.pos) | |
googletag.pubads().enableSingleRequest() | |
googletag.pubads().collapseEmptyDivs() | |
googletag.enableServices() | |
googletag.display(id) | |
} | |
googletag.cmd.push(function () { | |
try { | |
render(); | |
} catch(e) { | |
removeSlot(); | |
render(); | |
} | |
}) | |
} | |
} | |
export const defineOutOfPageSlot = function (name, id, params = {}) { | |
if (window) { | |
const { googletag, lotaudsList, screen } = window | |
const { dfpTargetingParams } = params | |
if (screen.width < 768 && adDevice[id] === "d") { return; } | |
if (screen.width >= 768 && adDevice[id] === "m") { return; } | |
const render = () => { | |
googletag.defineOutOfPageSlot(`/${dfpConst.slot_id}/${dfpConst.prefix}_${name}`, id).addService(googletag.pubads()) | |
googletag.pubads().setTargeting('lotauds', lotaudsList).setTargeting('section', dfpTargetingParams.section).setTargeting('pos', dfpTargetingParams.pos) | |
googletag.pubads().collapseEmptyDivs() | |
googletag.enableServices() | |
googletag.display(id) | |
} | |
googletag.cmd.push(function () { | |
try { | |
render(); | |
} catch(e) { | |
removeSlot(); | |
render(); | |
} | |
}) | |
} | |
} |
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
import { useRouter } from 'next/router' | |
import { removeSlot, defineSlot } from './lib/dfp' | |
import AdSlot from './components/AdSlot' | |
function Home(props) { | |
const router = useRouter() | |
useEffect(() => { | |
const dfpTargetingParams = { section: ['Homepage'], pos: 'about' } | |
defineSlot('Leaderboard', 'div-gpt-ad-12345678901234-0', [300, 250], { dfpTargetingParams}) | |
router.events.on('routeChangeComplete', removeSlot) | |
return () => { | |
router.events.off('routeChangeComplete', removeSlot) | |
} | |
}, []) | |
return(<> | |
<Head> | |
<script async='async' src='https://securepubads.g.doubleclick.net/tag/js/gpt.js' /> | |
<script> | |
{`var googletag = googletag || {}; | |
googletag.cmd = googletag.cmd || [];`} | |
</script> | |
</Head> | |
<AdSlot id={'div-gpt-ad-12345678901234-0'} /> | |
</>) | |
} |
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
if(!googletag) { const googletag = window.googletag } | |
if (googletag) { | |
googletag.pubads().addEventListener('slotRenderEnded', function (event) { | |
if (event.slot.getSlotElementId() == 'div-gpt-15487568756655') {// if slot is filled | |
if (event.isEmpty == true) { | |
// true if ad slot is empty | |
} else { | |
// if ad slot present/delivered | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment