Some publishers are using plugins or services to create an app for smartphones from their websites. It is good practice to disable ads from ad networks like Google AdSense in an app because you are not allowed to use them there. This tutorial shows you how to do that.
There are two methods to disable ads from your webview app. The solution you need to choose depends on the technology used by the software that creates the app. All cases I have seen so far are covered by the two methods described below.
Table of Contents
How to test which solution might work
Before we set up the solution to hide ads from your mobile app, we need to find out how to identify it on our website.
To do this, you can set up an ad with the purpose to show these information.
- go to Advanced Ads > Ads and create a new ad
- add a title like “User Agent and Referrer URL Test”
- choose the Plain Text and Code ad type and add the following code to the input field:
<div style="width: 300px; height: 250px; background: #428bca; color: #fff; text-align: center; font-size: 1.8em; overflow: scroll;">
<p>User Agent: <?php echo isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : 'user agent not set'; ?></p>
<p>Referrer URL: <?php echo isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'referrer not set'; ?></p>
</div>
Code language: HTML, XML (xml)
- enable the Allow PHP option
- publish the ad
- either choose a placement to embed the ad on every page or use the shortcode to place it only on one
Now, visit the page where you placed the ad code in your web app. You might need to refresh any caches to see the ad.
The output could look like this:
You either find a trace of the mobile app in the User Agent or Referrer URL. In this case, we see one in the Referrer URL and can choose the appropriate solution below.
Disable ads using the User Agent condition
Use this method to remove ads from the web app if it identifies itself through the user agent string. This solution uses the User Agent visitor condition included in Advanced Ads Pro.
- go to the edit page of the ad
- navigate to Visitor Conditions and select the User Agent condition
- choose “does not contain” and enter a unique part of the user agent string. It does not have to be the full string
- save the changes
The ad is now visible on your website and not in the webview app anymore.
Disable ads using the Referrer URL condition
The example from our test ad above shows how to identify the view of the mobile app when using the WPMobile.APP plugin to create it.
To remove AdSense or any other ads from this app version, we are using the Referrer URL condition in Advanced Ads Pro.
- go to the edit page of the ad
- navigate to Visitor Conditions and select the Referrer URL condition
- choose “does not contain” and enter the parameter that is used by the mobile app to identify itself
- save the changes
The ad is now only visible in your web version and not in the mobile app anymore.
Disable ads using the Cookie condition
The author of the WPMobile.APP plugin made us aware that one can also use the HTTP_X_WPAPPNINJA
cookie as a basis to disable ads in the mobile app view.
With Advanced Ads Pro being installed, follow these steps.
- go to the edit page of the ad
- navigate to Visitor Conditions and select the Cookie condition
- choose “is not” and leave the field for the value empty
- save the changes
This condition reads as: show this ad only if the HTTP_X_WPAPPNINJA
cookie is not present.
Other methods
The above methods are the ones we have worked with so far. Please let us know if you run into different cases.
Developers can also output the whole content of the $_SERVER
global array to check if there are other identifies. I am happy to update this tutorial accordingly.