36

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>
Marwelln
  • 28,492
  • 21
  • 93
  • 117

4 Answers4

102

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({});
});
Marwelln
  • 28,492
  • 21
  • 93
  • 117
  • 2
    I'm still only seeing one per page. – Abram Dec 05 '14 at 16:02
  • This worked for me when i also included the HTML snippet not just the js code! – BlackBurn027 May 22 '17 at 06:09
  • If I use the same "data-ad-slot" twice in a page, will the second ad displayed be counted as a separate impression? Or do I need to use a different data-ad-slot to gain revenue from the second displayed ad? – JAT86 Aug 25 '19 at 22:05
  • Each ad unit has a unique `data-ad-slot` ID right? Can I place to ads for the same ad unit (same `data-ad-slot`) on the same page? Thanks – cbdeveloper Feb 16 '20 at 16:15
16

Using jQuery...

$(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); });
Carl M. Gregory
  • 686
  • 6
  • 7
6

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>
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
  • If you place the – Danger May 13 '18 at 16:40
  • @Danger: why do you reckon it would be a problem? – Hassan Baig May 13 '18 at 19:15
  • Each ad unit has a unique `data-ad-slot` ID right? Can I place to ads for the same ad unit (same `data-ad-slot`) on the same page? Thanks – cbdeveloper Feb 16 '20 at 16:16
-5

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.)

galeksic
  • 2,176
  • 18
  • 25
  • 7
    see http://exisweb.net/using-google-adsense-async-tags-for-responsive-design. It's bad practice to **include** more than one time the adsbygoogle.js script. On time is enough. – Emmanuel Gleizer May 25 '15 at 15:17
  • 1
    @EmmanuelGleizer Just curious, then why doesn't Google's help documents say so? – Justin Skiles Aug 07 '15 at 11:58
  • 1
    @Emmanuel Gleizer - AdSense is not StackOverflow (for example - you can't "self-accept" your appeal if you get your AdSense account disabled), and it is not about programming, AdSense is business and that is **not** "bad practice" in AdSense. You are right about this: "_One time is enough._", that is correct. – galeksic Aug 07 '15 at 12:23
  • 1
    @Justin Skiles - Yes, AdSense documents say so, here: https://support.google.com/adsense/answer/3221666?hl=en and here: http://googledevelopers.blogspot.com/2013/07/an-async-script-for-adsense-tagging.html – galeksic Aug 07 '15 at 12:24
  • Quote from the given link: "To get the full benefit of the asynchronous code, we recommend that you switch all of the ad units on a given page at the same time." – dvdmn Apr 30 '17 at 03:03
  • The Google page cited says: "If I have multiple ad units on a page, do I have to include `` for each ad unit? No, this is not necessary, adsbygoogle.js can be loaded once." – JAT86 Mar 20 '18 at 12:32