Module: Awestruct::Extensions::GoogleAnalytics

Defined in:
lib/awestruct/extensions/google_analytics.rb

Instance Method Summary collapse

Instance Method Details

#google_analytics(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/awestruct/extensions/google_analytics.rb', line 22

def google_analytics(options={})
  options = defaults(options)

  html = ''
  html += %Q(<script type="text/javascript">\n)
  html += %Q(var _gaq = _gaq || [];\n)
  html += %Q(_gaq.push(['_setAccount','#{options[:account]}']);\n)
  html += %Q(_gaq.push(['_gat._anonymizeIp']);\n) if options[:anonymizeIp]
  html += %Q(_gaq.push(['_trackPageview']);\n)
  html += %Q(setTimeout("_gaq.push(['_trackEvent','#{options[:bounceTime]}_seconds','read'])", #{options[:bounceTime]}000);\n) if options[:bounceTime]
  html += %Q(_gaq.push(['_require', 'inpage_linkid', '//www.google-analytics.com/plugins/ga/inpage_linkid.js']);\n) if options[:linkAttribution]
  html += %Q[(function() {\n]
  html += %Q( var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n)


  if options[:demographics]
    html += %Q( ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';\n)
  else
    html += %Q( ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n)
  end


  html += %Q( var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n)
  html += %Q[})();\n</script>\n]
  html
end

#google_analytics_asyncObject



16
17
18
19
20
# File 'lib/awestruct/extensions/google_analytics.rb', line 16

def google_analytics_async()
  # deprecated
  $LOG.warn "google_analytics_async is deprecated. Please use google_analytics or google_analytics_universal." if $LOG.warn?
  google_analytics()
end

#google_analytics_universal(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/awestruct/extensions/google_analytics.rb', line 49

def google_analytics_universal(options={})
  options = defaults(options)

  html = ''
  html += %Q(<script>\n)
  html += %Q( (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
              (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
              m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
              })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n )
  html += %Q(\n)
  html += %Q(ga('create', '#{options[:account]}', 'auto');\n)
  html += %Q(ga('send', 'pageview');\n)
  html += %Q(ga('set', 'anonymizeIp', true);\n) if options[:anonymizeIp]
  html += %Q(ga('require', 'displayfeatures');\n) if options[:demographics]
  html += %Q(ga('require', 'linkid', 'linkid.js');\n) if options[:linkAttribution]
  html += %Q(setTimeout("ga('send', 'event', 'read', '#{options[:bounceTime]} seconds')", #{options[:bounceTime]}000);\n) if options[:bounceTime]
  html += %Q(\n)
  html += %Q(</script>\n)
  html
end