So, we all know it's a bad thing to click on your own AdSense adverts right? Well, what if you are in an office and you all share the same IP address and you know you shouldn't click on the AdSense links but other people don't realise it. Or maybe you want to block someone who is maliciously clicking on your links, trying to cause trouble between you and Google. Here's a way to block users from an IP address from clicking on the AdSense links on your site. It uses ASP.NET and VB.NET, but the principle could be used in other languages.
First of all create a .js file with the following code and call it adsense_blocker.js:
// attach function
attachOnloadEvent(adsenseBlockInit);
//do we have adsense?
function adsenseBlockInit() {
var el = document.getElementsByTagName("iframe");
for (var i=0; i<el.length; i++) {
if (el[i].src.indexOf('googlesyndication.com') > -1) {
el[i].onmouseover = adsenseBlock;
}
}
}
// when moving mouse over adsense show alert for current ad unit
function adsenseBlock() {
alert('Clicking on Google AdSense advertisements has been blocked!');
}
// attach function to window onload event
function attachOnloadEvent(func) {
if(typeof window.addEventListener != 'undefined') {
window.addEventListener('load', func, false);
} else
if (typeof document.addEventListener != 'undefined') {
document.addEventListener('load', func, false);
} else
if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onload', func);
} else {
if (typeof window.onload == 'function') {
var oldonload = onload;
window.onload = function() {
oldonload();
func();
};
} else {
window.onload = func;
}
}
}
This code will attach a mouseover event to any AdSense units you have on the page that shows the user an alert box. We don't want to block all users though, so create a default.aspx, paste your AdSense code in the page and then add the following code to your code behind file:
Imports System.Net.Dns
Partial
Class _Default
Inherits System.Web.UI.Page
Protected
Sub Page_Load(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
Me.Load
If
Not Page.IsPostBack Then
Dim strHostName As
String
Dim strClientIPAddress As
String
Dim strIPAddressToBlock As
String
'ip address we want to block
strIPAddressToBlock = "0.0.0.0"
'get user's ip address
strHostName = GetHostName()
strClientIPAddress = GetHostAddresses(strHostName).GetValue(0).ToString()
'register .js file if necessary
If strClientIPAddress = strIPAddressToBlock Then
ClientScript.RegisterStartupScript(Me.GetType(), "adsense_blocker", "<script type=""text/javascript"" src=""" & Request.ApplicationPath & "/adsense_blocker.js""></script>")
End
If
End
If
End
Sub
End
Class
Test it out by adding your IP address (look at the strClientIPAddress variable
to
find
it) and you should find that you can't click on any AdSense links. Change the IP address and you should be able to mouseover the AdSense units without getting the alert box. Remember not to click on them though! The good thing is that you don't have to change any of the Google code, as that's against their terms and conditions as well.