Instant Search

Archives

Article

AdSense and AJAX: Not a happy combination

After releasing my AjaxBerlee WordPress theme George and Milan’Che reported problems with Google’s AdSense. This left me with a problem. Because new page requests in my theme are parsed over the old ones.

AdSense works as follows. The Mediapartners-Google spider indexes the site, when visitor now requests one of your pages it will load the external JavaScript from the Google domain. In that JavaScript the text advertise is displayed.

So far so good. An AJAXified page will initially display the AdSense advertise very well (because the pages are backwards compatible). The visitor clicks on a link and now only a part of the page get reloaded, but not the AdSense advertise. Because it’s possible to load an JavaScript into JavaScript, but its not possible to reload JavaScript into JavaScript (for scripts that aren’t build for it).

I suspected that Google had some kind of a refresh function build in where I could specify which URL the currently displayed AJAXified site was. So when you click on an AJAXified link, the Ajax JavaScript calls a Google refresh function. (this is actually how the Google Analytics support works in the AJAX JavaScript)

I was surprised to find out that there was not such a function in the AdSense JavaScript. So I tried to find out how AJAXified sites like Google Maps and GMail works with AdSense. Well, actually, they don’t!

GMail has some uses some AJAX function to display realtime new incoming messages, but the actually displaying of mail goes thru frames and plain old loading of pages! And Google Maps actually doesn’t use AdSense at all… Well, they do, but its parsed into the page and doesn’t use external JavaScript at all, as described above.

I found one workaround by Kevin Cho who used an iframe. In that iframe the AdSense got loaded with the keywords of that page specially dedicated for AdSense for that site. You need to type these manually, which is a hell of a job if you have a lot of pages. And it’s probably against the Program Policies which are very clear about cloaking.

I decided not to implement that workaround because of the Program Policies violation. But I will publish a modified ajax.js that is compatible with AdSense but does not have the enhanced page loading.

Comments (15 comments)

Hello, Nico. Thanks for your research into updating Google Adsense ads in AJAX apps. Have you noted any developments in this area, since you wrote this article? Obviously, the best solution would be if Google were to extend their API to allow for refreshes of the Adsense content.

Basically, AdSense writes out an IFrame behind the scenes in itâ€™s ad script. We can grab the a div that contains the script and look for an IFrame within it. Then you can dynamically switch the src of that IFrame.

I have successfully done this technique on my site using IFRAME. However in my case stuff that was in IFRAME had to be refreshed anyway, so I didnt have to hide any keywords.

However there is one solution, without breaking the TOS (I think)… when you change the source of the file just make the iframe src filename related to the topic you want ads to be displayed for. It worked for me. check http://www.hungrysiam.com

How about using the iFrame idea, using Google’s standard script, but make the page within the iframe a server side page that accepts a URLString of the page id or similar. Then make that page grab the keywords, description and any other useful content such as the tags?

Would only need to be a simple page as nothing other than Google would accept it. You could even go one step further and use the real natural url. For example:

real URL: test.com/page/content-title
Ad URL: test.com/ad/page/content-title
OR
Ad URL: test.com/page/content-title?ad=true (the rendering of the site would need to grab the last variable and know it’s acutally an ad page).