Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

google adsense tracker

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, this is my first post.
I run a website with Google adsense. I recently made this little script, out of tutorial to track google ads.

this how the code looks like.

// javascript code after the adsense code

& lt;script type="text/javascript">
& lt;!--
function adsense_log_click()
if(window.status.indexOf('go to') == 0)
adsense_log_url_image = new Image();
adsense_log_url_image.src = '' +
escape(document.location) + '&U=' +
var elements;
if(document.getElementsByTagName) {
elements = document.body.getElementsByTagName("IFRAME");
} else if (document.body.all) {
elements = document.body.all.tags("IFRAME");
} else {
elements = Array();
for(var i = 0; i & lt; elements.length; i++) {
if(elements[i].src.indexOf('') > -1) {
elements[i].onfocus = adsense_log_click;

& lt;/script>

and this is the code for track.php

& lt;?php
// These are the parameters for connecting to the database.
define ("SITE_DATABASE_HOSTNAME", "localhost");
define ("SITE_DATABASE_PASSWORD", "password");
define ("SITE_DATABASE_USERNAME", "username");
define ("SITE_DATABASE_DATABASE", "dbname");

if(isset($_GET['U'])) {
$DestinationPage = mysql_escape_string(substr(trim($_GET['U']),0,255));
} else {
$DestinationPage = "";
if(isset($_GET['R'])) {
$SourcePage = mysql_escape_string(substr(trim($_GET['R']),0,255));
} else {
$SourcePage = "";
if($DestinationPage!= "" && $SourcePage!= "")
$RemoteAddress = mysql_escape_string(substr(trim($_SERVER['REMOTE_ADDR']),0,255));
$RemoteHost = mysql_escape_string(substr(trim($_SERVER['REMOTE_HOST']),0,255));

$query = "INSERT INTO tblAdsenseClicks ".
" (ClickDate,DestinationPage,SourcePage,RemoteAddress,RemoteHost) ".
" VALUES (".
" '".date("Y-m-d H:i:s")."',".
" '".$DestinationPage."',".
" '".$SourcePage."',".
" '".$RemoteAddress."',".
" '".$RemoteHost."')";


// Show the statistics page if the U and R variables are not given
& lt;html>& lt;head>& lt;title>Adsense click stats& lt;/title>
& lt;style type="text/css">
table.clickstats {
border-collapse: collapse;
table.clickstats th {
color: white;
background-color: #000080;
border: 1px solid #999999;
padding: 3px;
table.clickstats td {
border: 1px solid #999999;
text-align: center;
& lt;/style>
& lt;/head>
& lt;body>& lt;?php

$query = "SELECT ClickDate,DestinationPage,SourcePage,RemoteAddress,RemoteHost ".
" FROM tblAdsenseClicks ORDER BY ClickDate ";
print "& lt;/body>& lt;/html>";

// Connects to the site defined database and returns the connection handle.
// Uses a persistent connection and does error checking.
function connect_to_site_database()
// Connect to the mysql server
$link = mysql_pconnect(constant("SITE_DATABASE_HOSTNAME"),
die("Unable to connect to site database!");

// Select the database
or die("Unable to select database: ".constant("SITE_DATABASE_DATABASE"));

return $link;

// Standard function to run a query, with error checking.
// Returns the resultset.
function run_query($query)
// Connect to the database

// Run the query
$result = mysql_query ($query) or die ("Invalid query: $query");

// Return the result set
return $result;

// prints the query results in a table with the given table style class
function print_query($query, $table_class)

// Select all the records from the addressbook and store them in $result
$result = mysql_query ($query) or die ("Invalid query: $query");

// Get the number of columns (or fields) in the results
$num_fields = mysql_num_fields ($result);

// Get the number of rows in the results
$num_rows = mysql_num_rows ($result);

// Start printing the table to show all the records
echo "& lt;table class=\"$table_class\">\n";
echo "& lt;tr>";

// Print out all the field names as the first row table header
while ($i & lt; $num_fields) {
// Get the field name from the result
$fname = mysql_field_name ($result, $i);
echo "& lt;th>".$fname."& lt;/th>";
echo "& lt;/tr>\n";

// Now print out all the data
while ($row = mysql_fetch_row ($result)) {
echo "& lt;tr>";
while ($k & lt; $num_fields) {
echo "& lt;td>".$row[$k]."& lt;/td>\n";
echo "& lt;/tr>\n";
echo "& lt;/table>\n";

// Do a little clean up


the above code is working fine. this is how it looks like when its working:

What i really want to do is add a lil feature. It is to, capture the members name who is logged in and username is stored in cookie and also count how many ads he clicked on and send the information to my database, just like how the above code sent information about the
Google ads to my database.

if you are unsure what i am asking for, take a look at this picture i made in paint (visual representation of my idea)

Thank you,


[Bear edit: reduced shouting in subject]
[ July 10, 2005: Message edited by: Bear Bibeault ]
author and iconoclast
Posts: 24204
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Welcome to JavaRanch!

Even though Java is ten years old, and JavaScript is nine, people still don't always know that they're two seperate, utterly unrelated languages. You're using JavaScript, but this forum is for discussing Java.

We've got a forum devoted to HTML and JavaScript here. I will move this thread there for you.

We've also got a rule about display names: your display name must consist of a real-sounding first and last name. Nicknames and single names aren't acceptable. You can change your display name here. Thanks for cooperating!
[ July 10, 2005: Message edited by: Ernest Friedman-Hill ]
Remember to always leap before you look. But always take the time to smell the tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic