Mobile app version of vmapp.org
Login or Join
Samaraweera270

: How to prevent Google Adsense ads displaying in development mode on my OpenShift website? I'm hosting my website on OpenShift and it displays some ads. When I'm changing some content on it,

@Samaraweera270

Posted in: #GoogleAdsense

I'm hosting my website on OpenShift and it displays some ads.

When I'm changing some content on it, I have to manually comment out the ad code. Is there some way to automate this process or prevent Google from serving live ads when I'm in development mode?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Samaraweera270

1 Comments

Sorted by latest first Latest Oldest Best

 

@Turnbaugh106

There’s several ways that you can approach this but it’s in my honest opinion that the best method would be to block the adsense client side rather than server-side. Please note that Adsense never begins at the server, its the client that triggers the adverts, not your server, so with this said here are a few methods that come to mind:

Server-side htaccess method

Using the htaccess <Files> method you can block the adsense JavaScript being called whenever you or your employees visit the site, not only will this help block the adverts on the development server you can also port it across to the live server which protects you against accidental clicks and skewed stats.

In order to use this you need load the adsense from a file rather than inline code. So you would need to create a file with any filename you like but in this example we use adsense.js, the file contents will look something like this:


SOURCE

google_ad_client = "pub-xxxxxxxxxxxxxxxx";
google_ad_host = "pub-xxxxxxxxxxxxxxxx";
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
function ApplyAdSense() {
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = "http://pagead2.googlesyndication.com/pagead/show_ads.js";
document.getElementsByTagName("head")[0].appendChild(oScript);
}



And in the footer of your site you need to call the file using the following code:

<script type="text/javascript" src="adSense.js"></script>
<script type="text/javascript">
ApplyAdSense();
</script>


Then to simply block adsense.js for users matching IP addresses use something like this in your htaccess file:

<Files adsense.js>
Order allow, deny
Allow from all
Deny from x.x.x.x
</Files>


Client-side host file method

There are many browser plugins and software that will block the adsense for you, simply search Google for an ad-blocker, most will simply block the URL that the advert is served from preventing from miss-clicks and not affecting your page view stats. However, if you’re like me and prefer not to use bulk software or plugins for a simple taste then you can actually do this simply by editing your host file.

You can block the adsense JavaScript and the adverts being displayed using the host file by telling your operating system example.com is localhost. Both Windows & Mac operating systems have a host file, the location of these files are as follows:


Windows 95, 98, 98SE, ME: C:WINDOWShosts
Windows NT3 and NT4: C:WINNTsystem32driversetchosts
Windows 2K, XP, 2003, Vista, 7, 8, 10: C:WINDOWSsystem32driversetchosts
Mac OS: /private/etc/hosts


In Windows you will need to edit the hosts file in administrator mode and in Mac OS you will need significant permissions to edit your Mac Hosts file. Simply add the following lines to your hosts file and your never see an adsense advert again, obviously this method will block all adsense adverts everywhere and not just your site.

127.0.0.1 pagead.googlesyndication.com
127.0.0.1 pagead2.googlesyndication.com
127.0.0.1 ad.doubleclick.net


Also it's worth mentioning that you can also block adsense using your router, some routers will support this feature out of the box while some do not, but you can install OpenWRT or DD-WRT to add this feature if required. If opting to use DD-WRT then I recommend this video tutorial I made donkeys ago on how to block adverts using DD-WRT.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme