Welcome to WebmasterWorld Guest from 54.226.147.190

Forum Moderators: incrediBILL & martinibuster

Message Too Old, No Replies

developing AdSense tracking script

what do you think of tracking AdSense independently?

     

monolitik

9:44 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Hello,

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

jonathanleger

4:15 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I figured it out folks...


<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.

trillianjedi

4:20 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It's a beautiful thing!

TJ

figment88

4:23 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jonathan - those are great additions. I knew you could limit to only the Google iFrame, but since I don't use them for anything else, I didn't bother. Getting the clicked URL is fantastic.

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.

trillianjedi

4:31 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It's the perfect solution. Something inside me still wants ASA to come back and reaffirm that this is ok though. It possibly opens a can of worms for them.

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

Macro

4:38 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd like to see ASA's comment on this actual code before I run it.

Me too, me too.

Chndru

4:51 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:::::: still shaking my head in disbelief :::::::

me too.

trillianjedi

4:51 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Also, before ASA replies to that, how easy would it be for a .php or .asp page to shove the IP address of the client into the log via the script? I would presume, quite easy.

That opens possibilities for AdSense users to monitor themselves for "dodgy" clicks etc.

How would google feel about that?

TJ

Noel

4:53 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Jonathan, this is looking good!

I'm not using .asp, but ColdFusion, so I guess I need to rename the "as.asp" to "as.cfm".

Would it be possible to log the data straight to a text file? Maybe with ActiveX that is embedded into your JavaScript?

If so.. Do you know how?

Noel

trillianjedi

5:01 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Noel,

Sorry, I'm not really a web coder, but don't you just do that server side? you have the variables in the calling URL.

TJ

Noel

5:05 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



trillianjedi, me so stupid... Why did I not think of this...

Thanks for the tip :-)

Noel

figment88

5:55 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



trillianjedi, also server side it is easy to pick-up the user IP. The webbug example I showed above did this in a simplistic way.

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);
}

Yidaki

6:59 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



figment88, i believe you and the others that your script works. However, it doesn't work for me - erm, on my Mac (tested OS 9.2.2 / MSIE 5.1.6). No windoze machine at hand to test it. I gonna try to debug it tomorrow to run on mac too. ;)

jonathanleger

7:20 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Quick change to the log() function in the code I posted earlier:


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.

loanuniverse

8:09 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jonathan or anyone else that knows how tis works:

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.

Windaria

8:29 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Jonathan,

Your code... that as.asp that you reference in the javascript.

What code do you have inside the as.asp file?

Where is the information logged to?

jonathanleger

8:54 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



loanuniverse:

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]

jomaxx

9:40 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Surely you can change "as.asp" to something like "dot.gif", and simply write each click to your regular log file.

<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]

jomaxx

9:49 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Holy cow, that's fast.

Have you tested the code on browsers like Netscape and Opera? I did a modified test (NOT by clicking on advertiser ads, needless to say), and the code worked in MSIE but not Netscape.

Noel

10:02 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Just to clarify.

The script posted here will only show/log IF a user clicked on ANY AdSens ad. It will also show/log the URL of the site the AdSens code is running on.

It will not show what ad the user clicked on and to what URL he went..

Is this correct?

jonathanleger

10:11 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



No, it tracks the url of the ad, too (grabs it from the status bar text).

jonathanleger

10:12 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I have not tested the code in Netscape. I will do so and post the results here.

jonathanleger

10:17 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



The code worked in Netscape 7. What version did it not work in?

jomaxx

10:24 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm using Netscape 7.0 under WinXP. I didn't do rigorous testing though, or make any attempt to trace the code, so it may not be significant.

jomaxx

11:19 pm on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The other thing I am seeing (in MSIE) is that the action IS triggered successfully but the information taken from the window status bar is wrong.

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?

Noel

12:14 am on Feb 28, 2004 (gmt 0)

10+ Year Member



Hmm I can't get it to work at all..

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

richmondsteve

12:28 am on Feb 28, 2004 (gmt 0)

10+ Year Member



jonathanleger wrote:
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.

jomaxx

12:29 am on Feb 28, 2004 (gmt 0)

WebmasterWorld Senior Member jomaxx is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Noel, you've only got the function itself there. Be sure to include the other code jonathanleger posted in message 31 of this thread.

richmondsteve

12:30 am on Feb 28, 2004 (gmt 0)

10+ Year Member



Noel, it could be due to the reason in the first part of my post above.

Dreamquick

4:29 am on Feb 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very good thread with some excellent code examples.

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

David_M

4:51 am on Feb 28, 2004 (gmt 0)

10+ Year Member



I'm trying to pass the info to a perl script with no luck, can someone suggest why it isn't working and a solution?

1) added the javascript after the adsense code.
2) change to bug.src = as.cgi?

as.cgi writes to a flat text file, and works when called directly.

Thanks!

This 205 message thread spans 7 pages: 205
 

Featured Threads

Hot Threads This Week

Hot Threads This Month