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  |  Permalink  |  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 »
6,513 Comments

@Rudi,

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


Nov 10, 2009 at 11:10 AM // reply »
29 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 »
6,513 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 »
29 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 »
6,513 Comments

@Daniel,

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


Nov 10, 2009 at 11:42 AM // reply »
29 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 »
6,513 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 »
29 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 »
6,513 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 »
5 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 »
6,513 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 »
5 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 »
6,513 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 »
5 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 »
6,513 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 »
5 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 »
6,513 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 »
14 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 »
6,513 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).


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »
Nov 20, 2009 at 4:52 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Case in point, Ben, you may not be aware of this, but in Railo - OnApplicationStart() & OnSessionStart() act differently than in ACF. ACF does: OnApplicationStart (1st hit) OnSessionStart (1st and e ... read »
Nov 20, 2009 at 4:46 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, That's understandable. I am not sure if this really leaves any more security holes than the fact that using old cookie-based CFID / CFTOKEN values will create a new session using the old CFI ... read »
Nov 20, 2009 at 4:42 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
My opinion is that I don't think auto-generating your own CFID/CFTOKEN is recommended. I'll have to wait for Micha to answer what the ramifications are, but he probably didn't allow this in Railo for ... read »
Nov 20, 2009 at 4:31 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Yeah, each phone has a unique ID; this is the value that is being used to hack the custom CFID / CFTOKEN session values. ... read »
Nov 20, 2009 at 4:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Is there no unique (device or otherwise) id that gets passed in the request that you can shove into the application scope? ... read »
Nov 20, 2009 at 4:21 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I mean "bug" only in the sense that it deviates from the way "Adobe ColdFusion" implements it.... only so far in that Railo is an open-source version of ColdFusion. I guess it depends on w ... read »
Nov 20, 2009 at 4:18 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Not sure how it's a bug, definitely a difference. I don't want my users or coders creating their own CFID/CFTOKEN. That's the server's job. ... read »