Sha256: 3273b43dfc72e884c5cf3653e5f2cfff05d8cd1e21cd2acb4d917506c42c01a5

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module Analytics
  module Rails
    module ActionView
      module Base

        def google_analytics_include_tag(*args)
          options = args.extract_options!
          if ::Rails.env.production?
            [:variables, :events].each do |name|
              if options.has_key? name
                options[name].map! do |values|
                  values.map(&:inspect).join(', ').gsub('"',"'")
                end
              else
                options[name] = []
              end
            end
            variables = options[:variables].map do |values|
              "_gaq.push(['_setCustomVar', #{values}]);"
            end
            events = options[:events].map do |values|
              "ga('send', 'event', #{values});"
            end
            script = <<-SCRIPT
              var _gaq = _gaq || [];
              _gaq.push(['_setAccount', '#{args.first}']);
              #{variables.join("\n")}
              _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);
              })();
              #{events.join("\n")}
            SCRIPT
            content_tag :script, script.html_safe, type: 'text/javascript'
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
analytics-rails-0.2.1 lib/analytics/rails/action_view/base.rb
analytics-rails-0.2.0 lib/analytics/rails/action_view/base.rb