Forum Moderators: martinibuster
I have seen posts that wish detailed AdSense reports such as site level or page level stats.
Currently, we are working on a script to track AdSense impressions and clicks. The script lets you track the stats of sites, pages, referrers, color combinations, ad formats, visitor countries, paid or alternate ads displayed etc.
To remove questions, it conforms to Google TOC completely. No change to AdSense code, no interference.
I want to know your opinions on tracking AdSense independently. Do you personally want to use such a script? What features would you expect?
Thanks
<script language=javascript>
function log() {
if (window.status.indexOf('go to') == 0)
{
bug = new Image();
bug.src = 'as.asp?ref=' + document.referrer + '&url=' + window.status.substring(5);
}
}var elements;
elements = document.getElementsByTagName("iframe");
for (var i = 0; i < elements.length; i++) {
if(elements[i].src.indexOf('googlesyndication.com') > -1)
{
elements[i].onfocus = log;
}
}
</script>
This script has two purposes: the log() function checks to make sure that the terms 'go to' are at the beginning of the window status bar (which will prevent it from tracking clicks to the 'Ads by Google' link). The contents of the status bar at the time of the click are also passed to the ASP script (with the exception of the first five letters, the 'go to'). That way the link that the user clicked on is also passed to the script.
The second part of the script iterates through all of the iframes on the page and traps only those iframes which are pointing to googlesyndication.com--AdSense.
That's why I'm not a programmer, I can only write initial code. Debugging, adding features, dealing with multiple browsers, etc. drives me crazy.
From an AdSense webmasters point of view though, it replaces the Google control panel in all but telling google where to send the cheques.
Per site stats and a whole load of other goodies that we've wanted for a while now become possible.
I'd like to see ASA's comment on this actual code before I run it.
TJ
That opens possibilities for AdSense users to monitor themselves for "dodgy" clicks etc.
How would google feel about that?
TJ
In actual practice, you also have to worry about proxies. The actual PHP code I use is
if (getenv(HTTP_CLIENT_IP)){
$ip=getenv(HTTP_CLIENT_IP);
}
else {
$ip=getenv(REMOTE_ADDR);
}
function log() {
if (window.status.indexOf('go to') == 0)
{
bug = new Image();
bug.src = 'as.asp?ref=' + document.location + '&url=' + window.status.substring(5);
}
}
</script>
Instead of using document.referrer, I changed to document.location, so I'd get the page that they clicked on the ad instead of who referred them to that page.
A couple of quick questions from a non-programmer:
1) Do I need to do anything else besides inserting the code in my pages?
2) I am using php includes to add the adsense code to the pages. Could I just paste the tracking script before the adsense code?
3) Will the file with the information on the clicks be generated automatically or do I need to start one? What will be the name? Will it be in root? can I determine a location?
This is very sweet thanks to all that are contributing to the development of the script.
To answer your questions:
1) Yes, you need a server-side script to take the data passed from the javascript and write it to a database. You also, obviously, need a database setup to take the information and save it to be retrieved later.
2) You actually want to paste my code -AFTER- the adsense code, not before it, so it can find the IFRAME written by the adsense code.
3) The file is not generated automatically. You need a database already created.
<snip>
[edited by: Jenstar at 4:00 pm (utc) on Mar. 6, 2004]
[edit reason] No sticky me requests, please! [/edit]
<edit>Actually the as.asp code already writes an entry to the log file. But if that script does not exist then it will generate a 404 error, which is not optimal.</edit>
[edited by: jomaxx at 9:44 pm (utc) on Feb. 27, 2004]
This may be due to the presence of the Alexa toolbar (Alexa has a way of "taking over" the status bar). I normally surf with Alexa enabled, and the script actually reported one of the URL's showing in the toolbar. When I disabled Alexa, the script did not show any URL.
Anybody else playing with this and got it working?
The code that I added to the page that is serving the AdSense is:
<script language=javascript>
function log(){
if (window.status.indexOf('go to') == 0)
{
bug = new Image();
bug.src = 'log.cfm?ref=' + document.location + '&url=' + window.status.substring(5);
}
}
</script>
I've also put the code right under the AdSense code itself!
The log.cfm file (that the script calls is working fine!
When I call the "log.cfm?ref= bla bla bla" the data will show ok in the logfile)
I guess that for some reason the log.cfm is not called!
Any sugestions what I'm doing wrong with the javascript?
Noel
bug.src = 'as.asp?ref=' + document.referrer
I suggest that anyone who implements this specify the absolute URL of the script instead of the relative URL. In other words '/as.asp', not 'as.asp'. Otherwise if as.asp isn't in the same directory as the page it's called from as.asp will not be found.
+ '&url=' + window.status.substring(5);
That should be window.status.substring(6). I haven't tested against AdSense, but I discovered these two items testing the JS code and a PHP logging script.
Question on browser compatibility; I have a version of this working under IE6, however under Mozilla (Firefox 1.7a) & Opera (7.23) it runs the event attachment code (elements[i].onfocus = log;) with no errors, but I'm unable to get the event to actually fire.
I tried the same event attachment code on another element and I see my debug messages so both my function and the event attachment code are working & supported, which just leaves the iframe as a possible cause ... any ideas?
- Tony