-
-
Save defkode/954016 to your computer and use it in GitHub Desktop.
module AdsenseHelper | |
# Author: Tomasz Mazur ([email protected]) | |
# 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 |
Simplest scenario:
<%= ad_unit(:leader_board, "your-adsense-id" %>
using custom colors for border, link, background, text or url
for example: white text on black background
<%= ad_unit(:square, "you-adsense-id", { :theme => { :link => "#fff", :background => "#000" } }) %>
You can also provide ad slot id (google_ad_slot). When you provide this option theme parameters will be ignored, because you specify colors in adsense admin for that ad.
<%= ad_unit(:leader_board, "your adsense id", {:slot => "ad-slot-id"}) %>
Generally you can pass any adsense options as third argument. Most used parameters are beside ad colors and slot" are:
- "type" ("text", "text_image", "image")
- "alternate_url"
for more information how to use them please read adsense documentation.
The conclusion is just copy the helper file to your code. Make all necessary changes and call from views. If that is the case you made everyone's life easy. I highly appreciate your time and comments.
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.
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.
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.
Thanks a lot, man. It is working fine now. I appreciate your help.
So i put the above script on a rb file then put
<%= ad_unit(:leader_board, "your-adsense-id" %>
where i want it?
how do you use it in real code?