I‘ve been using Adsense for over three years to monetize my sites but even though I’ve followed the Adsense TOS and never clicked on my own ads, I was actually banned from Adsense for supposed invalid clicks last November. It turns out I was the victim of a click-bombing by some disgruntled ex-Adsense publisher who was stalking the Adsense support forums looking for innocent victims to get their accounts banned by clicking incessantly on their ads.
I did manage to have my Adsense account re-instated a few days later - minus two months earnings - so since then I have been looking at various solutions to prevent any click-bombings of my account in the future. I’ve tried several commercial solutions that were bloated with additional options that were unnecessary for my simple applications. Finally, I decided to begin coding my own solution based on the KISS (Keep It Simple Stupid) model that has only one function : disable Adsense ads after they have been clicked on “x” amount of times.
Over the next few days I will be publishing the solution, code named “ClickDodger”, to aid other publishers avoid the pitfall of click-bombing or invalid clicks. I would sincerely appreciate any input on this project and will work with anybody interested in utilizing this code to add additional functionality as the need arises.
Starting With The Simplest Scenario
In order for to accurately record the number of clicks a website visitor has made, it would be logical to use a simple html tracking cookie to record events. Granted, a hacker or click-bomber could easily delete the cookie each time they’ve clicked enough times to set off the tripwire but we’ll keep this simple for the moment.
I’ve opted to use Javascript because the modern DOM model supports event handlers as well as the ability to generate content on the fly. Javascript is also a viable option because it can be used in any scripting language (PHP, ASP, ColdFusion, Ruby) in addition to plain html.
Displaying the Appropriate Ads
Working backwards, the code to display either an Adsense ad or an alternative ad network based on the tripwire settings would look like this :
<div id="banners" onclick="trackclick();">
<script language="JavaScript">
if ( clickCount > 2 ){
document.write('
<a href="http://www.pjtra.com/t/RD1FRklFPURASUk9RURISQ">
<img src="http://www.pjtra.com/b/RD1FRklFPURASUk9RURISQ?sid=AFFBEST">
</a>
');
}else{
document.write('
<script type="text/javascript"><!--
google_ad_client = "pub-8299655316220106";
/* C4G-Forum-728x90, created 5/9/08 */
google_ad_slot = "3304568502";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
');
}
</script>
</div>
We have wrapped the banner code in a DIV element with the ID “banners”. The DIV also has an onclick event handler which calls a function trackclick() when a link within the DIV is clicked on. If the clickcount is less than 2, a Google Adsense ad is shown but if the clickcount is greater than 2, the PepperJam Network code is displayed.
Tracking Clicks Using a Cookie
Now that we have the code to display the appropriate ad block, we will need a Javascript handler to record the visitor’s clicks and set a cookie that keeps a running count of the number of clicks a visitor has made on an ad block. The Javascript code below is a basic implementation to handle these actions :
<script language="JavaScript">
var clickCount = readCookie();
if ( clickCount == null ){
clickCount = 0;
}
function trackclick() {
clickCount = parseInt(clickCount)+1;
setCookie( clickCount, 2 );
return true;
}
function setCookie( cookieValue, nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = "clicktrack="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
function readCookie() {
var nameEQ = "clicktrack=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0){
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
</script>
The script is rather simple. When the page loads, the script attempts to call readCookie and set a variable called clickcount. If readCookie() returns null (the cookie has not been set), then the clickcount is set to 0. The trackclick() function is called whenever the DIV we created in the previous step is clicked and it increments the clickcount and calls setCookie to set the incremented value.
This solution will work across all the pages on a site using the code and the Javascript can be stored in a .js file so it can easily be included into any page you wish to utilize it on. Although as I mentioned above, this method could be over-ridden if the clickbomber deletes the cookie each time the clickcount is maxxed, however, for most purposes, this application should suffice.
In tomorrows installment, we will be examining a more robust solution built on this example that uses PHP and mySQL to track the visitors IP address to ensure the Adsense click count can’t be reset by deleting the cookie.
Please feel free to leave comments on this article and I’ll be more than happy to address any questions or comments any of my readers might have.













