Extraordinary Thoughts
23Jan/104

Adsense and AJAX: A Solution

Tired of fight with Adsense when using AJAX?  That's exactly how I've been the last couple days.  For such a widely spread service, it's terrible how bad flexibility for Web applications can be!

Anyways, most reputable programmers wont be relying much on iFrame, so here's an easy solution - just run this function every time you want to update the ads on your page.

I must warn you, DO NOT abuse this, Google can and will ban your account if impressions are being forced.  I can't vouch that Google will even approve of this method, but from reading the ToS it should be acceptable giver the [end] user sent a request to change page content.

var redirects = 0;
function updateAdsense() {
	redirects++;
	var ads = document.getElementsByTagName('iframe');
	var src;
	if(ads && ads.length){
		for (var i = 0; i < ads.length; i++){
			src = ads[i].src.split('&');
			if(redirects > 1)
				src.pop();
			src = src.join('&');
			src = src + '&'+new Date().getTime();
			ads[i].contentWindow.location.replace(src);
		}
	}
}

You can see I use "location.replace", this solves issues with Adsense updates effecting browser navigation. In basic, it makes the back button work correctly.

As you can see this code is rather lazy, I will be providing an update soon. :)

Update

var redirects = 0;
function updateAdsense() {
	redirects++;
	var ads = document.getElementsByTagName('iframe');
	var src;
	if(ads && ads.length){
		for (var i = 0; i < ads.length && ads[i].id.substr(0,16)=='google_ads_frame'; i++){
			src = ads[i].src;
			if(redirects > 1) {
				src = src.split('&');
				src.pop();
				src = src.join('&');
			}
			src = src + '&'+new Date().getTime();
			ads[i].contentWindow.location.replace(src);
		}
	}
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Twitter
  • email
  • LinkedIn
  • Live
  • MySpace
  • Technorati
  • Tumblr

About Shawn Khameneh

I'm a "Web Guy" with a sense of reality. I enjoy trying new things and meeting new people! You could call me an entrepreneur, I enjoy both business and the effort behind it.
Comments (4) Trackbacks (0)
  1. I always appreciated Ajax and never used in my site because I was using Adsense as my primary source of revenue. I could not sacrifice the possibility of earning a few dollars over improving user experience. I believe and I’m waiting for some genius people like you will find some solution and make our life easier.

  2. This is great – although I was a little leary of it when I saw it was being used to update the ads. But you have a valid point – the user is clicking through your site in order to load new content, it just isn’t registering with Adsense and thereby not getting new content to the ads. I think that this is a legitimate use.

    Has anyone asked Google?
    Don Gilbert´s last blog ..Why Your Small Business Can’t Ignore Social Media Any Longer My ComLuv Profile

    • From what I have heard, Google does advise against it, but I have yet to hear them actually say no.
      This method does seem to comply with the ToS as it does not modify their code nor cause unintended “clicks”.

  3. I’m using something similar to this code. I’ve read through the AdSense TOS, and from what I can tell it should be kosher, as you say. It’s certainly in the spirit of ad serving, in that you issue a reload – essentially in place of the browser doing it – in response to a user request for new page content. We refresh virtually everything on the page, so all’s well there.

    The big limitation I’ve found is that when using banner ads, a portion of those are Flash – which doesn’t use an iframe. So, there’s nothing to target for the refresh, and it’s a complete roadblock from that point until a hard page reload occurs.


Leave a comment


CommentLuv Enabled

No trackbacks yet.

Categories

Comment Leaderboard

Links

Meta

Alexa Rank


Blog Directory - OnToplist.com
Clicky Web Analytics

This Blog is DoFollow!

Tags

1080p ajax album art better code cron dd-wrt dhcp dns dnsmasq dslr everun faster firefox gh1 good google graphics html5 laser logitech maximize mouse netbook note optimize panasonic php ping plugin plugins processor review router screen scripts search seo snapshots tcp/ip twitter upgrade video wordpress