Accessing adsense ad objects with javascript

Discussion in 'JavaScript' started by camjohnson95, Aug 24, 2009.

  1. #1
    Can anybody tell me how I can access adsense ads with javascript? For example, if I wanted to add a click handler to each of the ads, is this possible and how would I go about this?
     
    camjohnson95, Aug 24, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think it's can be not? ads displayed in a new window (frame), so you can't handle them, visit my Javascript library for examples
     
    JavaScriptBank.com, Aug 24, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    i've tried to do this also but since they are embedded into an iframe on a remote host, there are XSS security restrictions in place that disallow usage of selectors from the parent domain. i then tried to setup events on $("google_ads_frame1") and hope to do some event bubbling magic but failed miserably - the mouseenter/leave works, although w/o feedback on what's underneath. the click does not even fire locally...

    if you find a way around it, let me know :)
     
    dimitar christoff, Aug 24, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Okay, thanks. Will do.
     
    camjohnson95, Aug 24, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I think i have worked it out... it seems to work fine for me. The only way it will record a false click is if the user sets the focus on the ad (using the TAB key), and then exits the page within the next 10 seconds... so i doubt that will happen very often. I'll post the whole script including the server-side .net for anyone that is interested. I didn't want to use a database to keep it simple, so clicks are recorded in a simple comma-seperated format like:
    URL,Ad_ID,N:confused:f_Clicks
    The whole script consists of four files and are as follows:


    clickrecorder.js (include on all pages):
    
    window.onload = function() {
        var beenClicked;
        var myFrames = document.getElementsByTagName("iframe")
        for(i=0; i<myFrames.length; i++) {
            if(myFrames[i].name.substr(0,6)=="google") {
                myFrames[i].onfocus = handler;
            }
        }
        function handler() {
            beenClicked = this.id;
            setTimeout("beenClicked=null;", 10000);
        }
        function sendclick(adId) {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET","recordclick.aspx?adId=" + adId,false);
            xmlHttp.send(null);
        }
        window.onunload = function() {
            if(beenClicked!=null) {
                sendclick(beenClicked);
            }
        }
    }
    
    Code (markup):
    recordclick.aspx:
    
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="recordclick.aspx.vb" Inherits="recordclick" %>
    
    Code (markup):
    recordclick.aspx.vb (code-behind):
    
    Imports System.IO
    
    Partial Class recordclick
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.Expires = -1
    
            Dim ref As String
           
            ref = Request.ServerVariables("HTTP_REFERER")
    
            If Request.QueryString("adId") IsNot Nothing And ref <> "" Then
                Dim sr As New StreamReader(Server.MapPath("googleadcount.txt"))
                Dim thisLine, adId, strDoc As String
                Dim cnt As Integer = 0
    
                adId = Request.QueryString("adId")
    
                Do While Not sr.EndOfStream
                    thisLine = sr.ReadLine
                    Try
                        If thisLine.Split(",")(0) = ref And thisLine.Split(",")(1) = adId Then
                            cnt = CInt(thisLine.Split(",")(2))
                            cnt += 1
                            strDoc += ref & "," & adId & "," & CStr(cnt) & vbCrLf
                        Else
                            strDoc += thisLine & vbCrLf
                        End If
                    Catch ex As Exception
                    End Try
                Loop
    
                sr.Close()
    
                If cnt = 0 Then
                    strDoc += ref & "," & adId & ",1" & vbCrLf
                End If
    
                Dim sw As New StreamWriter(Server.MapPath("googleadcount.txt"), False)
                sw.WriteLine(strDoc)
                sw.Close()
            End If
        End Sub
    End Class
    
    Code (markup):
    googleadcount.txt (blank text file)
     
    camjohnson95, Aug 24, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    unfortunately mozilla doesn't support the onfocus event for iframes.... so it will not work in firefox... currently.
     
    Last edited: Aug 24, 2009
    camjohnson95, Aug 24, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    this will record the iframe id? in my case, my iframe can contain 1 or 4 adverts

    in any case, i wanted to hijack the behaviour for mouseover/click so I can style the adverts as well as record clicks out and I just could not get it to work in firefox in any meaningful shape or form...

    out of curiosity, isn't onbeforeunload a better option - doesn't onunload tend to fire a little too late, the browser transition can interrupt the ajax call...
     
    dimitar christoff, Aug 25, 2009 IP
  8. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I wasn't sure if all browsers supported onbeforeunload ... but this will only work in I.E anyway so i guess it doesn't matter.
    Yeh it will not record what ad exactly is clicked but can still give a rough idea... I'm going to try and work on something better but this will be a start.
    Browser security is a bit of an issue, in that you cannot alter the contents of an iframe that contains a page from a different domain.
     
    camjohnson95, Aug 25, 2009 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    dimitar christoff, Aug 25, 2009 IP