Coldfusion blog - by phil porter
   

fearlessFusion.com is my Blog and Online portfolio; I am currently the Director of Visualweb Ltd. Read More


Google Adsense Tracking

So today i decided to utilise my adsense account which i've had since 2004. I can't remember the last time I actually used it, but it hasn't been active for quite awhile.

Anyway, I've put some ad code here and there and I thought to myself, wouldn't it be great if I could detect when someone clicked on an ad and do some simple logging. Now I know adsense allows you to gather certain information using channels, but i wanted to do my own tracking.

So the following is what I came up with:

<script type="text/javascript">
var iFr;
var elements;

function AdSenseLog()
{
/* do some logging - maybe with ajax */
}


function adsense(){
   elements = document.getElementsByTagName("iframe");
   for (var i = 0; i < elements.length; i++)
    {
    if(elements[i].src.indexOf('googlesyndication.com') > -1)
    {
    AttachEvent(elements[i],"focus",AdSenseLog,false)
    iFr=elements[i];
    }
    }
}

function AttachEvent(obj,evt,fnc,useCapture){
   if (!useCapture) useCapture=false;
   if (obj.addEventListener){
      obj.addEventListener(evt,fnc,useCapture);
      return true;
   } else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
   else{
      MyAttachEvent(obj,evt,fnc);
      obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
   }
}

/*The following are for browsers like NS4 or IE5Mac which
don't support either attachEvent or addEventListener*/


function MyAttachEvent(obj,evt,fnc){
   if (!obj.myEvents) obj.myEvents={};
   if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
   var evts = obj.myEvents[evt];
   evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
   if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
   var evts = obj.myEvents[evt];
   for (var i=0,len=evts.length; i< len; i++) evts[i]();
}

window.onload = adsense;
</script>

Basically the script runs the function adsense when the window/page loads. This fuction scans through your page and attaches an 'onfocus' event to your google Adsense ads. All google adsense ads are in iframes (generated by the javascript you include in you pages). So, every time someone gives focus to an advert, the AdSenseLog function is called.

I haven't included the code i'm using for logging as i'm still working on it, but there is no reason why you couldn't utilise ajax to call some server side script/functions to do some logging.

You'll also notice that I've created my own function called AttachEvent. This is because IE doesn't support. The DOM friendly 'addEventListener' like Mozilla/FireFox does. IE uses its own event managment called 'attachEvent' (lower case 'a'). Therefore I needed a simple cross-browser solution as above, which i've called 'AttachEvent' (uppercase 'A').



Related Blog Entries

Comments

Sponsored Links

 

 




Calendar

« May 2008 »
M T W T F S S
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  


Recent Comments

Multiple SQL inserts: Oracle, Mysql, Mssql

JAYESH TALSANIYA said: in mssql u cannot use parentheses in select statement. use as below: INSERT INTO [TABLE] (column1,... [More]

Multiple SQL inserts: Oracle, Mysql, Mssql

JD said: I found with MSSQL, I couldn't use parentheses in my select statements for insert. So, from your ex... [More]

BlogCFC ping list and timeouts

Will Wilson said: Nice :) that will come in handy, thanks! [More]

Is this a Coldfusion 8 BUG

Barb said: I'm having the same problem . . . when I start CF8, it seems to hang during or just after the user M... [More]

Is this a Coldfusion 8 BUG

João Fernandes said: Phil, when you install CF8 they give you an option to install LCDS within CF. The messageBrokerServl... [More]





W3C Design Gallery

REKNOVA innovative reklame Colibri CMS
Michael Potts Photography Richard Deverell, illustrator
Illustrations & Web Design CROSSING TV