How do you use multiple adsense units on one page?

后端 未结 4 1609
野性不改
野性不改 2021-01-31 17:17

How do you have multiple adsense units on one website? The only code Google gives are per unit.



        
相关标签:
4条回答
  • 2021-01-31 17:23

    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({});
    });
    
    0 讨论(0)
  • 2021-01-31 17:28

    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>
    
    0 讨论(0)
  • 2021-01-31 17:29

    Using jQuery...

    $(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); });
    
    0 讨论(0)
  • 2021-01-31 17:33

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

    0 讨论(0)
提交回复
热议问题