Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Mobile Device-sensitive AdSense Ads

There are several compelling reasons to serve different ads to mobile devices than are served to desktop users.

  1. Google has AdSense ads specifically for mobile devices.

  2. Ads too large for the device's screen can distract users enough to send them elsewhere for a more pleasant experience.

  3. 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.

  1. Download Mobile_Detect (open-source under MIT License).

  2. Five lines of code are required on every page for device detection.

  3. 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/upload/copy-and-paste easy.

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/notebook computer users and another for mobile users.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC