Adsense with AJAX

后端 未结 3 1690
我寻月下人不归
我寻月下人不归 2020-12-14 21:24

I read this, this and this, but I think my situation is different. I don\'t need to refresh Ads everytime I make an AJAX call.

I have a page call it \"mypage.php\".

相关标签:
3条回答
  • 2020-12-14 21:55

    The default google adsense code is something like this:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <!-- banner-name -->
    <ins class="adsbygoogle"
         style="display:inline-block;width:728px;height:90px"
         data-ad-client="ca-pub-12345678901234950"
         data-ad-slot="987654321"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

    Spit the code up into 3 parts to make it work on ajax loaded content.

    Include the google script somewhere in your page (in your <head> for example) just once.

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    

    Place the google code in your (ajax) content wherever you want the banner(s)

    <ins class="adsbygoogle"
         style="display:inline-block;width:728px;height:90px"
         data-ad-client="ca-pub-12345678901234950"
         data-ad-slot="987654321"></ins>
    

    Trigger this function after your content has changed via ajax. (don't forget to trigger this on page load too, to display ads when pages isn't loaded via ajax.)

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

    Ps. I'm not sure if google will allow this, since your modifying/changing up the code a little bit. But i'm currently using it this way.

    0 讨论(0)
  • 2020-12-14 21:57

    Unfortunatelly the page https://developers.google.com/adsense-for-ajax/ says Google no longer accepting new applications for AdSense for AJAX

    0 讨论(0)
  • 2020-12-14 22:03

    Did some more research .. there is no easy solution to your problem.

    If your site uses AJAX for a majority of the content then you can look at implementing the Google Ajax-Crawling (aka Hash-Bang) specs. This will ensure that that the Google bot and Adsense bot crawl your AJAX content. This will help with both relevant ads and search results. https://developers.google.com/webmasters/ajax-crawling/docs/specification

    Or you have to wait till the Adsense for Ajax program start again. https://developers.google.com/adsense-for-ajax/

    Update: Changed answer after more research.

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