-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: core react component for google publisher tag ads - removed pac…
…kage-lock from commit
- Loading branch information
1 parent
df41fda
commit f824424
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useScript } from '@10up/react-hooks/src/useScript/useScript'; | ||
|
||
type GPTProps = { | ||
adUnitPath: string; | ||
sizes: Array<Array<number>>; | ||
divId: string; | ||
targeting?: Record<string, string>; | ||
enableSra?: boolean; | ||
minWidth?: number; | ||
minHeight?: number; | ||
}; | ||
|
||
declare global { | ||
interface Window { | ||
googletag: any; | ||
enableSra?: boolean; | ||
} | ||
} | ||
|
||
const AdvertGPT: React.FC<GPTProps> = ({ | ||
adUnitPath, | ||
sizes, | ||
divId, | ||
targeting = {}, | ||
enableSra = false, | ||
minWidth = 0, | ||
minHeight = 0, | ||
}): JSX.Element => { | ||
const [scriptLoaded] = useScript( | ||
'https://securepubads.g.doubleclick.net/tag/js/gpt.js', | ||
0, | ||
'head', | ||
); | ||
|
||
useEffect(() => { | ||
if (typeof window !== 'undefined' && scriptLoaded) { | ||
const { googletag } = window; | ||
googletag.cmd.push(() => { | ||
const slot = googletag.defineSlot(adUnitPath, sizes, divId); | ||
for (const [key, value] of Object.entries(targeting)) { | ||
slot.setTargeting(key, value); | ||
} | ||
slot.addService(googletag.pubads()); | ||
if (enableSra) { | ||
googletag.pubads().enableSingleRequest(); | ||
window.enableSra = true; | ||
} | ||
googletag.enableServices(); | ||
googletag.display(divId); | ||
}); | ||
} | ||
}, [adUnitPath, sizes, targeting, enableSra, divId, scriptLoaded]); | ||
|
||
const style: React.CSSProperties = { | ||
minWidth: minWidth ? `${minWidth}px` : 'auto', | ||
minHeight: minHeight ? `${minHeight}px` : 'auto', | ||
}; | ||
|
||
return <div id={divId} style={style} />; | ||
}; | ||
|
||
export default AdvertGPT; |
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