Class: Guider::Analytics

Inherits:
Object
  • Object
show all
Defined in:
lib/guider/analytics.rb

Overview

Generates HTML for Google Analytics.

Class Method Summary collapse

Class Method Details

.to_html(id) ⇒ Object

Given the ID of Google analytics tracker, returns the tracker script. Returns empty string when no ID given.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/guider/analytics.rb', line 6

def self.to_html(id)
  return "" unless id

  <<-EOHTML
  <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '#{id}']);
    _gaq.push(['_trackPageview']);
    (function() {
      var ga = document.createElement('script');
      ga.type = 'text/javascript';
      ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') +
        '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(ga, s);
    })();
  </script>
  EOHTML
end