Google analytics helper - aadlani/nanoc-toolbox GitHub Wiki

Helper that generates the JS code snippet used to track your site with analytics.

http://rubydoc.info/github/aadlani/nanoc-toolbox/Nanoc/Toolbox/Helpers/GoogleAnalytics

Setup

In the folder lib/helpers_.rb add the Helper inclusion as following:

# Helpers of the nanoc-toolbox
include Nanoc::Toolbox::Helpers::GoogleAnalytics

Usage

Default integration

To add the Google Analytics Tracking Code to your layout, simply add the following helper call, with you tracking code as parameter:

<%= ga_tracking_snippet("UA-XXXXXXX-X") %>

With a configuration file

If you prefere to centralize your configuration, you could call the helper method without parameter and specify the key ga_tracking_code in the config file

In your Layout (layout/default.html)

<%= ga_tracking_snippet %>

In your config file (config.yaml)

ga_tracking_code: UA-XXXXXXX-X

Generated code

<script type="text/javascript">        
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
        _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>
⚠️ **GitHub.com Fallback** ⚠️