关于 google ad manager 代码,请高手指教。

2022-06-12 14:55:49 +08:00
 Authorization
添加 google ad manager 后,正确显示 adsense 的广告。但是在控制台会报错:
GPT] Exception in googletag.cmd function: TypeError: Cannot read properties of null (reading 'addService').
到底是哪个环节的问题?
下面是本地的代码
import Head from 'next/head';
import { useEffect } from 'react';

const useDfpSlot = ({ path, size, id }) => {
useEffect(() => {
if(window){
const googletag = window['googletag'] || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(() => {
googletag.defineSlot(path, size, id).addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
googletag.display(id);
});
}
}, [path, size, id]);
};

// 侧边栏广告
export const AdSidebar = () => {
useDfpSlot({
path: '/20000000000/Sidebar',
size: [160,600],
id: 'div-gpt-ad-10000000000-0',
});

return (
<div
id="div-gpt-ad-10000000000-0"
style={{
display: 'inline-block',
width: '160px',
height: '600px',
margin: 'auto'
}}
>
</div>
);
};

// 收件箱广告
export const MailAd = () => {
useDfpSlot({
path: '/20000000000/ads/inbox-Ad',
size: [[728, 90],[970, 90]],
id: 'div-gpt-ad-10000000000-0',
});

return (
<div
id="div-gpt-ad-10000000000-0"
style={{
display: 'inline-block',
width: '970px',
height: '90px',
margin: 'auto',
}}
>
</div>
);
};


// 首页广告
export const MainPageAd = () => {
useDfpSlot({
path: '/20000000000/ads',
size: [728, 90],
id: 'div-gpt-ad-10000000000-0',
});

return (
<div
id="div-gpt-ad-10000000000-0"
style={{
display: 'inline-block',
width: '728px',
height: '90px',
margin: 'auto',
}}
>
</div>
);
};
732 次点击
所在节点    程序员
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/859066

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX