Forum Moderators: open

Message Too Old, No Replies

hit or miss javascript amazon click tracker

what is wrong with this code?

         

berto

12:11 am on Mar 20, 2006 (gmt 0)

10+ Year Member



I have implemented a JavaScript Amazon.com click tracker. It works, sort of. That is, sometimes it tracks clicks, but more often than not, it doesn't. (I know this, because I test its operation from several different browsers--IE, FireFox, Opera--across several different OSes--Windows XP, Windows ME, Linux.) It used to be more reliable when I opened a new Amazon.com window (using target="_blank"), but I no longer do that.

Here is the JavaScriptcode, in the <head> of the HTML document:

###############################################################################

<head>
[...]
<link rel="stylesheet" href="http://foobar.com/stylesheet.css"
type="text/css">
<script type="text/javascript">
var addr = '<!--#echo var="REMOTE_ADDR"-->';
var agent = '<!--#echo var="HTTP_USER_AGENT"-->'
var docuri = '<!--#echo var="DOCUMENT_URI"-->';
var params = "addr=" + addr + "&agent=" + agent + "&docuri=" + docuri;
function sendURL(url) {
window.location = url;
}
function alog(adpos, asin, prod) {
var cgiURL = "http://foobar.com/cgi-bin/alog.cgi?";
sendURL(cgiURL + params + "&adpos=" + adpos + "&asin=" + asin + "&prod="
+ prod);
return false;
}
</script>
</head>

###############################################################################

Here is the invoked alog.cgi script:

###############################################################################

#!/usr/bin/perl

$logfile = "/var/www/cgi-bin/log/alog";

$query_string = $ENV{QUERY_STRING};
if ($query_string =~
/^addr=([^&]+)&agent=([^&]+)&docuri=([^&]+)&adpos=([^&]+)&asin=([^&]+)&prod=(.+)
$/) {
open(LOG, ">> $logfile");
$addr = $1;
$agent = $2;
$docuri = $3;
$adpos = $4;
$asin = $5;
$prod = $6;
$time = localtime();
print LOG "$time $addr $agent $docuri $adpos $asin $prod\n";
close(LOG);
}

print "Status: 204 No Content\n\n";

exit 0;

###############################################################################

And here is a typical Amazon.com link invoking alog():

###############################################################################

<a href="http://www.amazon.com/exec/obidos/ASIN/BXXXYYYZZZ/ref=nosim/foobar-
20" rel="nofollow" onclick="alog('adfour', 'BXXXYYYZZZ', 'My Life as a Foo')">

###############################################################################

Before (when clicking on an Amazon link opened up a new browser window), this scheme tracked clicks pretty reliably, say 90% of time. Now, it tracks maybe less than 20% of the time.

Is this a timing issue? Is it dependent on browser, OS, CPU speed, ...? I can't discern any pattern. It seems to be entirely hit or miss.

I have added debugging code to determine that it's not an environment variables issue (hence failure of the pattern match, therefore no open and write to the log file). The problem seems to be that, most of the time (now), the alog() function is not being called in the first place.

What am I doing wrong here? Help!

berto

1:38 pm on Mar 22, 2006 (gmt 0)

10+ Year Member



Answering my own question...

Substituting "onmousedown" does the trick. That is, by using

onmousedown="alog()"

instead of

onclick="alog()"

I am now getting 90+% reliability. (This is true across all OS and browser combinations tested.) Problem essentially solved.