module JonathanNelson # :nodoc: module WoopraAnalyticsMixin def woopra_analytics_code WoopraAnalytics.woopra_analytics_code(request.ssl?) if WoopraAnalytics.enabled?(request.format) end # An after_filter to automatically add the analytics code. def add_woopra_analytics_code if WoopraAnalytics.defer_load response.body.sub! '
', "#{woopra_analytics_code}" if response.body.respond_to?(:sub!) end end end class WoopraAnalyticsConfigurationError < StandardError; end # The core functionality to connect a Rails application # to a Woopra Analytics installation. # # * domain_name # # Specify a different domain name from the default. See the Woopra Analytics documentation for more # information. # # * analytics_url # # I can't see why you'd want to do this, but you can always change the # Woopra Analytics URL. # # * analytics_ssl_url # # I can't see why you'd want to do this, but you can always change the # Woopra Analytics URL (ssl version). # # * environments # # The environments in which to enable the Woopra Analytics code. Defaults # to 'production' only. Supply an array of environment names to change this. # # * formats # # The formats for which to add. Defaults to +:html+ only. Supply an array # of formats to change this. # # * defer_load # # Set this to true (the default) if you want to load the Analytics javascript at # the bottom of page. Set this to false if you want to load the Analytics # javascript at the top of the page. The page will render faster if you set this to # true. # # * local_javascript # # Set this to true to use a local copy of the woopra.js (or /js/woopra.js) file. # This gives you the added benefit of serving the JS directly from your # server, which in case of a big geographical difference between your server # and Woopra's can speed things up for your visitors. Use the # 'woopra_analytics:update' rake task to update the local JS copies. # # * override_domain_name # # Set this to override the initialized domain name for a single render. Useful # when you're serving to multiple hosts from a single codebase. Typically you'd # set up a before filter in the appropriate controller: # before_filter :override_domain_name # def override_domain_name # JonathanNelson::WoopraAnalytics.override_domain_name = 'foo.com' # end # # ... class WoopraAnalytics @@domain_name = nil cattr_accessor :domain_name @@analytics_url = 'http://static.woopra.com/js/woopra.js' cattr_accessor :analytics_url @@analytics_ssl_url = 'https://sec1.woopra.com/js/woopra.js' cattr_accessor :analytics_ssl_url @@environments = ['production'] cattr_accessor :environments @@formats = [:html] cattr_accessor :formats @@defer_load = true cattr_accessor :defer_load @@local_javascript = false cattr_accessor :local_javascript cattr_accessor :override_domain_name # Return true if the Woopra Analytics system is enabled and configured # correctly for the specified format def self.enabled?(format) raise JonathanNelson::WoopraAnalyticsConfigurationError if analytics_url.blank? environments.include?(RAILS_ENV) && formats.include?(format.to_sym) end # Construct the javascript code to be inserted on the calling page. def self.woopra_analytics_code(ssl = false) extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");" if !override_domain_name.blank? extra_code = "pageTracker._setDomainName(\"#{override_domain_name}\");" self.override_domain_name = nil end code = if local_javascript <<-HTML HTML else <<-HTML HTML end end end class LocalAssetTagHelper # :nodoc: # For helping with local javascripts include ActionView::Helpers::AssetTagHelper end end