V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Authorization
V2EX  ›  程序员

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

  •  
  •   Authorization · 2022-06-12 14:55:49 +08:00 · 731 次点击
    这是一个创建于 850 天前的主题,其中的信息可能已经有所发展或是发生改变。
    添加 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>
    );
    };
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5803 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:29 · PVG 14:29 · LAX 23:29 · JFK 02:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.