Changeset 1336

Show
Ignore:
Timestamp:
08/09/08 11:21:33 (4 months ago)
Author:
alban
Message:

Multiple Google Analytics support. Refs #18

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/vendor/plugins/google_analytics/init.rb

    r1223 r1336  
    44ActionController::Base.send :after_filter, :add_google_analytics_code 
    55ActionView::Base.send :include, Rubaidh::GoogleAnalyticsViewHelper 
     6 
     7ActionController::AbstractRequest.send :include, Rubaidh::GoogleAnalyticsAccountSupport 
  • trunk/vendor/plugins/google_analytics/lib/rubaidh/google_analytics.rb

    r1223 r1336  
    22  module GoogleAnalyticsMixin 
    33    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) 
    55    end 
    6      
     6 
    77    # 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 
    99    #   (see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006) 
    1010    def add_google_analytics_code 
    1111      response.body.sub! '</body>', "#{google_analytics_code}</body>" if response.body.respond_to?(:sub!) 
    1212    end 
     13  end 
     14 
     15  module GoogleAnalyticsAccountSupport 
     16 
     17    @google_analytics_account = nil 
     18    attr_accessor :google_analytics_account 
     19 
    1320  end 
    1421 
     
    1825    # Specify the Google Analytics ID for this web site.  This can be found 
    1926    # as the value of +_uacct+ in the Javascript excerpt 
    20     @@tracker_id = nil 
    21     cattr_accessor :tracker_id 
     27    @tracker_id = nil 
     28    attr_accessor :tracker_id 
    2229 
    2330    # Specify a different domain name from the default.  You'll want to use 
     
    2532    # one report.  See the Google Analytics documentation for more 
    2633    # 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 
    2941 
    3042    # Specify whether the legacy Google Analytics code should be used. 
    3143    @@legacy_mode = false 
    3244    cattr_accessor :legacy_mode 
    33      
     45 
    3446    # I can't see why you'd want to do this, but you can always change the 
    3547    # analytics URL.  This is only applicable in legacy mode. 
     
    4658    @@environments = ['production'] 
    4759    cattr_accessor :environments 
    48      
     60 
    4961    # The formats for which to add.  Defaults 
    5062    # to :html only. 
     
    5264    cattr_accessor :formats 
    5365 
     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 
    5476    # Return true if the Google Analytics system is enabled and configured 
    5577    # correctly for the specified format 
    5678    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? 
    5880      environments.include?(RAILS_ENV) && formats.include?(format.to_sym) 
    5981    end 
    60      
    61     def self.google_analytics_code(ssl = false) 
    62       return legacy_google_analytics_code(ssl) if legacy_mode 
    6382 
     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) 
    64100      extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");" 
    65101 
     
    81117 
    82118    # 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) 
    84124      extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";" 
    85125      url = ssl ? analytics_ssl_url : analytics_url