Module: Rmobio::Ads::AdSense

Defined in:
lib/rmobio/ads/ad_sense.rb

Constant Summary collapse

@@adServer =
"http://pagead2.googlesyndication.com/pagead/ads"
@@mobio_useragent =

we should probably come up with a better string

"Mobio"
@@default_ip =
"127.0.0.1"

Instance Method Summary collapse

Instance Method Details

#get_ad(keywords, ad_client, request, kw_type = "broad") ⇒ Object

better wrapper than what we hjad



35
36
37
# File 'lib/rmobio/ads/ad_sense.rb', line 35

def get_ad(keywords,ad_client,request,kw_type="broad")
  @ad=getAd(keywords,ad_client,ip=request.remote_ip,request.request_uri,useragent=request.user_agent,request.env["HTTP_USERID"],kw_type)
end

#getAd(keywords, ad_client, ip = request.remote_ip, url = request.request_uri, useragent = request.user_agent, userid = , kw_type = "broad") ⇒ Object

retrieve ad fron network with specified

  • keywords - to search

  • ad_client - the publisher ID for the network (Google AdSense publisher ID in this case)

  • ip (optional, defaults to accessing IP of client) - accessing IP

  • url (optional, defaults to URL being accessed from the request header) - URL that the ad is for

  • useragent (optional, defaults to useragent from the request header) - user agent string for the browser



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rmobio/ads/ad_sense.rb', line 81

def getAd(keywords,ad_client,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"],kw_type="broad")
  doTinyUrl=true
  #@user_id=request.env['HTTP_USERID'] if request.env and request.env['HTTP_USERID']
  # build up the various arguments in the adArgs hash
  adArgs= SequencedHash.new
  adArgs["ad_type"]="text_image"
  adArgs["client"]=ad_client
  adArgs["format"]="mobile_single"

  adArgs["ip"]=URI.escape(ip) if ip
  adArgs["markup"]="xhtml"
  adArgs["output"]="xhtml"
  adArgs["oe"]="utf-8"
  adArgs["url"]=url if url
  adArgs["useragent"]=URI.escape(useragent) if useragent
  adArgs["eip"]=Digest::SHA1.hexdigest(userid)[0..20] if userid # oneway hashed userid
  if (keywords)
    adArgs["kw"]=CGI::escape(keywords)
    adArgs["kw_type"]=kw_type  # defaults to broad, can be set to "exact"
  end
  # now built the URL to call out to based upon the base URL and ad hash
  adURL=@@adServer + "?"

  first=1 # dont put ampersand on first one
  adArgs.each_key do |x|
    if adArgs[x]
      (adURL=adURL+"&") unless first
      first=nil # start putting in &s
      adURL = adURL + x + "=" + adArgs[x]
    end
  end

  $log.info "Ad URL: " + adURL  # paste in your browser from log to verify/test
  # ok, now call google's mobile adSense service and get back the full ad
  @ad=open(adURL,"User-Agent"=>CGI::escape(useragent)).read
  if @ad
    adDoc = REXML::Document.new @ad
    @text = adDoc.elements['//p']
    if @text  # do we have a <p> element
      # ok, now text has the full ad display content including links
      # grab @url, @urltext (link text for url), @phone, @phonetext (link text for call)
      if defined? @text[1].attributes['href'] and @text[1].attributes['href']
        @adurl = doTinyUrl ? tinyUrl(@text[1].attributes['href']) : @text[1].attributes['href']
      else
        @adurl = "nolink.rwap"
      end
      @urltext=@text[1].text
      @extratext=@text[2].to_s if @text[2] # this should have everything else that is not a child element
      if (@text.size>3 and @text[3]) # only process @text[3] for phone stuff if it exists
        @phone = (doTinyUrl ? tinyUrl(@text[3].attributes['href']) : @text[3].attributes['href']) if defined? @text[3].attributes['href'] and @text[3].attributes['href']
        @phonetext=@text[3].text if defined? @text[3].text and @text[3].text
      end
    end
  end # if we get an ad back from adsense
  @ad # return the full ad text

end

#getAdByPage(adClient, ip = request.remote_ip, url = request.request_uri, useragent = request.user_agent, userid = request.env["HTTP_USERID"]) ⇒ Object

this returns an ad from Google’s mobile AdSense, given the current page

  • ad_client - your Google adsense ID

  • ip - optional, the IP you want to tell Google its coming from

  • url - optional, the page you want to put an ad on. Defaults to what is detected

  • useragent - the useragent of the mobile browser. Defaults to what is detected.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rmobio/ads/ad_sense.rb', line 44

def getAdByPage(adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"])
  @@userId=request.env['HTTP_USERID']
  adArgs= SequencedHash.new
  adArgs["ad_type"]="text"
  adArgs["channel"]="8618723264"
  adArgs["client"]=adClient
  adArgs["format"]="mobile_single"
  adArgs["host"]=request.host if request.host
  adArgs["ip"]=URI.escape(ip) if ip
  adArgs["markup"]="xhtml"
  adArgs["output"]="xhtml"
  adArgs["oe"]="utf8"
  #adArgs["ref"]=referer
  adArgs["url"]=url if url
  adArgs["useragent"]=URI.escape(useragent) if useragent
  adArgs["eip"]=Digest::SHA1.hexdigest(userid)[0..20] if userid  # oneway hashed userid
  # now build the URL to call out to based upon the base URL and ad hash
  adURL=@@adServer + "?"
  first=1 # dont put ampersand on first one
  adArgs.each_key do |x|
    if adArgs[x]
      (adURL=adURL+"&") unless first
      first=nil # start putting in &s
      adURL = adURL + x + "=" + adArgs[x]
    end
  end
  # ok, now call google's mobile adSense service and get back the full ad
  @pagead=open(adURL,"User-Agent"=>useragent).read

end