Module: ActionView::Helpers::AdsenseHelper
- Defined in:
- lib/action_view/helpers/adsense_helper.rb
Overview
Action View Google Adsense Helper
Instance Method Summary collapse
-
#adsense_tag(options = {}) ⇒ Object
Parameters:.
Instance Method Details
#adsense_tag(options = {}) ⇒ Object
Parameters:
- client
-
16 digits
- slot
-
10 digits
- dimension
-
“#widthx#height”, default is 336x280
Example:
adsense_tag(client: 0000000000000000, slot: 1111111111, dimension: '336x280')
Generated code:
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:inline-block;width:336px;height:280px"
data-ad-client="ca-pub-0000000000000000"
data-ad-slot="1111111111"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/action_view/helpers/adsense_helper.rb', line 28 def adsense_tag( = {}) defaults = { dimension: '336x280' } = .to_h.reverse_merge(defaults).with_indifferent_access width, height = [:dimension].split('x').to_a raise 'Invalid ad client, should be 16 digits' unless [:client].to_s[/^\d{16}$/] raise 'Invalid ad slot, should be 10 digits' unless [:slot].to_s[/^\d{10}$/] raise 'Invalid ad dimension, should be "#{width}x#{height}"' unless width.to_s[/^\d+$/] && height.to_s[/^\d+$/] = [ content_tag('script', '', async: '', src: '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'), content_tag('ins', '', class: 'adsbygoogle', style: "display:inline-block;width:#{width}px;height:#{height}px", 'data-ad-client' => "ca-pub-#{[:client]}", 'data-ad-slot' => [:slot]), content_tag('script', '(adsbygoogle = window.adsbygoogle || []).push({});'), ] safe_join() end |