tags:

views:

1698

answers:

5

Do anyone know how to put Google adsense ads inside a GWT web application?

+1  A: 

According to this thread on AdSense:

Short version, you can't use Adsense via Ajax without breaking the programme policies/t&c's

Long version...

Ad code passed through an xmlhttp call is not rendered, it's just treated as text (hence, responseText). The only way to execute js code is to use "responseXML" coupled with the "exec()" command.

For instance...

If your xml contains something along the lines of:

This is the content from the external file javascript code goes here

You would assign a variable (called page_data for instance) using ajax_obj.responseXML, run the XML through a parser and run

exec(js variable or line from XML here);

Not really helpful from an Adsense standpoint, but that's how it's done.

It's also worth mentioning Why I dumped GWT:

Another problem were my adsense banners. Since I didn’t have a lot of content on the page, the banners were sometimes off topic. An even bigger problem was that the banners stayed the same when people searched for different keywords (since the ajax refresh didn’t trigger an adsense refresh). I solved this by doing the search with a page refresh instead of an ajax call. The ajax part of the site was limited to sorting, faceting, i18n and displaying tips.

cletus
thanks, i really appreciate your, but i still need any workaround to make this work!
Thiago Diniz
You realize by doing so you're violating the AdSense terms?
cletus
+1  A: 

You can put the javascript-code from Adsense in the single HTML page that GWT starts with. This way the advertising will not be displayed in the same area as GTW but above/below the GWT code. For advertising that could be ok.

This example places a baner above the application:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>www.javaoracleblog.com</title>
    <script type="text/javascript" language="javascript" src="com.javaoracleblog.aggregator.nocache.js"></script>
  </head>
  <body>
<script type="text/javascript"..
ADsense code here 
</script>
    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
  </body>
</html>

In order to indicate to Google WT that the site of Google adsense can be trusted you need to add a regex matching URL to the -whitelist command line argument.

Note that this will probably not solve the problems desribed in the above "Why I dumped GWT" article.

Edwin
A: 

Google's AdSense bot crawls your page to determine what ads to serve. Therefore, you should not put AdSense on pages with mostly dynamic content. It won't work well.

Maybe you should look into other ad programs?

+2  A: 

You might check out the interview I did with InfoQ. It includes a sample chapter from my book and it happens to be on SEO.

It's not trivial, but I think the solutions in the chapter let GWT work nicely in an environment where SEO is important. The basic solution is to implement something I call 'bootstrapping'. This means that your pages take the info that would normally come across in GWT-RPC requests and serialize them into the page. The GWT widget then loads this information without an RPC request. While your page serializes the info into JavaScript, it's easy to also write a <noscript> to the page that can be used for SEO.

Take a look at the PDF included here: InfoQ GWT it goes into all the detail. The whole sample project is here: google code with source on github.

jdwyah
+1  A: 
Alexander Orlov