Module: DfpHelper::ViewHelpers

Defined in:
lib/dfp_helper.rb

Instance Method Summary collapse

Instance Method Details

#dfp_helper_headObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dfp_helper.rb', line 29

def dfp_helper_head
  return unless dfp_helper_slots.size > 0
  o = dfp_helper_slots.collect{|i|
    _targeting = (i[:targeting]||[]).collect{|k,v| ".setTargeting(#{k.to_json}, #{v.to_json})"}.join
    "googletag.defineSlot('#{i[:id]}', [#{i[:size].join(', ')}], '#{i[:div_id]}').addService(googletag.pubads())#{_targeting};"
  }.join("\n")
  
  raw <<-END.strip
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') + 
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>

<script type='text/javascript'>
googletag.cmd.push(function() {
#{o}
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
  END
end

#dfp_helper_slot(_i, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dfp_helper.rb', line 12

def dfp_helper_slot(_i, options = {})
  @@dfp_helper_id ||= (Time.now.to_f*1000).to_i
  
  _id = "div-gpt-ad-#{@@dfp_helper_id}-#{dfp_helper_slots.size}"
  _size = options[:size] || _i.match(/\d+x\d+/)[0].split('x')
  dfp_helper_slots << options.merge({:id => _i, :div_id => _id, :size => _size})
  
  raw <<-END.strip
<!-- #{_i} -->
<div id='#{_id}' style='width:#{_size[0]}px; height:#{_size[1]}px;'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('#{_id}'); });
</script>
</div>
  END
end

#dfp_helper_slotsObject



9
10
11
# File 'lib/dfp_helper.rb', line 9

def dfp_helper_slots
  @dfp_helper_slots||=[]
end