Is it possible to use asynchronous google adsense, i.e. non-blocking?

7,864

Solution 1

this does it:

<script type="text/javascript"><!--
// dynamically Load Ads out-of-band
setTimeout((function ()
{
    // placeholder for ads
        var eleAds = document.createElement("ads");  
        // dynamic script element
        var eleScript = document.createElement("script");  
        // remember the implementation of document.write function
        w = document.write;
        // override and replace with our version
        document.write = (function(params)
        {
        // replace our placeholder with real ads
        eleAds.innerHTML = params;
        // put the old implementation back in place
        // Aristos, add this check because called more than ones
        //  and ends, with this symbol.
        if(params.indexOf("</ins>") != -1)
            document.write=w;
        });
        // setup the ads script element
        eleScript.setAttribute("type", "text/javascript");
        eleScript.setAttribute("src", "http://pagead2.googlesyndication.com/pagead/show_ads.js");
        // add the two elements, causing the ads script to run
        document.body.appendChild(eleAds);              
        document.body.appendChild(eleScript);           
}), 1);
                //-->
        </script> 

see this blog item "Google Ads Async (asynchronous)" basically I've found a way to get the ad code to run truly async..

Solution 2

Yes, there is a way - using iframes. We do asynchronous loading of ad content on one of our customer's sites.

You first have to contact Google for support of their ads in iframes (which was not widely available back when we built that site). You can then use JavaScript to trigger the loading of the AdSense iframe content, which is what we do.

The solution is really straight forward once you have Google's iframe support.

Edit: Looks like - from the posts referenced by Virtuosi Media - this practice might not be welcome by Google...

Edit 2: This is the code we use in the iframe. Try it out to see if it works, maybe even without specifically asking Google for support. Removed type="text/javascript" language="JavaScript" for better legibility:

<head runat="server">
    <script src="http://partner.googleadservices.com/gampad/google_service.js">
    </script>

    <script>
        GS_googleAddAdSenseService("ca-pub-YOURKEY");
        GS_googleEnableAllServices();
    </script>

    <!-- For this method, it is not necessary to define the slots in advance 
         and we do not use the FetchAds function -->
    <script>
        GA_googleUseIframeRendering(true);    //<%-- This indicates iframe rendering for all slots --%>
    </script>
</head>
<body>
    <form id="formAd" runat="server">
        <div>
            <script>
                GA_googleFillSlotWithSize("ca-pub-YOURKEY", "AdSlotId",
                                          iframeWidth, iframeHeight);
            </script>
        </div>
    </form>
</body>

We fill all of our slots through adslot ids, so we are not seeing any PSAs.

Solution 3

They don't currently offer the option, as far as I can see. Your best option, if it was allowed by the Google TOS, would have been to use a placeholder element and replace it dynamically with JavaScript after the page has loaded. However, it appears as if it may be against the TOS. See this thread for more details.

Solution 4

You can use a placeholder, call the Adsense code and then switch the placeholder with the ad. I'm using something like this on my site:

if ( ad1 = document.getElementById('ad-top') )
{
    ad2 = document.getElementById('banner_tmp468');
    ad1.appendChild(ad2);
    ad2.style.display = 'block';
}

Google even recommends doing this when they say that you should have your most important ad position first in your HTML code. If you want to accomplish that you'll have to use JS or CSS to move less important ad units to the (visual) top of your page.

But as far as I know AdSense already renders ads asynchronously. I've only very rarely seen it block pageloading. Not sure about this though.

Share:
7,864

Related videos on Youtube

Tom
Author by

Tom

Updated on September 17, 2022

Comments

  • Tom
    Tom over 1 year

    I know that there is a Asynchronous method of using Google Analytics but is there something similar for adsense? I can't find one from my current searches - I was wondering whether there is a common trick I'm missing?

  • Virtuosi Media
    Virtuosi Media over 13 years
    Can you provide more detail on contacting Google about this? I don't see anything about it in my AdSense account.
  • Virtuosi Media
    Virtuosi Media over 13 years
    Also, I'd be curious to know if the ad units still deliver relevant ads or just PSA's and how you worked around that problem.
  • Oliver
    Oliver over 13 years
    Actually, we fill all of our slots through adslot ids, so we are not seeing any PSAs.
  • Kristian Damian
    Kristian Damian over 12 years
    +1 for the smart solution provided in the link. It's very interesting the way it backups the standard document.write function and restores it later. The only downside is that I'm wondering if Google Adsense TOS allow to do this kind of stuff?
  • Timo Huovinen
    Timo Huovinen over 12 years
    when using this method, I get GA_googleFillSlotWithSize is not defined error
  • Oliver
    Oliver over 12 years
    @YuriKolovsky: looks like Google's script is not loading properly. See my Edit 2: you might need to apply for this method with Google.
  • Timo Huovinen
    Timo Huovinen over 12 years
    @Oliver Yeah, it probably didn't work because I didn't apply for the iframe method.
  • Aristos
    Aristos over 12 years
    +1 for the idea, I take it and make it work a little better and post it here. I fail to make this GS_googleAddAdSenseService work with the other idea.