Module: Nanoc::Toolbox::Helpers::GoogleUA

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

Overview

NANOC Helper for the Google Universal 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

#ua_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



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

def ua_tracking_snippet(ga_tracking_code=nil)
  ga_tracking_code ||= @config[:ga_tracking_code] || "UA-xxxxxx-x"
  js = <<-EOS
    (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');

    ga('create', '#{ga_tracking_code}', 'auto');
    ga('send', 'pageview');
  EOS
  ('script', js, { :type => 'text/javascript' })
end