Class: JonathanNelson::WoopraAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/jonathannelson/woopra_analytics.rb

Overview

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

   ...

Constant Summary collapse

@@domain_name =
nil
@@analytics_url =
'http://static.woopra.com/js/woopra.js'
@@analytics_ssl_url =
'https://sec1.woopra.com/js/woopra.js'
@@environments =
['production']
@@formats =
[:html]
@@defer_load =
true
@@local_javascript =
false

Class Method Summary collapse

Class Method Details

.enabled?(format) ⇒ Boolean

Return true if the Woopra Analytics system is enabled and configured correctly for the specified format



99
100
101
102
# File 'lib/jonathannelson/woopra_analytics.rb', line 99

def self.enabled?(format)
  raise JonathanNelson::WoopraAnalyticsConfigurationError if analytics_url.blank?
  environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
end

.woopra_analytics_code(ssl = false) ⇒ Object

Construct the javascript code to be inserted on the calling page.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/jonathannelson/woopra_analytics.rb', line 105

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
    <script src="#{LocalAssetTagHelper.new.javascript_path( '/js/woopra.js' )}" type="text/javascript">
    </script>
    HTML
  else
    <<-HTML
  <!-- Woopra Code Start -->  
  <script type="text/javascript">
  var _wh = ((document.location.protocol=="https:") ? "https://sec1.woopra.com" : "http://static.woopra.com");
  document.write(unescape("%3Cscript src='" + _wh + "woopra.com/js/woopra.js' type='text/javascript'%3E%3C/script%3E"));
  </script>
  <!-- Woopra Code End -->
    HTML
  end

end