Class: Liquid::Locomotive::Tags::GoogleAnalytics

Inherits:
Tag
  • Object
show all
Defined in:
lib/locomotive/liquid/tags/google_analytics.rb

Constant Summary collapse

Syntax =
/(#{::Liquid::Expression}+)?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ GoogleAnalytics

Returns a new instance of GoogleAnalytics.



8
9
10
11
12
13
14
15
16
# File 'lib/locomotive/liquid/tags/google_analytics.rb', line 8

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @account_id = $1.gsub('\'', '')
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'google_analytics' - Valid syntax: google_analytics <account_id>")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/locomotive/liquid/tags/google_analytics.rb', line 18

def render(context)
  %{
  <script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '#{@account_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>}
end