Skip to content

Commit d1ee729

Browse files
committed
fix: allow adblocker
1 parent 0ee5324 commit d1ee729

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

app/components/GoogleScripts.tsx

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,28 @@ import { getLibrary, libraries } from '~/libraries'
55

66
declare global {
77
interface Window {
8-
googletag: any
8+
googletag:
9+
| undefined
10+
| Partial<{
11+
cmd: {
12+
push: (fn: () => void) => void
13+
}
14+
pubads: () => {
15+
enableSingleRequest: () => void
16+
refresh: (slots: any[]) => void
17+
}
18+
enableServices: () => void
19+
display: (id: string) => void
20+
defineSlot: (
21+
path: string,
22+
sizes: [number, number][],
23+
id: string
24+
) => {
25+
addService: (pubads: any) => {
26+
setTargeting: (key: string, value: string[]) => void
27+
}
28+
}
29+
}>
930
}
1031
}
1132

@@ -64,28 +85,32 @@ function Gad({
6485
const adId = adSlot.id
6586

6687
React.useEffect(() => {
67-
if (window.googletag) {
68-
window.googletag.cmd.push(function () {
69-
// Define all ad slots
70-
const slot = window.googletag
71-
.defineSlot('/23278945940/TopLevel', adSlot.sizes, adSlot.id)
72-
.addService(window.googletag.pubads())
73-
.setTargeting(adSlot.targeting, [adSlot.targeting])
74-
75-
window.googletag.pubads().enableSingleRequest()
76-
window.googletag.enableServices()
77-
window.googletag.display(adId)
78-
79-
// Set individual refresh intervals for each ad
80-
const interval = setInterval(function () {
81-
window.googletag.cmd.push(function () {
82-
window.googletag.pubads().refresh([slot])
83-
})
84-
}, adSlot.refreshInterval)
85-
86-
return () => clearInterval(interval)
87-
})
88-
}
88+
const googletag = window.googletag
89+
if (!googletag) return
90+
91+
const cmd = googletag.cmd
92+
if (!cmd) return
93+
94+
cmd.push(function () {
95+
// Define all ad slots
96+
const slot = googletag
97+
.defineSlot?.('/23278945940/TopLevel', adSlot.sizes, adSlot.id)
98+
.addService(googletag.pubads?.())
99+
.setTargeting(adSlot.targeting, [adSlot.targeting])
100+
101+
googletag.pubads?.().enableSingleRequest()
102+
googletag.enableServices?.()
103+
googletag.display?.(adId)
104+
105+
// Set individual refresh intervals for each ad
106+
const interval = setInterval(function () {
107+
cmd.push(function () {
108+
googletag.pubads?.().refresh([slot])
109+
})
110+
}, adSlot.refreshInterval)
111+
112+
return () => clearInterval(interval)
113+
})
89114
}, [])
90115

91116
return (

0 commit comments

Comments
 (0)