Removing blocking AdSense

Removing blocking AdSense

It’s the last part of our Stage 3 optimization research. And we are going to perform some actions to remove blocking behavior of AdSense blocks (and any other advertisement as well).

What is blocking JavaScript

Here is a very good article from Steve Souders about how JavaScript blocks web page rendering and how this can be prevented. For our website such blocking behavior can be illustrated the following way:

Firebug Net Panel before

As you can see here AdSense (googleads.g.doubleclick.net) prevents our pictures to be downloaded (and actually stops page rendering).

How to prevent it

We can just move all our JavaScript libraries to the very bottom of the page (all — counters, jQuery, MooTools, etc, etc). And browser will load them only after the other content of the page will be rendered. Generally it’s good only if you have some libraries which don’t perform document.write code. And what is case of Google AdSense?

Almost every dynamic advertisement network (this can be rich media ones, and context ads, and so on) uses document.write to inject a part of DOM tree (it can be also an iframe) directly to advertisement placeholder. In case of iframes this can be tolerably – because they download all resources in parallel. But browser every time stops to get the initial JavaScript file, and page ‘freezes’ for a while. What is not good.

And how can we handle such dynamic ads?

Dynamic ads — dynamic unblocking

First of all we definitely can load ads in the bottom of the page. But we have a trouble here — we need to move them to initial placeholders. So we can use a piece of JavaScript code to perform this (actually we are just removing blocking behavior, not building a complex system to load all dynamic assets on the web page in a special order).

For example for AdSense we have the following JavaScript code:

<script type="text/javascript"><!--
google_ad_client = "pub-xxxxx";
google_ad_slot = "xxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

We need to use here a placeholder with the same height (to prevent content jumping when we inject actual advertisement back). As far as we have here height of 60 pixels we can use

<div id="placeholder" style="height:60px"></div>

And in the end of our HTML document (just in footer) we should write the following:

<div id="sourcecode"><script type="text/javascript">... google code here...
</script></div>

<script type="text/javascript">(function() {
var a=document.getElementById("sourcecode"),
b=document.getElementById("placeholder").parentNode;
b.innerHTML=b.innerHTML.replace(/
<\/div>/i,a.innerHTML); a.parentNode.removeChild(a) }())</script>

The first part of this code just consists of AdSense code (which is listed above) enveloped by a container. The second part is a dynamic replacement our placeholder (with a given height) with our advertisement. If you have several different ads — you just need to use different pairs of sourcecode/placeholder for them. That’s all.

Automation and actual results

There is the only WordPress plugin which implements the above logic — it’s WEBO Site SpeedUp. It has about 40 default patterns for known ads and counters.

Actual result from this magic is non-blocking JavaScript. The figure below shows our website load waterfall. As you can see some advertisements are loaded even after onDOMready event (blue line here) is fired.

Firebug Net Panel after

Conclusion

Depending on ads number and their placement you can significantly improve your website usability and prevent content rendering blocking with AdSense (and any other advertisement) delayed loading.

VN:F [1.8.0_1031]
Rating: 0.0/10 (0 votes cast)

Related Posts

7 RESPONSES TO Removing blocking AdSense

  1. stoimen March 9, 2010 at 10:07 am

    Is that technique better than putting the Ads snippet in an iframe?

    UN:F [1.8.0_1031]
    Rating: 0.0/5 (0 votes cast)
  2. WEBO Software March 10, 2010 at 9:44 am

    @stoimen: iframes are generally better than blocking JavaScript but AdSense is already enveloped into iframes. Iframes’ resources are mixed with assets from web pag, so content is being loaded slowly. So iframes must be alos delayed as well (and the technique here is the same).

    UA:F [1.8.0_1031]
    Rating: 0.0/5 (0 votes cast)
  3. Luti March 29, 2010 at 4:46 pm

    Your script is generate “false” traffic. In may case the real page downloads are 60% of the adsense showings report.

    UN:F [1.8.0_1031]
    Rating: 0.0/5 (0 votes cast)
  4. WEBO Software March 29, 2010 at 8:51 pm

    @Luti: do you have any reasons for this suggestion? Mentioned JavaScript piece of code does nothing but move AdSense from one place of the page to another. Without any additional actions

    UA:F [1.8.0_1031]
    Rating: 0.0/5 (0 votes cast)
    • Luti May 20, 2010 at 2:38 pm

      Yes. I think it is make bad CTR = Some case ’smart pricing’.

      UN:F [1.8.0_1031]
      Rating: 0.0/5 (0 votes cast)
  5. Luti May 20, 2010 at 5:28 pm

    Ok, the solution is here:
    http://www.webmasterworld.com/javascript/3890334.htm

    UN:F [1.8.0_1031]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

MySQL queries: 89 | 0.621s
Memory used: 15.23M
Memory limit: 32M