require 'split/helper' module Split module Analytics def tracking_code(options = {}) # needs more options: http://code.google.com/apis/analytics/docs/gaJS/gaJSApi.html account = options.delete(:account) tracker_url = options.delete(:tracker_url) ssl_tracker_url = options.delete(:ssl_tracker_url) tracker_methods = options.delete(:tracker_methods) tracker_url = 'http://' + (tracker_url || 'www.google-analytics.com/ga.js') ssl_tracker_url = 'https://' + (ssl_tracker_url || 'ssl.google-analytics.com/ga.js') code = <<-EOF EOF code = raw(code) if defined?(raw) code end def custom_variables return nil if ab_user.nil? arr = [] ab_user.active_experiments.each_with_index do |h, i| arr << "_gaq.push([\"_setCustomVar\", #{i + 1}, \"#{h[0]}\", \"#{h[1]}\", 1]);" end arr.reverse[0..4].reverse.join("\n") end private def insert_tracker_methods(tracker_methods) return nil if tracker_methods.nil? arr = [] tracker_methods.each do |k, v| if v.class == String && v.empty? # No argument tracker method arr << "_gaq.push([\"" + "_" + "#{k}\"]);" else case v when String # String argument tracker method arr << "_gaq.push([\"" + '_' + "#{k}\", \"#{v}\"]);" when TrueClass # Boolean argument tracker method arr << "_gaq.push([\"" + '_' + "#{k}\", #{v}]);" when FalseClass # Boolean argument tracker method arr << "_gaq.push([\"" + '_' + "#{k}\", #{v}]);" when Array # Array argument tracker method values = v.map { |value| "\"#{value}\"" }.join(', ') arr << "_gaq.push([\"" + '_' + "#{k}\", #{values}]);" end end end arr.join("\n") end end end module Split::Helper include Split::Analytics end if defined?(Rails) class ActionController::Base ActionController::Base.send :include, Split::Analytics ActionController::Base.helper Split::Analytics end end