Module: Nanoc::Toolbox::Helpers::GoogleAnalytics

Includes:
HtmlTag
Defined in:
lib/nanoc/toolbox/helpers/google_analytics.rb

Overview

NANOC Helper for the Google Analytics JS to add at the end of the layout.

This module contains helper functions to generate the JS code snipet used to track your site analytics by simply entering your tracking ID as parameter or in the configuration file

See Also:

Author:

Instance Method Summary collapse

Methods included from HtmlTag

#content_tag, #tag, #tag_options

Instance Method Details

#ga_tracking_snippet(ga_tracking_code = nil) ⇒ String

Return the javascript code snipet to use in your layout or views

Parameters:

  • ga_tracking_code (String) (defaults to: nil)

    the Google Analytics Tracking Code

Returns:

  • (String)

    the script tag to place in your layout


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nanoc/toolbox/helpers/google_analytics.rb', line 19

def ga_tracking_snippet(ga_tracking_code=nil)
  ga_tracking_code ||= @config[:ga_tracking_code] || "UA-xxxxxx-x"
  js = <<-EOS
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '#{ga_tracking_code}']);
    _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);
    })();
  EOS
  ('script', js, { :type => 'text/javascript' })
end