Skip to content

Commit

Permalink
added support for new GA code
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Yu committed May 17, 2008
1 parent 8d87f39 commit 1c029c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
10 changes: 9 additions & 1 deletion README
Expand Up @@ -16,4 +16,12 @@ If you want to disable the code insertion for particular pages, add the followin

skip_after_filter :add_google_analytics_code

Simple. :-)
Simple. :-)

NOTE

This version of the plugin uses the new Google Analytics code (ga.js) by
default. To use the legacy tracking code add the following line to your
`config/environmet.rb`:

Rubaidh::GoogleAnalytics.legacy_mode = true
30 changes: 27 additions & 3 deletions lib/rubaidh/google_analytics.rb
Expand Up @@ -25,14 +25,18 @@ class GoogleAnalytics
# information.
@@domain_name = nil
cattr_accessor :domain_name

# Specify whether the legacy Google Analytics code should be used.
@@legacy_mode = false
cattr_accessor :legacy_mode

# I can't see why you'd want to do this, but you can always change the
# analytics URL.
# analytics URL. This is only applicable in legacy mode.
@@analytics_url = 'http://www.google-analytics.com/urchin.js'
cattr_accessor :analytics_url

# I can't see why you'd want to do this, but you can always change the
# analytics URL (ssl version).
# analytics URL (ssl version). This is only applicable in legacy mode.
@@analytics_ssl_url = 'https://ssl.google-analytics.com/urchin.js'
cattr_accessor :analytics_ssl_url

Expand All @@ -50,6 +54,26 @@ def self.enabled?
end

def self.google_analytics_code(request = nil)
return legacy_google_analytics_code(request) 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">
var pageTracker = _gat._getTracker("#{tracker_id}");
#{extra_code}
pageTracker._initData();
pageTracker._trackPageview();
</script>
HTML
code
end

# Run the legacy version of the Google Analytics code.
def self.legacy_google_analytics_code(request = nil)
extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
url = (not request.blank? and request.ssl?) ? analytics_ssl_url : analytics_url

Expand All @@ -67,4 +91,4 @@ def self.google_analytics_code(request = nil)
code
end
end
end
end

0 comments on commit 1c029c0

Please sign in to comment.