Class: Rubaidh::GoogleAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/rubaidh/google_analytics.rb

Overview

The core functionality to connect a Rails application to a Google Analytics installation.

Constant Summary collapse

@@tracker_id =
nil
@@domain_name =
nil
@@legacy_mode =
false
@@analytics_url =
'http://www.google-analytics.com/urchin.js'
@@analytics_ssl_url =
'https://ssl.google-analytics.com/urchin.js'
@@environments =
['production']
@@formats =
[:html, :all]
@@defer_load =
true
@@local_javascript =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enabled?(format) ⇒ Boolean

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

Returns:

  • (Boolean)

Raises:



144
145
146
147
# File 'lib/rubaidh/google_analytics.rb', line 144

def self.enabled?(format)
  raise Rubaidh::GoogleAnalyticsConfigurationError if tracker_id.blank? || analytics_url.blank?
  environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
end

.google_analytics_code(ssl = false) ⇒ Object

Construct the javascript code to be inserted on the calling page. The ssl parameter can be used to force the SSL version of the code in legacy mode only.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rubaidh/google_analytics.rb', line 151

def self.google_analytics_code(ssl = false)
  return legacy_google_analytics_code(ssl) if legacy_mode

  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( 'ga.js' )}" type="text/javascript">
    </script>
    HTML
  else
    <<-HTML
  <script type="text/javascript">
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  </script>
    HTML
  end
  
  code << <<-HTML
  <script type="text/javascript">
  <!--//--><![CDATA[//><!--
  try {
  var pageTracker = _gat._getTracker('#{request_tracker_id}');
  #{extra_code}
  pageTracker._initData();
  pageTracker._trackPageview(#{request_tracked_path});
  } catch(err) {}
  //--><!]]>
  </script>
  HTML
end

.legacy_analytics_js_url(ssl = false) ⇒ Object

Generate the correct URL for the legacy Analytics JS file



211
212
213
214
215
216
217
# File 'lib/rubaidh/google_analytics.rb', line 211

def self.legacy_analytics_js_url(ssl = false)
  if local_javascript
    LocalAssetTagHelper.new.javascript_path( 'urchin.js' )
  else
    ssl ? analytics_ssl_url : analytics_url
  end
end

.legacy_google_analytics_code(ssl = false) ⇒ Object

Construct the legacy version of the Google Analytics code. The ssl parameter specifies whether or not to return the SSL version of the code.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rubaidh/google_analytics.rb', line 190

def self.legacy_google_analytics_code(ssl = false)
  extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
  if !override_domain_name.blank?
    extra_code = "_udn = \"#{override_domain_name}\";"
    self.override_domain_name = nil
  end

  url = legacy_analytics_js_url(ssl)

  code = <<-HTML
  <script src="#{url}" type="text/javascript">
  </script>
  <script type="text/javascript">
  _uacct = "#{request_tracker_id}";
  #{extra_code}
  urchinTracker(#{request_tracked_path});
  </script>
  HTML
end

.request_tracked_pathObject

Determine the path to report for this request



227
228
229
230
231
# File 'lib/rubaidh/google_analytics.rb', line 227

def self.request_tracked_path
  use_tracked_path = override_trackpageview.blank? ? '' : "'#{override_trackpageview}'"
  self.override_trackpageview = nil
  use_tracked_path
end

.request_tracker_idObject

Determine the tracker ID for this request



220
221
222
223
224
# File 'lib/rubaidh/google_analytics.rb', line 220

def self.request_tracker_id
  use_tracker_id = override_tracker_id.blank? ? tracker_id : override_tracker_id
  self.override_tracker_id = nil
  use_tracker_id
end

Instance Method Details

#analytics_ssl_urlObject

:singleton-method: The URL that analytics information is sent to when using SSL. This defaults to the standard Google Analytics URL, and you’re unlikely to need to change it. This has no effect unless you’re in legacy mode.



73
# File 'lib/rubaidh/google_analytics.rb', line 73

cattr_accessor :analytics_ssl_url

#analytics_urlObject

:singleton-method: The URL that analytics information is sent to. This defaults to the standard Google Analytics URL, and you’re unlikely to need to change it. This has no effect unless you’re in legacy mode.



65
# File 'lib/rubaidh/google_analytics.rb', line 65

cattr_accessor :analytics_url

#defer_loadObject

:singleton-method: 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, but that will break the linking functions in Rubaidh::GoogleAnalyticsViewHelper.



98
# File 'lib/rubaidh/google_analytics.rb', line 98

cattr_accessor :defer_load

#domain_nameObject

:singleton-method: Specify a different domain name from the default. You’ll want to use this if you have several subdomains that you want to combine into one report. See the Google Analytics documentation for more information.



50
# File 'lib/rubaidh/google_analytics.rb', line 50

cattr_accessor :domain_name

#environmentsObject

:singleton-method: The environments in which to enable the Google Analytics code. Defaults to ‘production’ only. Supply an array of environment names to change this.



80
# File 'lib/rubaidh/google_analytics.rb', line 80

cattr_accessor :environments

#formatsObject

:singleton-method: The request formats where tracking code should be added. Defaults to [:html, :all]. The entry for :all is necessary to make Google recognize that tracking is installed on a site; it is not the same as responding to all requests. Supply an array of formats to change this.



89
# File 'lib/rubaidh/google_analytics.rb', line 89

cattr_accessor :formats

#legacy_modeObject

:singleton-method: Specify whether the legacy Google Analytics code should be used. By default, the new Google Analytics code is used.



57
# File 'lib/rubaidh/google_analytics.rb', line 57

cattr_accessor :legacy_mode

#local_javascriptObject

:singleton-method: Set this to true to use a local copy of the ga.js (or urchin.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 Google’s can speed things up for your visitors. Use the ‘google_analytics:update’ rake task to update the local JS copies.



108
# File 'lib/rubaidh/google_analytics.rb', line 108

cattr_accessor :local_javascript

#override_domain_nameObject

:singleton-method: 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
   Rubaidh::GoogleAnalytics.override_domain_name  = 'foo.com'
end


119
# File 'lib/rubaidh/google_analytics.rb', line 119

cattr_accessor :override_domain_name

#override_tracker_idObject

:singleton-method: Set this to override the initialized tracker ID 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_tracker_id
 def override_tracker_id
   Rubaidh::GoogleAnalytics.override_tracker_id  = 'UA-123456-7'
end


130
# File 'lib/rubaidh/google_analytics.rb', line 130

cattr_accessor :override_tracker_id

#override_trackpageviewObject

:singleton-method: Set this to override the automatically generated path to the page in the Google Analytics reports for a single render. Typically you’d set this up on an action-by-action basis:

def show
  Rubaidh::GoogleAnalytics.override_trackpageview = "path_to_report"
  ...


140
# File 'lib/rubaidh/google_analytics.rb', line 140

cattr_accessor :override_trackpageview

#tracker_idObject

:singleton-method: Specify the Google Analytics ID for this web site. This can be found as the value of _getTracker if you are using the new (ga.js) tracking code, or the value of _uacct if you are using the old (urchin.js) tracking code.



41
# File 'lib/rubaidh/google_analytics.rb', line 41

cattr_accessor :tracker_id