0

我只想在移动设备上启用 Google AdSense Autoads 以显示锚定广告(宽度<800px),而在大屏幕上我只需要特定的广告元素而不需要 Autoads。

所以我尝试在头部添加这个:

<script sync src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxx" crossorigin="anonymous""></script>

    <script>
        if (screen.width<800) {
        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-xxxx",
            enable_page_level_ads: true,
            overlays: {
                bottom: true,
            }
        });
    }
    </script>

在体内:

<style>  
@media (max-width:800px) {
    .ads-right {
        display: none;
    }
}
</style>
<div class="ads-right">    
        <ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-xxxxxxx" data-ad-slot="yyyyy" data-ad-format="rectangle" data-full-width-responsive="true"></ins>
        <script>
            if (screen.width>800) {
                (adsbygoogle = window.adsbygoogle || []).push({});
            }            
        </script>
    </div>

结果是它可以工作,但是在小屏幕(宽度<800px)上,我在控制台中收到此错误,并且锚广告位于顶部,而不是底部:

Uncaught N {message: "adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page.", name: 'TagError', pbr: true, stack: "TagError: adsbygoogle.push() error: Only one 'enab…google.js?client=ca-pub-xxxxx:222:259)"}

有什么建议么?

4

1 回答 1

0

这应该可以使用经典的旧代码而不是向 adsbygoogle.js 添加参数。

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" crossorigin="anonymous"></script>

enable_page_level_ads 应该在脚本中调用一次

if (screen.width<800) {
        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-xxxx",
            enable_page_level_ads: true,
            overlays: {
                bottom: true,
            }
        });
    } else {

        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-xxxx",
            enable_page_level_ads: false,
            overlays: {
                bottom: true,
            }
        });
}
于 2021-09-26T07:37:24.597 回答