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)
<add key="showAds" value="True"/>
<add key="noAdsForAddress" value="89.xx.x.123"/>
You can find out your current IP Address from the following link (or clicking on Your Connection Info link on this site:
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" >
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";
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
/// Decides whether to show ads or not.
/// <param name="currentAddress"></param>
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))
// Can show Ads
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.