Browse files

Change Rakefile

Extract tracking tag to helper method
Delete rails/init
  • Loading branch information...
1 parent 1060ca8 commit 659241da44543010b718197b5b5c11d037d4f9f4 @halfdan halfdan committed Oct 10, 2012
Showing with 55 additions and 64 deletions.
  1. +2 −2 Rakefile
  2. +1 −1 init.rb
  3. +10 −0 lib/piwik_analytics.rb
  4. +41 −1 lib/piwik_analytics/helpers.rb
  5. +1 −57 lib/piwik_analytics/railtie.rb
  6. +0 −3 rails/init.rb
View
4 Rakefile
@@ -1,6 +1,6 @@
require 'rake'
require 'rake/testtask'
-require 'rake/rdoctask'
+require 'rdoc/task'
desc 'Default: run unit tests.'
task :default => :test
@@ -20,4 +20,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
+end
View
2 init.rb
@@ -1 +1 @@
-require File.dirname(__FILE__) + "/rails/init"
+require File.dirname(__FILE__) + "/lib/piwik_analytics"
View
10 lib/piwik_analytics.rb
@@ -1,3 +1,13 @@
+require File.join(File.dirname(__FILE__), 'piwik_analytics', 'configuration')
+
module PiwikAnalytics
require 'piwik_analytics/railtie' if defined?(Rails)
+
+ class <<self
+ attr_writer :configuration
+
+ def configuration
+ @configuration ||= PiwikAnalytics::Configuration.new
+ end
+ end
end
View
42 lib/piwik_analytics/helpers.rb
@@ -1,7 +1,47 @@
module PiwikAnalytics
module Helpers
def piwik_tracking_tag
-
+ config = PiwikAnalytics.configuration
+ if config.use_async?
+ tag = <<-CODE
+ <!-- Piwik -->
+ <script type="text/javascript">
+ var _paq = _paq || [];
+ (function(){
+ var u=(("https:" == document.location.protocol) ? "https://#{config.url}" : "http://#{config.url}");
+ _paq.push(['setSiteId', #{config.id_site}]);
+ _paq.push(['setTrackerUrl', u+'piwik.php']);
+ _paq.push(['trackPageView']);
+ var d=document,
+ g=d.createElement('script'),
+ s=d.getElementsByTagName('script')[0];
+ g.type='text/javascript';
+ g.defer=true;
+ g.async=true;
+ g.src=u+'piwik.js';
+ s.parentNode.insertBefore(g,s);
+ })();
+ </script>
+ <!-- End Piwik Tag -->
+ CODE
+ tag.html_safe
+ else
+ tag = <<-CODE
+ <!-- Piwik -->
+ <script type="text/javascript">
+ var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{config.url}" : "http://#{config.url}");
+ document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
+ </script><script type="text/javascript">
+ try {
+ var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{config.id_site});
+ piwikTracker.trackPageView();
+ piwikTracker.enableLinkTracking();
+ } catch( err ) {}
+ </script>
+ <!-- End Piwik Tag -->
+ CODE
+ tag.html_safe
+ end
end
end
end
View
58 lib/piwik_analytics/railtie.rb
@@ -10,65 +10,9 @@ class Railtie < Rails::Railtie
load "generators/piwik_analytics.rb"
end
- initializer "piwik_analytics.init" do
+ initializer "piwik_analytics.init", :before=> :load_config_initializers do
require "piwik_analytics/helpers"
load "piwik_analytics/init.rb"
end
end
-
- module PiwikAnalyticsMixin
- def piwik_tracking_js
- if Config.use_async
- <<-code
- <!-- Piwik -->
- <script type="text/javascript">
- var _paq = _paq || [];
- (function(){
- var u=(("https:" == document.location.protocol) ? "https://#{Config.url}" : "http://#{Config.url}");
- _paq.push(['setSiteId', #{Config.id_site}]);
- _paq.push(['setTrackerUrl', u+'piwik.php']);
- _paq.push(['trackPageView']);
- var d=document,
- g=d.createElement('script'),
- s=d.getElementsByTagName('script')[0];
- g.type='text/javascript';
- g.defer=true;
- g.async=true;
- g.src=u+'piwik.js';
- s.parentNode.insertBefore(g,s);
- })();
- </script>
- <!-- End Piwik Tag -->
- code
- else
- <<-code
- <!-- Piwik -->
- <script type="text/javascript">
- var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{Config.url}" : "http://#{Config.url}");
- document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
- </script><script type="text/javascript">
- try {
- var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{Config.id_site});
- piwikTracker.trackPageView();
- piwikTracker.enableLinkTracking();
- } catch( err ) {}
- </script>
- <!-- End Piwik Tag -->
- code
- end
- end
-
- def add_piwik_analytics_tracking
- if Config.enabled? request.format
- if Config.use_async
- self.response.body= response.body.sub /<\/[hH][eE][aA][dD]>/, "#{piwik_tracking_js}</head>" if response.body.respond_to?(:sub!)
- else
- self.response.body= response.body.sub! /<\/[bB][oO][dD][yY]>/, "#{piwik_tracking_js}</body>" if response.body.respond_to?(:sub!)
- end
- end
- end
- end
-
- class PiwikAnalyticsConfigurationError < StandardError; end
-
end
View
3 rails/init.rb
@@ -1,3 +0,0 @@
-require 'piwik_analytics'
-ActionController::Base.send :include, PiwikAnalytics::PiwikAnalyticsMixin
-ActionController::Base.send :after_filter, :add_piwik_analytics_tracking

0 comments on commit 659241d

Please sign in to comment.