Mobile Device-sensitive AdSense Ads
There are several compelling reasons to serve different ads to mobile devices than are served to desktop users.
-
Google has AdSense ads specifically for mobile devices.
-
Ads too large for the device's screen can distract users enough to send them elsewhere for a more pleasant experience.
-
Some ads are inappropriate for mobile users (such as software for desktops) and vice versa.
Publish the AdSense ad that's correct for the device it's displayed in.
(The article's instructions are for publishing AdSense ads depending on device. To publish other specific content for certain types of devices, translate AdSense ad references to the content you want to publish.)
Overview: Implementing Device-sensitive AdSense Ads
There are three steps to implementing device-sensitive functionality. PHP web pages are required.
-
Download Mobile_Detect (open-source under MIT License).
-
Five lines of code are required on every page for device detection.
-
Put the AdSense source code between certain PHP tags.
Each step is described in detail.
Implementing Device-sensitive Functionality
The three steps for implementing device-sensitive AdSense ads download/
1. Download Mobile_Detect
PHP Mobile_Detect is a lightweight PHP class for detecting mobile phones and tablets, using the User-Agent string and HTTP headers for detection.
Download Mobile_Detect from http://mobiledetect.net (In case you need to download it in the future and above URL is changed, the class is also available at the public depository GitHub and linked from Google Code — URLs in the downloaded file from mobiledetect.net)
I can't take credit for this code. It was a fortunate happenstance when I found it. Mobile_Detect has been in use on another of our websites for some months now with no issues that I'm aware of.
No customizations are required. Just upload Mobile_Detect to a public directory on your server.
The following instructions assume you upload Mobile_Detect into the /php/ subdirectory. If it's located elsewhere, modify the instructions accordingly.
2. The Five Lines Of Code for Every Page
These five lines of code are required on every page for device detection:
<?php
require_once($_SERVER["DOCUMENT_ROOT"]."/php/Mobile_Detect.php");
$detectDeviceType = new Mobile_Detect;
$deviceType = ( $detectDeviceType->isMobile() ? ( $detectDeviceType->isTablet() ? "tablet" : "phone" ) : "computer" );
?>
If Mobile_Detect is located on your server somewhere other than the /php/ subdirectory, modify the blue-colored "/php/Mobile_Detect.php" code for the correct location.
The five lines need to be on the page somewhere above the point where the AdSense ads will be published. Putting it at the very top of the page is OK.
3. The AdSense Code
Here, you'll learn how to put the AdSense source code between certain PHP tags so certain AdSense ads will be shown when the page is displayed in a desktop browser and a different ad when it's displayed in a mobile device. Alternatively, a distinction may be made between mobile phone and tablet.
You'll need the code for both AdSense ads. One for desktop/
Here's the code:
<?php if( $deviceType == "computer" ): ?> [AD FOR DESKTOP AND NOTEBOOK COMPUTERS] <?php else: ?> [AD FOR MOBILE PHONE AND TABLET DEVICES] <?php endif; ?>
Paste the appropriate AdSense ad to replace the placeholders. Then insert the result into your web page where AdSense ads are to be published.
It should work a treat right out of the box, so to speak.
To differentiate between mobile phones and tables and deliver a different ad depending on whether the device is a computer, a mobile phone, or a tablet, you'll need three AdSense ads, one for each type of device.
Here's the code:
<?php if( $deviceType == "computer" ): ?> [AD FOR DESKTOP AND NOTEBOOK COMPUTERS] <?php elseif( $deviceType == "phone" ): ?> [AD FOR MOBILE PHONES] <?php else: ?> [AD FOR TABLETS] <?php endif; ?>
As before, paste the appropriate AdSense ad to replace the placeholders. Then paste the result into your web page.
Testing
Test the page on each type of device to ensure the correct AdSense code is published.
You now have the information needed to publish AdSense ads that are correct for the device they're displayed in.
(This article first appeared in Possibilities ezine.)
Will Bontrager