Skip to content

Commit 7ea0284

Browse files
committed
update ga
1 parent cf2041d commit 7ea0284

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

app/components/GoogleScripts.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ const adSlots = {
4141
},
4242
}
4343

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+
}
69+
4470
export function GoogleScripts() {
4571
return (
4672
<>
@@ -51,33 +77,7 @@ export function GoogleScripts() {
5177

5278
<script
5379
dangerouslySetInnerHTML={{
54-
__html: `
55-
(() => {
56-
const adSlots = ${JSON.stringify(adSlots)};
57-
58-
window.googletag = window.googletag || {cmd: []};
59-
googletag.cmd.push(function() {
60-
// Define all ad slots
61-
Object.values(adSlots).forEach(slot => {
62-
googletag.defineSlot('/23278945940/TopLevel', slot.sizes, slot.id)
63-
.addService(googletag.pubads())
64-
.setTargeting(slot.targeting, [slot.targeting]);
65-
});
66-
67-
googletag.pubads().enableSingleRequest();
68-
googletag.enableServices();
69-
});
70-
71-
// Set individual refresh intervals for each ad
72-
Object.values(adSlots).forEach(slot => {
73-
setInterval(function() {
74-
googletag.cmd.push(function() {
75-
googletag.pubads().refresh([googletag.slots[slot.id]]);
76-
});
77-
}, slot.refreshInterval);
78-
});
79-
})();
80-
`,
80+
__html: `(${googleScriptFn.toString()})(${JSON.stringify(adSlots)});`,
8181
}}
8282
/>
8383
</>

0 commit comments

Comments
 (0)