添加 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>
);
};