{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
How not to show the AdSense ads while testing
Summary
You don't want to show ads from adsense while you are testing your site. This will inflate your adsense numbers and there is a chance of you accidentally clicking on one of the ads. Google does not allow you clicking on ads shown on your site. This article shows how to be safe and not show ads while testing.
 
Table of Contents

Additions to Web.Config file

Wrap your Google Ads inside a Panel

Set the Visibility of the Panel according to whether to show ads or not

 

Your site has just been approved to display the ads provided by Google’s AdSense. You are experimenting with ad layouts and where to put the ads on various pages and so on. In your Google AdSense account the ad impressions are piling up; and almost all those impressions are from your own testing. You are also worried that you might accidentally click one of the ads.

With Google seriously going after fradulent clicks, you want to eliminate the possibility of anything being misconstrued. This article shows how to switch on or off the Google AdSense ads by using keys from the web.config file. With this you don’t need to recompile -- just change the web.config file.

Not just the fear of Google, but this approach also gives you some flexibility in switching on or off the ads if circumstances warrant that.

This idea here is:

  • Wrap the AdSense code inside a Panel.
  • Show/Hide the Panel according to web.config keys
  • Set the Web.Config keys whether to show ads or not

Additions to Web.Config file

You need to take care of two situations:

  • Disable ads entirely -- this is useful when somebody (or lot of internal people) is going against a test server. As shown below set showAds key to False, no ads will be shown to anybody accessing this server.
  • Disable ads for a specific address -- say, you are testing the real production site from your laptop and you don’t want any ads displayed on your machine. In that case, set the IP Address of your machine to the key noAdsForAddress (as shown below)

<appSettings>
  <add key="showAds" value="True"/>
  <add key="noAdsForAddress" value="89.xx.x.123"/>
</appSettings>

You can find out your current IP Address from the following link (or clicking on Your Connection Info link on this site:

http://www.infinitezest.com/MyConnectionInfo.aspx

Generally, even the dynamic IP Addresses don’t change that frequently, so, you shouldn’t have to update the above key (noAdsForAddress) that often.

Wrap your Google Ads inside a Panel

From the AdSense site, you will be generating the script code to put on your site. Simply wrap it inside a panel. Panel makes it easy to hide or display something it contains.

<asp:Panel ID="pnlGoogleAd" runat="server" Visible="True" >

    <div>
        <script type="text/javascript"><!--
        google_ad_client = "pub-xxxx";
        google_ad_width = 160;
        google_ad_height = 600;
        google_ad_format = "160x600_as";
        google_ad_type = "text_image";
        google_ad_channel = "xxx";
        google_color_border = "336699";
        google_color_bg = "FFFFFF";
        google_color_link = "0000FF";
        google_color_text = "000000";
        google_color_url = "008000";

        //-->
        </script>
        <script type="text/javascript"
          src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
        </script>   
    </div>

</asp:Panel>  

Set the Visibility of the Panel according to whether to show ads or not

Set the visibility of the panel (pnlGoogleAd) according to a static method called ShowAds (this resides in a class AdManager -- you can put it anywhere you want). This one line can be used wherever a panel holding a Google ad is present.

pnlGoogleAd.Visible = AdManager.ShowAds(Request.UserHostAddress);

In the ShowAds methods, we need to check for three situations where we don’t want to display ads:

  • If it’s a request from localhost,
  • If we don’t want to show any ads at all (for any reason)
  • If we don’t want to show ads for a specific ip address

/// <summary>
/// Decides whether to show ads or not.
/// </summary>
/// <param name="currentAddress"></param>
/// <returns></returns>
public static bool ShowAds(string currentAddress)
{
    // No local address
    // See if ads are not be shown on any address
    // See if this is an IP Address where ads are not to be shown
    if ((currentAddress == "127.0.0.1") ||
        (ConfigurationManager.AppSettings["showAds"].ToString() == "False") ||
        (ConfigurationManager.AppSettings["noAdsForAddress"].ToString() == currentAddress))
        return false;

    // Can show Ads
    return true;
}

If you place ShowAds in a generic utilities type class, you can access it from any number of placess where Google Ads are being shown.

Bookmark and Share This

More Articles With Similar Tags
icon-fiddler-googlebot-http-request-results.jpg
RewritePath used to service search engine friendly urls has problems with search engines. While this works fine with regular browsers, the request fails with the user agents of most all search engines. This article looks at the workarounds (fix from Microsoft was not available by early 2007)
It's easy and convenient to obtain articles, blog posts, etc. via ID as query string. But the page generated with ID paramater has less of a chance of being indexed by search engine. Instead of ID, use a more search-engine-friendly name in the URL.
About  Contact  Privacy Policy  Site Map