Refresh Google Ads with Javascript for AJAX Pages

Have you ever tried posting Google Ads on an AJAX website? You spend
all your time making your application avoid refreshing but dramatically
reduce your Google Ad impressions because your page never needs to refresh.

Here is a technique I came up with to refresh the ads via JavaScript.
Thanks to the awesome power of FireBug I was able to look at the global
variables defined by the Google Ad includes.

The secret is that Google loads the ads in an iframe. So, you find
the iframe then update the src attribute which will cause it to refresh.
The URL for your specific ad channel are stored in a global variable named “google_ad_url”.
However, if you just set the iframe.src = google_ad_url, your browser will not
pull a new set of ads because of caching, so you need to append a dummy
variable onto the url to force a refresh.

refresh_ads = function() {
  var a = $$('iframe[name="google_ads_frame"]');
  if (a.first() && google_ad_url) {
    a.first().src = google_ad_url + '&' + new Date().getTime();

The above function is using the mighty $$ method provided by
prototype.js. The return value “a” is an array, so I grab the first
element (.first() is added by the prototype library). I append
a new time string on the url for each request.

Just call refresh_ads() whenever you need to refresh the ads. I’m actually
calling it via the page object in a rjs file.

I’m not sure if this fits in the strict interpretation of Google Ads TOS, but
the relevant section states:

You shall not, and shall not authorize or encourage any third party to: (i) directly or indirectly generate queries, Referral Events, or impressions of or clicks on any Ad, Link, Search Result, or Referral Button (including without limitation by clicking on “play” for any video Ad) through any automated, deceptive, fraudulent or other invalid means, including but not limited to through repeated manual clicks,…

If I didn’t use this method, I could just force a new page request on each update instead of fancy AJAX updating. So, I think it fits with in the spirit of Google Ad Impressions based on each page the user accesses. I’ll let you know if I hear any different.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: