Skip to content

Commit a35d32d

Browse files
committed
fixes
1 parent 2f0f9ba commit a35d32d

File tree

1 file changed

+35
-49
lines changed

1 file changed

+35
-49
lines changed

app/components/GoogleScripts.tsx

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,25 @@ const adSlots = {
2222
},
2323
rightRail: {
2424
id: 'div-gpt-ad-1738811978953-right-rail',
25-
sizes: [
26-
// [160, 600],
27-
[300, 250],
28-
[250, 250],
29-
],
25+
sizes: [[300, 250]],
3026
targeting: 'right-side-rail',
3127
refreshInterval: 45_000, // 45 seconds
3228
},
3329
leftRail: {
3430
id: 'div-gpt-ad-1738811978953-left-rail',
35-
sizes: [
36-
// [160, 600],
37-
[300, 250],
38-
],
31+
sizes: [[300, 250]],
3932
targeting: 'left-side-rail',
4033
refreshInterval: 45_000, // 45 seconds
4134
},
42-
}
43-
44-
const googleScriptFn = (slots: typeof adSlots) => {
45-
window.googletag = window.googletag || { cmd: [] }
46-
googletag.cmd.push(function () {
47-
// Define all ad slots
48-
const slotInstances = Object.values(slots).map((slot) => {
49-
return googletag
50-
.defineSlot('/23278945940/TopLevel', slot.sizes, slot.id)
51-
.addService(googletag.pubads())
52-
.setTargeting(slot.targeting, [slot.targeting])
53-
})
54-
55-
googletag.pubads().enableSingleRequest()
56-
googletag.enableServices()
57-
58-
// // Set individual refresh intervals for each ad
59-
// slotInstances.forEach((slotInstance, index) => {
60-
// const slot = Object.values(slots)[index]
61-
// setInterval(function () {
62-
// googletag.cmd.push(function () {
63-
// googletag.pubads().refresh([slotInstance])
64-
// })
65-
// }, slot.refreshInterval)
66-
// })
67-
})
68-
}
35+
} satisfies Record<
36+
string,
37+
{
38+
id: string
39+
sizes: [number, number][]
40+
targeting: string
41+
refreshInterval: number
42+
}
43+
>
6944

7045
export function GoogleScripts() {
7146
return (
@@ -74,24 +49,38 @@ export function GoogleScripts() {
7449
async
7550
src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"
7651
></script>
77-
78-
<script
79-
dangerouslySetInnerHTML={{
80-
__html: `(${googleScriptFn.toString()})(${JSON.stringify(adSlots)});`,
81-
}}
82-
/>
8352
</>
8453
)
8554
}
8655

8756
function Gad({
88-
adId,
57+
name,
8958
children,
9059
...props
91-
}: { adId: string } & React.HTMLAttributes<HTMLDivElement>) {
60+
}: { name: keyof typeof adSlots } & React.HTMLAttributes<HTMLDivElement>) {
61+
const adSlot = adSlots[name]!
62+
const adId = adSlot.id
63+
9264
React.useEffect(() => {
9365
window.googletag.cmd.push(function () {
66+
// Define all ad slots
67+
const slot = window.googletag
68+
.defineSlot('/23278945940/TopLevel', adSlot.sizes, adSlot.id)
69+
.addService(window.googletag.pubads())
70+
.setTargeting(adSlot.targeting, [adSlot.targeting])
71+
72+
window.googletag.pubads().enableSingleRequest()
73+
window.googletag.enableServices()
9474
window.googletag.display(adId)
75+
76+
// // Set individual refresh intervals for each ad
77+
// const interval = setInterval(function () {
78+
// window.googletag.cmd.push(function () {
79+
// window.googletag.pubads().refresh([slot])
80+
// })
81+
// }, slot.refreshInterval)
82+
83+
// return () => clearInterval(interval)
9584
})
9685
}, [])
9786

@@ -121,10 +110,7 @@ export function GadLeader() {
121110

122111
export function GadFooter() {
123112
return (
124-
<Gad
125-
adId={adSlots.footer.id}
126-
style={{ maxWidth: '728px', aspectRatio: '728 / 90' }}
127-
/>
113+
<Gad name="footer" style={{ maxWidth: '728px', aspectRatio: '728 / 90' }} />
128114
)
129115
}
130116

@@ -142,7 +128,7 @@ export function GadLeftRailSquare() {
142128
export function GadRightRailSquare() {
143129
return (
144130
<Gad
145-
adId={adSlots.rightRail.id}
131+
name="rightRail"
146132
className="[aspect-ratio:250/250] xl:[aspect-ratio:300/250] flex items-center justify-center"
147133
>
148134
<Link

0 commit comments

Comments
 (0)