如何在同一页上使用多个Adsense广告单元?

36

如何在一个网站上放置多个AdSense广告单元?Google提供的代码只能针对每个单元。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-123456"
     data-ad-slot="123456"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
如果我想在一个网站上使用多个 AdSense 广告单元,该怎么办? 我只使用一次 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>(adsbygoogle = window.adsbygoogle || []).push({});,然后放置 <ins ...></ins> 代码到我想要的位置。

问题是只有第一个广告单元会被解析和显示。 要显示多个广告单元,需要做什么?

这是我如何使用它(只显示第一个 ins):

<!doctype html>
<html>
    <body>
        <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="first"></ins>

         <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="second"></ins>

         <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="third"></ins>

        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
    </body>
</html>
4个回答

104

如果要在页面上添加多个广告单元,则需要添加更多的代码行 (adsbygoogle = window.adsbygoogle || []).push({});

所以,如果你有3个广告单元,你需要使用它3次。

(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});

如果您想要动态地执行此操作,请使用以下内容:

[].forEach.call(document.querySelectorAll('.adsbygoogle'), function(){
    (adsbygoogle = window.adsbygoogle || []).push({});
});

2
我仍然只能在每页上看到一个。 - Abram
当我包含HTML片段而不仅仅是JS代码时,这对我很有效!<ins class="adsbygoogle" style="display:inline-block;width:300px;height:250px" data-ad-client="ca-pub-123456" data-ad-slot="123456"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); - BlackBurn027
如果我在同一页中两次使用相同的"data-ad-slot",那么第二个广告显示是否会被计算为单独的展示次数?还是我需要使用不同的"data-ad-slot"来从第二个显示的广告中获得收入? - JAT86
每个广告单元都有一个唯一的data-ad-slot ID,对吧?我可以在同一页上为同一个广告单元(相同的data-ad-slot)放置两个广告吗?谢谢。 - cbdeveloper

17

使用jQuery...

$(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); });

6

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>代码放置在页面底部(就在</body>标签前面)并仅调用一次。

接下来,将广告片段分开放置,像这样:

<!-- Top Banner Ad -->
<ins class="adsbygoogle"
    style="display:inline-block;width:320px;height:100px"
    data-ad-client="ca-pub-1234567890"
    data-ad-slot="4693644638"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<!-- Responsive Ad -->
<ins class="adsbygoogle"
    style="display:block"
    data-ad-client="ca-pub-1234567890"
    data-ad-slot="3097818646"
    data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

如果您将包含adsbygoogle.js的<script>放置在push()调用下面,那么这不会成为一个问题吗?难道您不需要更早地包含它吗? - Danger
@危险:你认为这会是个问题吗? - Hassan Baig
每个广告单元都有一个唯一的data-ad-slot ID,对吧?我能在同一页上为同一个广告单元(相同的data-ad-slot)放置两个广告吗?谢谢。 - cbdeveloper

-5
如果您想在同一页面上使用多个 AdSense 单元,那么您需要创建并粘贴多个 AdSense 代码片段。
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-123456"
     data-ad-slot="first"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-123456"
     data-ad-slot="second"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-123456"
     data-ad-slot="third"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

AdSense 中只允许有限数量的代码修改。 https://support.google.com/adsense/answer/1354736?hl=zh-Hans

我可能可以回答为什么“仅解析并显示第一个 AdSense 单元”,并尝试向您展示如何修改示例以显示所有三个广告,但在我看来这是不相关的(在本例中),因为它在 AdSense 中是不允许的。(而且可能完全没有必要。您可以简单地粘贴三个广告代码片段,或者 - 重复使用同一个片段三次。)


7
请参见http://exisweb.net/using-google-adsense-async-tags-for-responsive-design。将adsbygoogle.js脚本包含超过一次是不好的做法。一次足矣。 - Emmanuel Gleizer
1
@EmmanuelGleizer 只是好奇,那么为什么 Google 的帮助文档没有提到呢? - Justin Skiles
1
@Emmanuel Gleizer - AdSense不是StackOverflow(例如-如果您的AdSense帐户被禁用,则无法“自我接受”您的上诉),它与编程无关,AdSense是业务,这在AdSense中不是“不良实践”。您是正确的:“_一次就足够了_”,这是正确的。 - galeksic
1
@Justin Skiles - 是的,AdSense文档中确实如此,可以在这里找到:https://support.google.com/adsense/answer/3221666?hl=zh_CN 和这里:http://googledevelopers.blogspot.com/2013/07/an-async-script-for-adsense-tagging.html - galeksic
从给定的链接中引用: “为了充分利用异步代码,我们建议您同时切换给定页面上的所有广告单元。” - dvdmn
谷歌引用的页面上说:“如果我在一个页面上有多个广告单元,我是否必须为每个广告单元包含<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>? 不需要,只需加载一次adsbygoogle.js即可。” - JAT86

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接