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]

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:



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

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubaidh/google_analytics.rb', line 61

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

.legacy_google_analytics_code(ssl = false) ⇒ Object

Run the legacy version of the Google Analytics code.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubaidh/google_analytics.rb', line 83

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

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