Class: Rubaidh::GoogleAnalytics

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

Constant Summary collapse

@@tracker_id =

Specify the Google Analytics ID for this web site. This can be found as the value of _uacct in the Javascript excerpt

nil
@@domain_name =

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.

nil
@@legacy_mode =

Specify whether the legacy Google Analytics code should be used.

false
@@analytics_url =

I can’t see why you’d want to do this, but you can always change the analytics URL. This is only applicable in legacy mode.

'http://www.google-analytics.com/urchin.js'
@@analytics_ssl_url =

I can’t see why you’d want to do this, but you can always change the analytics URL (ssl version). This is only applicable in legacy mode.

'https://ssl.google-analytics.com/urchin.js'
@@environments =

The environments in which to enable the Google Analytics code. Defaults to ‘production’ only.

['production']
@@formats =

The formats for which to add. Defaults to :html only.

[:html]
@@defer_load =

Set this to true if you want to load the Analytics javascript at the bottom of each page rather than at the top. This may result in faster page render times, but may break link_to_tracked functionality.

false
@@local_javascript =

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.

false

Class 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:



76
77
78
79
# File 'lib/rubaidh/google_analytics.rb', line 76

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubaidh/google_analytics.rb', line 81

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}\");"
  
  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[//><!--
  var pageTracker = _gat._getTracker('#{tracker_id}');
  #{extra_code}
  pageTracker._initData();
  pageTracker._trackPageview();
  //--><!]]>
  </script>
  HTML
end

.legacy_analytics_js_url(ssl = false) ⇒ Object

Generate the correct URL for the legacy Analytics JS file



129
130
131
132
133
134
135
# File 'lib/rubaidh/google_analytics.rb', line 129

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

Run the legacy version of the Google Analytics code.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubaidh/google_analytics.rb', line 113

def self.legacy_google_analytics_code(ssl = false)
  extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
  url = legacy_analytics_js_url(ssl)

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