Skip to content

Instantly share code, notes, and snippets.

@defkode
Created May 3, 2011 19:22
Show Gist options
  • Save defkode/954016 to your computer and use it in GitHub Desktop.
Save defkode/954016 to your computer and use it in GitHub Desktop.
Google Adsense Rails 3 helper
module AdsenseHelper
# Author: Tomasz Mazur (defkode@gmail.com)
# http://www.mydigitallife.info/2009/01/17/google-adsense-legacy-old-generation-code-reference-generate-and-get/
# :slot
# :theme
# :background
# :border
# :link
# :text
# :url
# :channel 10 digits
# :type "text", "text_image", "image"
# :alternate_url
# 120×90_0ads_al (4 links)
# 120×90_0ads_al_s (5 links)
# 160×90_0ads_al (4 links)
# 160×90_0ads_al_s (5 links)
# 180×90_0ads_al (4 links)
# 180×90_0ads_al_s (5 links)
# 200×90_0ads_al (4 links)
# 200×90_0ads_al_s (5 links)
# 468×15_0ads_al (4 links)
# 468×15_0ads_al_s (5 links)
# 728×15_0ads_al (4 links)
# 728×15_0ads_al_s (5 links)
# links_per_unit
# def link_unit(type, client, theme = {}, options = {})
# end
def ad_unit(type, client = "pub-xxxxxxxxxxxxxxxx", options = {})
units = {
:leader_board => "728x90",
:wide_skyscraper => "160x600",
:banner => "468x60",
:half_banner => "468x30",
:skyscraper => "120x600",
:vertical_banner => "120x240",
:large_rectangle => "336x280",
:medium_rectangle => "300x250",
:square => "250x250",
:small_square => "200x200",
:small_rectangle => "180x150",
:button => "125x125"
}
unit = units[type]
width, height = unit.split("x")
opts = {}
opts[:google_ad_client] = client
opts[:google_ad_width] = width.to_i
opts[:google_ad_height] = height.to_i
if options[:slot]
opts[:google_ad_slot] = options[:slot]
else
opts[:google_ad_type] = options[:type]
opts[:google_ad_format] = "#{unit}_as"
if options[:theme]
opts[:google_color_border] = options[:theme][:border]
opts[:google_color_link] = options[:theme][:link]
opts[:google_color_background] = options[:theme][:background]
opts[:google_color_text] = options[:theme][:text]
opts[:google_color_url] = options[:theme][:url]
end
end
adsense_opts = opts.map do |k, v|
v = v.is_a?(String) ? "\"#{v}\"" : v
"#{k} = #{v};" unless v.blank?
end.compact.sort.join("\n")
content_tag(:div, [javascript_tag(adsense_opts), content_tag(:script, nil, :type => "text/javascript",
:src => "http://pagead2.googlesyndication.com/pagead/show_ads.js")].join("\n").html_safe, :class => "google_adsense #{type}")
end
end
@amoldjoshi
Copy link

module AdsenseHelper
def ad_unit(type, client = "ca-pub-58xxxxxxxxx700", options = {})
units = {
:leader_board => "728x90",
:wide_skyscraper => "160x600",
:banner => "468x60",
:half_banner => "468x30",
:skyscraper => "120x600",
:vertical_banner => "120x240",
:large_rectangle => "336x280",
:medium_rectangle => "300x250",
:square => "250x250",
:small_square => "200x200",
:small_rectangle => "180x150",
:button => "125x125"
}

unit = units[type]
width, height = unit.split("x")

opts = {}
opts[:google_ad_client] = client
opts[:google_ad_width] = width.to_i
opts[:google_ad_height] = height.to_i
if options[:slot]
opts[:google_ad_slot] = options[:slot]
else
opts[:google_ad_type] = options[:type]
opts[:google_ad_format] = "#{unit}_as"
if options[:theme]
opts[:google_color_border] = options[:theme][:border]
opts[:google_color_link] = options[:theme][:link]
opts[:google_color_background] = options[:theme][:background]
opts[:google_color_text] = options[:theme][:text]
opts[:google_color_url] = options[:theme][:url]
end
end

adsense_opts = opts.map do |k, v|
v = v.is_a?(String) ? ""#{v}"" : v
"#{k} = #{v};" unless v.blank?
end.compact.sort.join("\n")

content_tag(:div, [javascript_tag(adsense_opts), content_tag(:script, nil, :type => "text/javascript",:src => "http://pagead2.googlesyndication.com/pagead/show_ads.js")].join("\n").html_safe, :class => "google_adsense #{type}")  

end
end


In the view portion of code I have:


Avertisements.


<%= ad_unit(:skyscraper, "ca-pub-58xxxxxxxxx700", {:slot => "3xxxxxxx3"}) %>

Do you have any feedback? xxx in slot and pub id just here, in code they are proper numbers. Can you please help why am i seeing all blank on my site?
Thanks.

@amoldjoshi
Copy link

Hey,
I looked at the source data generate and i found following thing. Notice that height, slot and width are in wrong order as compared to the code provided by google. Is that creating the site to be left blank? Or do you think any other reason? Thanks in advance.

<script type="text/javascript"> // </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

@defkode
Copy link
Author

defkode commented Jul 19, 2011

hey,

order of parameters doesn't count. Do you test it on localhost or external (public available host)?

Google tries do match ads for your content, if your host is not accessible sometimes there are no ads, just empty content. Google provide a mechanism for that: you can provide "alternate_url" parameter, and when there are no ads availalbe it loads content from specified url.

Does ads load correctly when pasting code generated by google adsense admin panel? It will answer the question where is the problem.

@amoldjoshi
Copy link

Thanks a lot, man. It is working fine now. I appreciate your help.

@Chaarizard
Copy link

So i put the above script on a rb file then put
<%= ad_unit(:leader_board, "your-adsense-id" %>
where i want it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment