Changeset 1336
- Timestamp:
- 08/09/08 11:21:33 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/vendor/plugins/google_analytics/init.rb
r1223 r1336 4 4 ActionController::Base.send :after_filter, :add_google_analytics_code 5 5 ActionView::Base.send :include, Rubaidh::GoogleAnalyticsViewHelper 6 7 ActionController::AbstractRequest.send :include, Rubaidh::GoogleAnalyticsAccountSupport trunk/vendor/plugins/google_analytics/lib/rubaidh/google_analytics.rb
r1223 r1336 2 2 module GoogleAnalyticsMixin 3 3 def google_analytics_code 4 GoogleAnalytics.google_analytics_code(request .ssl?) if GoogleAnalytics.enabled?(request.format)4 GoogleAnalytics.google_analytics_code(request) if GoogleAnalytics.enabled?(request.format) 5 5 end 6 6 7 7 # An after_filter to automatically add the analytics code. 8 # Add the code at the top of the page to support calls to _trackPageView 8 # Add the code at the top of the page to support calls to _trackPageView 9 9 # (see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006) 10 10 def add_google_analytics_code 11 11 response.body.sub! '</body>', "#{google_analytics_code}</body>" if response.body.respond_to?(:sub!) 12 12 end 13 end 14 15 module GoogleAnalyticsAccountSupport 16 17 @google_analytics_account = nil 18 attr_accessor :google_analytics_account 19 13 20 end 14 21 … … 18 25 # Specify the Google Analytics ID for this web site. This can be found 19 26 # as the value of +_uacct+ in the Javascript excerpt 20 @ @tracker_id = nil21 cattr_accessor :tracker_id27 @tracker_id = nil 28 attr_accessor :tracker_id 22 29 23 30 # Specify a different domain name from the default. You'll want to use … … 25 32 # one report. See the Google Analytics documentation for more 26 33 # information. 27 @@domain_name = nil 28 cattr_accessor :domain_name 34 @domain_name = nil 35 attr_accessor :domain_name 36 37 def initialize(tracker_id, domain_name = nil) 38 @tracker_id = tracker_id 39 @domain_name = domain_name 40 end 29 41 30 42 # Specify whether the legacy Google Analytics code should be used. 31 43 @@legacy_mode = false 32 44 cattr_accessor :legacy_mode 33 45 34 46 # I can't see why you'd want to do this, but you can always change the 35 47 # analytics URL. This is only applicable in legacy mode. … … 46 58 @@environments = ['production'] 47 59 cattr_accessor :environments 48 60 49 61 # The formats for which to add. Defaults 50 62 # to :html only. … … 52 64 cattr_accessor :formats 53 65 66 @@default_account = nil 67 cattr_accessor :default_account 68 69 @@request_accounts = [] 70 cattr_accessor :request_accounts 71 72 def self.add_request_account(account) 73 @@request_accounts << account 74 end 75 54 76 # Return true if the Google Analytics system is enabled and configured 55 77 # correctly for the specified format 56 78 def self.enabled?(format) 57 raise Rubaidh::GoogleAnalyticsConfigurationError if tracker_id.blank? || analytics_url.blank?79 raise Rubaidh::GoogleAnalyticsConfigurationError if default_account.blank? || analytics_url.blank? 58 80 environments.include?(RAILS_ENV) && formats.include?(format.to_sym) 59 81 end 60 61 def self.google_analytics_code(ssl = false)62 return legacy_google_analytics_code(ssl) if legacy_mode63 82 83 def self.accounts(request) 84 [ default_account, request.google_analytics_account ].compact 85 end 86 87 def self.collect_accounts_code(request, legacy = false) 88 method = legacy ? :legacy_google_analytics_code : :google_analytics_code 89 90 accounts(request).collect do |account| 91 account.send method, request.ssl? 92 end.join("\n") 93 end 94 95 def self.google_analytics_code(request) 96 collect_accounts_code(request, legacy_mode) 97 end 98 99 def google_analytics_code(ssl = false) 64 100 extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");" 65 101 … … 81 117 82 118 # Run the legacy version of the Google Analytics code. 83 def self.legacy_google_analytics_code(ssl = false) 119 def self.legacy_google_analytics_code(request) 120 collect_accounts_code(request, false) 121 end 122 123 def legacy_google_analytics_code(ssl = false) 84 124 extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";" 85 125 url = ssl ? analytics_ssl_url : analytics_url
