Getting AdSense working with JQuery Mobile and PHP

Post to Twitter

I’ve been working on a fun little project using JQuery Mobile and PHP in which I wanted to integrate Google’s Adsense as well as Google’s Mobile AdSense (not AdMob) depending on what device was visiting the site. Today I’m going to go over a few tips that I learned along the way.

One thing I learned right away was that AdSense for Mobile ads don’t show up on non-mobile devices and currently it only shows up on iPhones and Android phones as best as I can tell. The iPad is not currently treated as a “mobile” device by AdSense Mobile. So I needed a way to display mobile ads to iPhones and Android phones and a way to display regular AdSense ads to all other devices. I found this site to detect mobile devices. The actual PHP code can be found here: mdetect.php

With that code included onto my PHP pages I was able to quickly determine if an incoming request was from and iPhone or an Android phone like so:

$uagentObj = new uagent_info();

$iPhone = $uagentObj->DetectIphone();
$androidPhone = $uagentObj->DetectAndroid();

if($iPhone || $androidPhone)
   // Call AdSense for Mobile code
   // Call regular AdSense code

Another thing to note is with JQuery Mobile its easy to put all your content into one webpage – that way all the transition effects like sliding, etc. work flawlessly however this also presents a problem in the fact you can currently only have a maximum of 3 ads per page so even though you might only be displaying a portion of your content which may or may not have an ad visible you need to ensure you don’t go over the 3 ad limit even if they are currently hidden (unless of course you code it so the JQuery Mobile page manually loads the ad when it’s visible). This does pose a problem with some JQuery Mobile projects so be aware of this. You can avoid this issue by simply loading a page and having rel=”external” in your href so the default Jquery Mobile AJAX page loading isn’t used but you will lose the transition effects from page-to-page.

Post to Twitter

This entry was posted in JavaScript, Mobile, PHP. Bookmark the permalink.

One Response to Getting AdSense working with JQuery Mobile and PHP

  1. Eric Priou says:

    You’d consider also WURFL to detect devices.
    They use a cache system to speed the things up.

Comments are closed.