Tracking Google AdSense Clicks With jQuery And ColdFusion
Posted November 10, 2009 at 9:36 AM
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
Newer Post
Using EXSLT To Extend XSLT With Custom Functions In ColdFusion
Older Post
jQuery Live() Method And Event Bubbling
Reader 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
@Rudi,
Oh wow - looks awesome. I'll check that out, thanks.
@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
@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.
@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
@Daniel,
In FireFox, it opened in a non-focused new tab.
@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
@Daniel,
It must be that the click event is still targeted at the IFrame and still de-focuses the primary window?
@Ben,
Maybe I am missing something, does it give a false positive if you click in the google adsense not on a link?
@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.
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
@Dave,
Once you join your AdSense and Analytics accounts, I am not sure that using this technique would offer any benefit?
@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
@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.
@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%.
@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.
@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
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 :)
@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.
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:
@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).




