markcatley-google_analytics 1.0.20080715 → 1.0.20080717

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +37 -0
  2. data/lib/rubaidh/google_analytics.rb +35 -3
  3. metadata +1 -1
data/README CHANGED
@@ -55,3 +55,40 @@ Tracked links respect the legacy_mode flag.
55
55
 
56
56
  Note: Link-tracking works by inserting onclick() code in the HTML. Because of this, it will overwrite
57
57
  any onclick that you insert in the html_options hash.
58
+
59
+ USING LOCAL COPIES OF THE ANALYTICS JAVASCRIPT FILES
60
+
61
+ Under certain circumstances you might find it valuable to serve a copy of the
62
+ Analytics JavaScript directly from your server to your visitors, and not
63
+ directly from Google. If your visitors are geograhically very far from Google,
64
+ or if they have low quality international bandwidth, the loading time for the
65
+ Analytics JS might kill the user experience and force you to remove the valuable
66
+ tracking code from your site.
67
+
68
+ This plugin now supports local copies of the legacy and new Analytics JavaScript
69
+ files, updated via a rake task and served courtesy of the Rails AssetTagHelper
70
+ methods. So even if you use asset hosts, the JS will be served from the correct
71
+ source and under the correct protocol (HTTP/HTTPS).
72
+
73
+ To enable cached copies and the following to your initialization code:
74
+
75
+ Rubaidh::GoogleAnalytics.local_javascript = true
76
+
77
+ Use the following rake task to update the local copy of the JavaScript file:
78
+
79
+ rake google_analytics:updates
80
+
81
+ To keep the file updated you can add the following to your Capistrano
82
+ configuration:
83
+
84
+ after "deploy:symlink", "deploy:google_analytics"
85
+
86
+ namespace :deploy do
87
+ desc "Update local Google Analytics files"
88
+ task :google_analytics, :role => :web do
89
+ run "cd #{current_path} && rake google_analytics:update RAILS_ENV=#{ENV['RAILS_ENV']}"
90
+ end
91
+ end
92
+
93
+ The above Capistrano recipe will almost certainly need some adjustments based
94
+ on how you run your deployments, but you should get the idea.
@@ -63,6 +63,14 @@ module Rubaidh # :nodoc:
63
63
  @@defer_load = false
64
64
  cattr_accessor :defer_load
65
65
 
66
+ # Set this to true to use a local copy of the ga.js (or urchin.js) file.
67
+ # This gives you the added benefit of serving the JS directly from your
68
+ # server, which in case of a big geographical difference between your server
69
+ # and Google's can speed things up for your visitors. Use the
70
+ # 'google_analytics:update' rake task to update the local JS copies.
71
+ @@local_javascript = false
72
+ cattr_accessor :local_javascript
73
+
66
74
  # Return true if the Google Analytics system is enabled and configured
67
75
  # correctly for the specified format
68
76
  def self.enabled?(format)
@@ -74,12 +82,22 @@ module Rubaidh # :nodoc:
74
82
  return legacy_google_analytics_code(ssl) if legacy_mode
75
83
 
76
84
  extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");"
77
-
78
- code = <<-HTML
85
+
86
+ code = if local_javascript
87
+ <<-HTML
88
+ <script src="#{LocalAssetTagHelper.new.javascript_path( 'ga.js' )}" type="text/javascript">
89
+ </script>
90
+ HTML
91
+ else
92
+ <<-HTML
79
93
  <script type="text/javascript">
80
94
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
81
95
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
82
96
  </script>
97
+ HTML
98
+ end
99
+
100
+ code << <<-HTML
83
101
  <script type="text/javascript">
84
102
  <!--//--><![CDATA[//><!--
85
103
  var pageTracker = _gat._getTracker('#{tracker_id}');
@@ -94,7 +112,7 @@ module Rubaidh # :nodoc:
94
112
  # Run the legacy version of the Google Analytics code.
95
113
  def self.legacy_google_analytics_code(ssl = false)
96
114
  extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
97
- url = ssl ? analytics_ssl_url : analytics_url
115
+ url = legacy_analytics_js_url(ssl)
98
116
 
99
117
  code = <<-HTML
100
118
  <script src="#{url}" type="text/javascript">
@@ -106,5 +124,19 @@ module Rubaidh # :nodoc:
106
124
  </script>
107
125
  HTML
108
126
  end
127
+
128
+ # Generate the correct URL for the legacy Analytics JS file
129
+ def self.legacy_analytics_js_url(ssl = false)
130
+ if local_javascript
131
+ LocalAssetTagHelper.new.javascript_path( 'urchin.js' )
132
+ else
133
+ ssl ? analytics_ssl_url : analytics_url
134
+ end
135
+ end
136
+ end
137
+
138
+ class LocalAssetTagHelper
139
+ # For helping with local javascripts
140
+ include ActionView::Helpers::AssetTagHelper
109
141
  end
110
142
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markcatley-google_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.20080715
4
+ version: 1.0.20080717
5
5
  platform: ruby
6
6
  authors:
7
7
  - Graeme Mathieson