From fe49579ff5788357e7524278761b7aab0a8dd8f3 Mon Sep 17 00:00:00 2001
From: danielgrippi <danielgrippi@gmail.com>
Date: Tue, 1 May 2012 18:01:50 -0700
Subject: [PATCH] =?UTF-8?q?remove=20google-analytics=20middleware=20due=20?=
 =?UTF-8?q?to=20the=20fact=20that=20it=20doesn't=20support=20injection=20o?=
 =?UTF-8?q?f=20custom=20variables=20before=20=5FtrackPageView=20is=20calle?=
 =?UTF-8?q?d=20(this=20results=20in=20custom=20vars=20not=20getting=20trac?=
 =?UTF-8?q?ked)=20i've=20submitted=20an=20issue=20to=20the=20gem.=20=20hop?=
 =?UTF-8?q?efully=20we=20can=20put=20it=20back=20in=20later=E2=80=A6=20for?=
 =?UTF-8?q?=20now=20i've=20copy=20and=20pasted=20its=20output=20into=20a?=
 =?UTF-8?q?=20helper=20method.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Gemfile                                 |  1 -
 Gemfile.lock                            |  2 --
 app/helpers/analytics_helper.rb         | 33 +++++++++++++++++++++++++
 app/helpers/application_helper.rb       |  6 -----
 app/views/layouts/application.html.haml |  2 +-
 app/views/layouts/post.html.haml        |  2 +-
 config/initializers/load_analyitics.rb  |  5 ----
 7 files changed, 35 insertions(+), 16 deletions(-)
 create mode 100644 app/helpers/analytics_helper.rb

diff --git a/Gemfile b/Gemfile
index 10a9394f78..378e6588a5 100644
--- a/Gemfile
+++ b/Gemfile
@@ -37,7 +37,6 @@ gem "rpm_contrib", "~> 2.1.7"
 group :production do # we don't install these on travis to speed up test runs
   gem 'rack-ssl', :require => 'rack/ssl'
   gem 'rack-rewrite', '~> 1.2.1', :require => false
-  gem 'rack-google-analytics', :require => 'rack/google-analytics'
   gem 'rack-piwik', :require => 'rack/piwik'
 end
 
diff --git a/Gemfile.lock b/Gemfile.lock
index 5b61087e8c..e87b148a25 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -321,7 +321,6 @@ GEM
     rack-cors (0.2.4)
       rack
     rack-fiber_pool (0.9.2)
-    rack-google-analytics (0.10.0)
     rack-mobile-detect (0.3.0)
       rack
     rack-mount (0.8.3)
@@ -554,7 +553,6 @@ DEPENDENCIES
   parallel_tests
   pg
   rack-cors (~> 0.2.4)
-  rack-google-analytics
   rack-piwik
   rack-rewrite (~> 1.2.1)
   rack-ssl
diff --git a/app/helpers/analytics_helper.rb b/app/helpers/analytics_helper.rb
new file mode 100644
index 0000000000..cf9b93c0dd
--- /dev/null
+++ b/app/helpers/analytics_helper.rb
@@ -0,0 +1,33 @@
+#   Copyright (c) 2010-2012, Diaspora Inc.  This file is
+#   licensed under the Affero General Public License version 3 or later.  See
+#   the COPYRIGHT file.
+
+module AnalyticsHelper
+  def include_google_analytics
+    return unless google_configured?
+
+    segment = current_user ? current_user.role_name : "unauthenticated"
+    javascript_tag do
+      <<-JS
+          var _gaq = _gaq || [];
+          _gaq.push(['_setAccount', "#{AppConfig[:google_a_site]}"]);
+
+          _gaq.push(['_setCustomVar', 1, 'Role', '#{segment}']);
+          _gaq.push(['_trackPageview']);
+          _gaq.push(['_trackPageLoadTime']);
+
+          (function() {
+            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+          })();
+      JS
+    end
+  end
+
+  private
+
+  def google_configured?
+    AppConfig[:google_a_site].present? && AppConfig[:google_a_site].present?
+  end
+end
\ No newline at end of file
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index e7e6768311..acebf3057b 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -40,12 +40,6 @@ module ApplicationHelper
     javascript_tag("Modernizr.mq('(min-width:0)') ||  document.write(unescape('#{j javascript_include_tag("mbp-respond.min")}'));")
   end
 
-  # This will *only* fire if analytics are configured (don't panic!)
-  def include_advanced_segments
-    segment = current_user ? current_user.role_name : "unauthenticated"
-    javascript_tag("if(window._gaq) { _gaq.push(['_setCustomVar', 1, 'Role', '#{segment}']) }")
-  end
-
   # Require jQuery from CDN if possible, falling back to vendored copy, and require
   # vendored jquery_ujs
   def jquery_include_tag
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml
index c7ad46c087..1e277f6933 100644
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -40,7 +40,7 @@
     = set_current_user_in_javascript
     = translation_missing_warnings
     = current_user_atom_tag
-    = include_advanced_segments
+    = include_google_analyics
 
     = yield(:head)
     = csrf_meta_tag
diff --git a/app/views/layouts/post.html.haml b/app/views/layouts/post.html.haml
index b995d03d5f..c4cdca34aa 100644
--- a/app/views/layouts/post.html.haml
+++ b/app/views/layouts/post.html.haml
@@ -45,7 +45,7 @@
     = set_asset_host
     = translation_missing_warnings
     = current_user_atom_tag
-    = include_advanced_segments
+    = include_google_analytics
 
     - if @post.present?
       %link{:rel => 'alternate', :type => "application/json+oembed", :href => "#{oembed_url(:url => post_url(@post))}"}
diff --git a/config/initializers/load_analyitics.rb b/config/initializers/load_analyitics.rb
index d071392c5c..15ccc71b3c 100644
--- a/config/initializers/load_analyitics.rb
+++ b/config/initializers/load_analyitics.rb
@@ -5,11 +5,6 @@
 
 if Rails.env == 'production'
   Diaspora::Application.configure do
-    if AppConfig[:google_a_site].present?
-      config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
-      config.middleware.use Rack::GoogleAnalytics, :tracker => AppConfig[:google_a_site]
-    end
-
     if AppConfig[:piwik_url].present?
       config.gem 'rack-piwik', :lib => 'rack/piwik'
       config.middleware.use Rack::Piwik, :piwik_url => AppConfig[:piwik_url], :piwik_id => AppConfig[:piwik_id]
-- 
GitLab