Tracking Google AdSense Clicks With jQuery And ColdFusion

Posted November 10, 2009 at 9:36 AM

Tags: ColdFusion, Javascript / DHTML

While Google AdSense is in no way a driving force behind my blogging - I just love being a programmer - sometimes, it would be nice to know which pages on my site generated the most Google AdSense clicks. This might be something that I can find out from Google AdSense reporting or from Google Analytics; but, to be honest, I don't know all that much about those services and I wanted to see if there was a way this kind of information could be tracked in the domain of my understanding.

 
 
 
 
 
 
 
 
 
 

Because the content of a Google Ad lives inside of an IFrame under a different domain, I don't have the browser's permission to get in there and hijack any of the clicking events. As such, I think the best that we can do is "guesstimate" as to when an ad is clicked. But unfortunately, we can't perform that guesstimation with clicking since the click event won't bubble up from the AdSense IFrame. Luckily, however, we can track mouse movements over and out of the IFrame. Using this fact, and the window's blur() event, I think we can come up with something rather convincing.

In the following demo, we are going to use the mouseover and mouseout events to keep in an internal flag as to whether the user's cursor is over a Google AdSense unit. Then, when the window blurs (loses focus), we are going to see if the user's cursor is currently over a Google AdSense unit. If so, then we can assume to some degree that the blur event was triggered when the user clicked on the Google ad.

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>Tracking Google AdSense Clicks With jQuery And ColdFusion</title>
  • <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready to be interacted with, hook up
  • // the DOM events for Google AdSense tracking.
  • jQuery(function( $ ){
  •  
  • // I am a flag that will determine if the user is
  • // currently mousing over a Google AdSense.
  • var isOverGoogleAd = false;
  •  
  • // When a user mouses over a Google AdSense iFrame, we
  • // want to track that activity. This way, when the
  • // current window blurs, we can guesstimate as to
  • // whether or not the window-blur was due to the user
  • // clicking the Google ad.
  • $( "iframe[ id *= google ]" )
  • .mouseover(
  • function(){
  • isOverGoogleAd = true;
  • }
  • )
  • .mouseout(
  • function(){
  • isOverGoogleAd = false;
  • }
  • )
  • ;
  •  
  • // Now that we are tracking the mouse movements over
  • // the Google AdSense, let's track the window's blur
  • // event to see if we can guesstimate the AdSesnse
  • // usage.
  • $( window ).blur(
  • function(){
  • // Check to see if the user was over a Google
  • // AdSense ad when the window was blurred.
  • if (isOverGoogleAd){
  •  
  • // Because the user was mousing over a
  • // Google AdSense iFrame when the window
  • // was blurred, it is reasonable to
  • // estimate that the blurring is due to
  • // the user clicking one of the ads.
  • $.ajax({
  • type: "post",
  • url: "track.cfm",
  • data: {
  • adUrl: window.location.href
  • }
  • });
  •  
  • }
  • }
  • )
  • // Focus the window by default.
  • .focus()
  • ;
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Tracking Google AdSense Clicks With jQuery And ColdFusion
  • </h1>
  •  
  • <p>
  • Google AdSense elements have to be on the same page as the
  • primary content of the page. As such, we will mix in the
  • AdSense SCRIPT tags within this content.
  • </p>
  •  
  • <!--- Google adsense. --->
  • <script type="text/javascript">
  • <!--
  • google_ad_client = "pub-1768217795487021";
  • google_ad_width = 468;
  • google_ad_height = 60;
  • google_ad_format = "468x60_as";
  • google_ad_type = "text_image";
  • //-->
  • </script>
  • <script
  • type="text/javascript"
  • src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  • </script>
  •  
  • <p>
  • Here is some more content for the page. To fill up some
  • space, I'll just talk about some random stuff that is
  • not that interesting.
  • </p>
  •  
  • </body>
  • </html>

When we have determined that the blur() event was triggered by a Google ad click, we make a jQuery AJAX request to the server, sending the current location of the browser as the request data. This request is then received by a simple ColdFusion page that, for this demo, appends the URL to a given file:

 Launch code in new window » Download code as text file »

  • <!---
  • I am the location of the browser when a Google AdSense ad
  • was clicked (theoretically).
  • --->
  • <cfparam name="form.adUrl" type="string" default="" />
  •  
  • <!--- Make sure that the URL has a valid length. --->
  • <cfif len( form.adUrl )>
  •  
  • <!---
  • Simply append the URL to the tracking file for this demo.
  • In reality, you would probably insert this into a database
  • or an email.
  • --->
  • <cffile
  • action="append"
  • file="#expandPath( './google_ad_clicks.txt' )#"
  • output="#dateFormat( now(), 'mm/dd/yy' )# - #form.adUrl#"
  • />
  •  
  • </cfif>
  •  
  • <!--- Retur a response just so the AJAX can finish ASAP. --->
  • <cfcontent type="text/plain" /><cfabort />

This is simple, but I think the theory is sound. Like I said above, I don't really know Google AdSense or Google Analytics all that well; so, if this kind of information is easily obtainable form those services, please let me know.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 10, 2009 at 10:02 AM // reply »
1 Comments

Ben,

Cool ideas here. This could really be expanded to a variety of uses. I have not worked with Adsense myself, but I was told by a colleague about this article: http://adsense.blogspot.com/2009/04/analytics-integration-for-all.html

They claim it is a feature within Adsense, but I've not tried it out.

-Rudi


Nov 10, 2009 at 10:16 AM // reply »
7,207 Comments

@Rudi,

Oh wow - looks awesome. I'll check that out, thanks.


Nov 10, 2009 at 11:10 AM // reply »
32 Comments

@Ben,

I think I see a couple areas this may fail

first case

What if a user uses ALT+TAB while the cursor is over the GoogleAd

second case

What if the user opens the link in a new tab? which I don't think blurs the window at the time of the click


Nov 10, 2009 at 11:23 AM // reply »
7,207 Comments

@Daniel,

I had the same suspicion regarding the ALT-TAB. The ALT-TAB does seem to trigger the event as a false-positive. The new-window, CTRL+CLICK, however, triggers it correctly.

This is, of course, just an estimation as to when the user clicks on the ad. One thing I was thinking about, which was a bit beyond the scope was to add a "complete" call back for the ad such that, let's say, 7 seconds after the AJAX call returns, it launches another request to erase the previous tracking.

The idea there being that if the page still existed 7 seconds later, chances are the page is not loading another URL. Of course, this would return a false-negative if the use opened the ad in the other window.

So again, just an educated guess as to when this stuff is clicked, certainly not a dead-on accurate approach.


Nov 10, 2009 at 11:33 AM // reply »
32 Comments

@Ben,

did the CTRL+Click open a new window or new tab, I know when it is a new tab the tab opens in the background; However, when it is a new window, the window is in the foreground


Nov 10, 2009 at 11:40 AM // reply »
7,207 Comments

@Daniel,

In FireFox, it opened in a non-focused new tab.


Nov 10, 2009 at 11:42 AM // reply »
32 Comments

@Ben,

Thanks for clarifying, wasn't sure with the way you worded it before.

Interesting that the blur() even is triggered there. I wonder if it has to do with the internal workings of firefox


Nov 10, 2009 at 12:05 PM // reply »
7,207 Comments

@Daniel,

It must be that the click event is still targeted at the IFrame and still de-focuses the primary window?


Nov 10, 2009 at 12:10 PM // reply »
32 Comments

@Ben,

Maybe I am missing something, does it give a false positive if you click in the google adsense not on a link?


Nov 10, 2009 at 12:16 PM // reply »
7,207 Comments

@Daniel,

Oops, I think I got myself confused :) I believe earlier I called teh CTRL+CLICK a false positive because it didn't close the current window. But, I forgot that closing the window was not the main motive - tracking the clicks was.

In summary, what I saw:

CTRL+CLICK (new tab) - successfully tracked.

ALT+TAB - false-positive for click.


Nov 16, 2009 at 5:05 PM // reply »
9 Comments

I am not sure if someone above answered this but you can track this when you marry Google Analytics and Google AdSense together. It works great at tracking which pages are preforming the best.

Dave


Nov 16, 2009 at 5:06 PM // reply »
7,207 Comments

@Dave,

Once you join your AdSense and Analytics accounts, I am not sure that using this technique would offer any benefit?


Nov 16, 2009 at 5:28 PM // reply »
9 Comments

@Ben

I don't think so, but I still enjoyed your video on the topic.

If I remember right they allowed us to join the accounts earlier this year. It has been a great thing because now I know which pages and keywords bring in the most $ and I can adjust for that.

Dave


Nov 16, 2009 at 5:29 PM // reply »
7,207 Comments

@Dave,

It's definitely very cool; I only found about the linking of AdSense and Analytics in @Rudi's comments above. So, it's very new to me; so far, it seems very cool in my Analytics.


Nov 16, 2009 at 5:36 PM // reply »
9 Comments

@Ben

I have a hobby solar site and the info I get from knowing which pages are performing the best has increased my monthly revenue by a lot - perhaps 20%.


Nov 16, 2009 at 5:48 PM // reply »
7,207 Comments

@Dave,

Sounds awesome. I know what you did to increase the revenue is probably something you don't care to share; but, if you have any tidbits of insight, I'm sure we'd all love to hear.


Nov 16, 2009 at 5:59 PM // reply »
9 Comments

@Ben,

Sure I would love to share. I owe you at least that since you have answered some of my coding questions in the past.

Here is one easy tip, check out my SolarDave.com site and notice what I did on the home page, I placed all my top producing $ pages under the heading "Popular Posts", and doing that increase the traffic to those pages and generated more revenue for me. I wouldn't have known which posts made the most $ without the joined accounts.

Again this is just my hobby site and I have used other methods on my main sites like knowing which pages to pass the most link juice to for internal and external links. More links to a page usually means more traffic to a page - which means more $.

Also it is about looking at themes, for instance anytime I write a post on my Solar site that has the theme of DIY I usually get the highest CTR on those pages. Also since you are getting into video, post your video on YouTube and check out how long people actually watch it and that lets you know how engaged they are with that theme.

Dave


Nov 17, 2009 at 7:39 AM // reply »
1 Comments

Really Nice video with useful explanation. But i have some questions in my mind..

1. The technique will pass Google Terms and conditions?

2. Where you are hosted the above video? Or its self hosted? If self hosted, Which plugin you are used to embed video in your blog?

3. It seems you are using Jing for your video demo. Which video format you are used to record this video.

Video buffers faster than ever :)


Nov 17, 2009 at 2:17 PM // reply »
7,207 Comments

@Dave,

Ahh, very nice - driving as much traffic to the top earning posts. That makes a lot of sense. It will be interesting to see what the Analytics tells me. Looking forward to getting a bit more data in there.

@Selvam,

Yeah, I am using JING and I self-host the videos since they are quite small. You can hook JING directly into FTP.


Nov 18, 2009 at 4:58 PM // reply »
19 Comments

Hmmm, is there not away to see if someone is leaving your site? -- You know like those annoying little alert boxes that come up on sales page ( Are you sure you want to leave this AMAzING offer! ) you know something like that? But have it tied into the iframe?

:confused:


Nov 18, 2009 at 5:01 PM // reply »
7,207 Comments

@Jody,

You can fire an onUnload() event on the window; however, that doesn't happen until the URL *actually* changes; this might be a few second delay between when they click and when they navigate away. In this time, I didn't want the user to mouse-away from the IFrame (and cancel the tracking).


Jim
Dec 1, 2009 at 4:08 AM // reply »
1 Comments

I'd like to ask the same question Selvam asked (but not answered):

Is this technology allowed by Google's Terms and Conditions?


Dec 18, 2009 at 3:52 AM // reply »
5 Comments

Is there any reason why you can't just wrap the googleAds script tag with a container div tag and then add an OnClick event to that in JQuery?


Jan 5, 2010 at 8:59 AM // reply »
7,207 Comments

@Jim,

I don't see how it could be against policy.

@Paolo,

Hmm, interesting question. I'm trying to think back if I tried that or not. The only problem might be that the click would be trapped by the IFRAME document. The question becomes - can an event bubble up across documents (from IFRAME to parent page).

I am not sure if it can.


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 9, 2010 at 4:14 AM
Ask Ben: Converting a Query to an Array
Exellent script! Tried to make it shorter, gained approx 10% performance improvement :) [CODE] <cffunction name="queryToArray" access="public" returntype="array" output="false"hint="This turns a q ... read »
Feb 9, 2010 at 3:16 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Just a quick fix for the code example above: $('.panel').height() should instead be... $('.panel').outerHeight() ... read »
Feb 9, 2010 at 3:09 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
I came across this page because I was searching for a certain behavior. I didn't just want the element to slide up; I wanted it's contents to slide up with it. As if the contents were written on a p ... read »
Feb 9, 2010 at 12:57 AM
Ask Ben: Creating A PDF And Attaching It To An Email Using ColdFusion
Have you got any reference to code explaining the procedure to use cfdocument, file to disk and attach to email? ... read »
Feb 8, 2010 at 11:27 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Randi, While I can appreciate your specific situation, I personally am not a fan of a situation where people have the option to run their own ad-hoc reports on the database. This is, honestly, a r ... read »
Feb 8, 2010 at 11:15 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Candice, No problem - glad you got it figured out. @David, To view the XLS document as an XML file, if I remember correctly, I actually opened up the document in Excel and then went "File > ... read »
Feb 8, 2010 at 11:11 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob, As @Julian pointed out, you need to enable multiple queries in order to run queries containing semi-colons. For more info on that, take a look at this post: http://www.bennadel.com/blog/120 ... read »
Feb 8, 2010 at 11:08 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
@Robert, Awesome. When you start reading it, I'd love for you to drop by and share your comments. I happen to love the book and am always happy to have a good conversation about it. ... read »