Reputation: 29433
How do you have multiple adsense units on one website? The only code Google gives are per unit.
<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>
What if I want to use multiple adsense units on one website? I only use <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
and (adsbygoogle = window.adsbygoogle || []).push({});
once, and then place the <ins ...></ins>
code where I want it to be.
The problem is that only the first adsense unit is parsed and shown. What do you need to do to be able to display more than one adsense unit?
This is how I use it (only first ins
is shown):
<!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>
Upvotes: 37
Views: 31141
Reputation: 15844
Call <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
just once, at the bottom of the page (right before </body>
).
Next, place your ad snippets separately like so:
<!-- 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>
Upvotes: 6
Reputation: 2176
If you want to use multiple AdSense units on one page, then you need to create and paste multiple AdSense snippets:
<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>
Only limited number of code modifications is allowed in AdSense. https://support.google.com/adsense/answer/1354736?hl=en
I could probably answer why "only the first adsense unit is parsed and shown" and I could try to show you how to modify your example to show all three ads, but in my opinion that is irrelevant (in this case), because it is not permitted in AdSense. (And probably completely unnecessary. You can simply paste three ad code snippets, or - same snippet three times.)
Upvotes: -5
Reputation: 29433
To have more then one adsense unit on one page you must add more rows of (adsbygoogle = window.adsbygoogle || []).push({});
.
So if you have 3 ad units, you want to use it 3 times.
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
If you want to do it dynamically, use this:
[].forEach.call(document.querySelectorAll('.adsbygoogle'), function(){
(adsbygoogle = window.adsbygoogle || []).push({});
});
Upvotes: 107
Reputation: 696
Using jQuery...
$(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); });
Upvotes: 17