Google AdSense自动广告取决于屏幕宽度



我想让Google AdSense自动广告只在移动设备上显示锚广告(宽度<800px),而在大屏幕上,我只想要一个特定的广告元素,没有自动广告。

所以我试着在head中加上这个:

<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)"}

有什么建议吗?

应该可以而不是添加参数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,
}
});
}

最新更新