How to add Google AdSense for Mobile to your Ruby on Rails site
In September, Google announced AdSense for Mobile, which allows the monetization of mobile sites through AdSense ads.
However, AdSense for Mobile currently only officially supports PHP, CGI, JSP, and ASP web pages, not for any deep technical reason but because Google doesn’t supply code snippets for other languages.
To address this shortcoming in the case of Rails, I’ve translated the code to create _adsense.rhtml.
To use AdSense for Mobile with your Rails site, do the following:
- Copy_adsense.rhtml into one of your view subdirectories in your Rails tree.
- Edit the value of client_id on line 8 of _adsense.rhtml to include your actual client ID. Your client ID can be copied from line 4 of the PHP script provided by Google.
- Insert this line to include the adsense partial in one of your views: <%= render :partial => "adsense" %>
- Sit back and watch the money pour in.
Note that by default Adsense for Mobile will return a HTTP 400 error if you attempt to display ads on a non-mobile browser. To bypass this check for testing purposes, you can pass in a user_agent query string parameter to pretend to be a particular mobile browser; e.g. http://example.com/controller/action?user_agent=Blackberry8800


