My PPC Click Fraud Modification

I thought this might be useful for someone in helping combat Adwords click fraud. I’ve provided sample code from my implementation. Hope it helps someone.

This modification uses data from the API to add the following values to the Visitor Log:

CIDR: (would like to add one click IP exclusion via Adwords API as soon as I can figure out SOAP)
Maxmind IP: (I use maxmind for GEOIP on the landingpage)
Blacklisted: (Yes/No ip address used by known spammers)
Known Bot: (Yes/No ip address is used by known spambots)
Location: CIty/State (so you don’t have to mouse over the flag)

You’ll need to install the custom dimension plugin and configure 4 custom dimensions:

maxmindIP (if you’re using)

Once you register for the AP (it’s paid unfortuantley) you’ll receive an API key and sample code for implementation. They provide javascript and PHP. I chose PHP because I don’t like the idea of client side API keys.

PHP code for API lookup:

<?php //you'll have to replace your credentials here $userip = $_GET ['userip']; /* Any IP Address or Website Name */ /**********************************/ $query = $userip; /* Your API Details */ /**********************/ $api_id = "YOUR_API_ID"; $api_key = "YOUR_API_KEY"; $api_url = ""; /* Whois Result */ /******************/ $url = create_api_url($query, $api_id, $api_key, $api_url); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $ch, CURLOPT_HEADER, 0); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch, CURLOPT_TIMEOUT, 90); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 90); $data = curl_exec($ch); curl_close($ch); $arr = json_decode($data, true); /* Examples */ /******************/ // Example1 - Display IP Address Owner/s if (isset($arr["status"]) && $arr["status"] == "error") { echo "Error! ".(isset($arr["error_desc"])?$arr["error_desc"]:""); } else { if (isset($arr["owners"]["owner"]["ownerName"])) echo "".$arr["owners"]["owner"]["cidr"]; echo '||'; if (isset($arr["owners"]["owner"]["ownerName"])) echo "".$arr["crawlerbot_use_ip"]; echo '||'; if (isset($arr["owners"]["owner"]["ownerName"])) echo "".$arr["ip_blacklist"]["blacklist"]; } function create_api_url($query, $api_id, $api_key, $api_url, $timestamp = '') { $url = ""; if (!$timestamp) $timestamp = gmdate("Y-m-d_H:i:s"); if (trim($query) != '') { $url = $api_url."/".$query.'/api_id/'.$api_id.'/api_key/'.$api_key; $signature = md5($url.'/timestamp/'.$timestamp); $url .= '/signature/'.$signature.'/timestamp/'.$timestamp; } return $url; } ?>

Javascript/Jquery to query/parse response and set custom dimensions.

var ajax = getHTTPObject();

function getHTTPObject()
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);

} else {
  //alert("Your browser does not support XMLHTTP!");
return xmlhttp;


function getCIDR() {
var userip = $("#remote").val();

if (userip)
var url = “CIDRtest.php”;
var param = “?userip=” + escape(userip);“GET”, url + param, false);
myipms = ajax.responseText.split("||");;
var cidr = myipms[0];
var bot = myipms[1];
var blacklist = myipms[2];
if (ajax.readyState == 4)
_paq.push([‘setCustomDimension’, 3, cidr]);
_paq.push([‘setCustomDimension’, 4, userip]);
_paq.push([‘setCustomDimension’, 5, bot]);
_paq.push([‘setCustomDimension’, 6, blacklist]);


Make sure your custom dimension ids correspond to what you set up in your installation.

Finally, you’ll need to add custom dimensions and city/state info the visitor log.
Navigate to the /plugins/live/templates/_dataTableViz_visitorLog.twig file and add the following code where you’d like the info to appear. I put mine at line 154.

Location: {{ visitor.getColumn(‘city’) }}, {{ visitor.getColumn(‘region’) }}
{% else %}
Location: {{ visitor.getColumn(‘location’) }}
{% endif %}

CIDR: {{ visitor.getColumn(‘dimension3’) }}

Maxmind: {{ visitor.getColumn('dimension4') }}
Blacklisted: {{ visitor.getColumn('dimension6') }}
Known Bot: {{ visitor.getColumn('dimension5') }}

Make sure the dimension numbers correspond to the dimensions you configured earlier.

If you’ve set up and configured API correclty you’ll now have CIDR, Known Bot, and Blackist information in your visitor log. Hope this helps someone, It would make a good plugin, however, plugin development is outside of my wheelhouse.