piwik_analytics 0.9.1 → 1.0.0

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.
@@ -1,28 +1,38 @@
1
1
  = PiwikAnalytics
2
2
 
3
- This plugin enables support for Piwik in your Rails application. By default
4
- it will output the synchronous piwik tracking code for every page automatically
5
- (given that it is configured correctly). Configuring this plugin is easy:
3
+ **Note:** This Gem has undergone a major rewrite in 1.x compared to 0.9.x.
6
4
 
7
- PiwikAnalytics::Config.id_site = <Site ID>
8
- PiwikAnalytics::Config.url = <Piwik-URL>
5
+ The piwik_analytics gem provides an easy way to include Piwik into your application.
6
+ By default it will output the synchronous piwik tracking code for every page
7
+ (given that it is configured correctly).
9
8
 
10
- Where the <Site ID> the ID of your website inside Piwik. You can look it up
11
- under Settings -> Websites. <Piwik-URL> must be replaced by the URL to your
12
- Piwik installation.
9
+ == Installation
13
10
 
14
- If you want PiwikAnalytics to use the asynchronous tracking code which was
15
- introduced in Piwik 1.1 you can add the folliwing line:
11
+ Add `piwik_analytics` to Gemfile:
16
12
 
17
- PiwikAnalytics::Config.use_async = true
13
+ gem 'piwik_analytics', '~> 1.0.0'
18
14
 
19
- If you want to disable the tracking code for particular controllers simply add
20
- the following line to those controllers:
15
+ Run the generator:
21
16
 
22
- skip_after_filter :add_piwik_analytics_tracking
17
+ rails g piwik_analytics:install
18
+
19
+ This will install a piwik.yml configuration file into the `config` directory
20
+ of your application.
21
+
22
+
23
+ == Configuration
24
+
25
+ Open up `config/piwik.yml` and edit the settings. Each setting is described in
26
+ the config file itself.
27
+
28
+ == Usage
29
+
30
+ Inside your `application.html.erb` (or haml, slim) simply add
31
+
32
+ <%= piwik_tracking_tag %>
23
33
 
24
34
  Enjoy :)
25
35
 
26
36
 
27
37
  = Licence
28
- Copyright(c) 2010-2011 Fabian Becker, released under MIT licence.
38
+ Copyright(c) 2010-2012 Fabian Becker, released under MIT licence.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
3
+ require 'rdoc/task'
4
4
 
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
@@ -20,4 +20,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
20
20
  rdoc.options << '--line-numbers' << '--inline-source'
21
21
  rdoc.rdoc_files.include('README')
22
22
  rdoc.rdoc_files.include('lib/**/*.rb')
23
- end
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/lib/piwik_analytics"
@@ -0,0 +1,7 @@
1
+ module PiwikAnalytics
2
+ module Generators
3
+ class Base < Rails::Generators::NamedBase
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'piwik_analytics', generator_name, 'templates'))
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module PiwikAnalytics
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def self.source_root
5
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
6
+ end
7
+ def copy_config_file
8
+ template 'config/piwik.yml'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ # Configuration:
2
+ #
3
+ # disabled
4
+ # false if tracking tag should be shown
5
+ # use_async
6
+ # Set to true if you want to use asynchronous tracking
7
+ # url
8
+ # The url of your piwik instance (e.g. localhost/piwik/
9
+ # id_site
10
+ # The id of your website inside Piwik
11
+ #
12
+ production:
13
+ piwik:
14
+ id_site: 1
15
+ url: localhost
16
+ use_async: false
17
+ disabled: false
18
+
19
+ development:
20
+ piwik:
21
+ id_site: 1
22
+ url: localhost
23
+ disabled: true
24
+ use_async: false
25
+ hostname: localhost
26
+
27
+ test:
28
+ piwik:
29
+ id_site: 1
30
+ url: localhost
31
+ disabled: true
32
+ use_async: false
33
+ hostname: localhost
@@ -1,81 +1,13 @@
1
- # PiwikAnalytics
2
- require 'active_support'
1
+ require File.join(File.dirname(__FILE__), 'piwik_analytics', 'configuration')
3
2
 
4
3
  module PiwikAnalytics
5
- module PiwikAnalyticsMixin
6
- def piwik_tracking_js
7
- if Config.use_async
8
- <<-code
9
- <!-- Piwik -->
10
- <script type="text/javascript">
11
- var _paq = _paq || [];
12
- (function(){
13
- var u=(("https:" == document.location.protocol) ? "https://#{Config.url}" : "http://#{Config.url}");
14
- _paq.push(['setSiteId', #{Config.id_site}]);
15
- _paq.push(['setTrackerUrl', u+'piwik.php']);
16
- _paq.push(['trackPageView']);
17
- var d=document,
18
- g=d.createElement('script'),
19
- s=d.getElementsByTagName('script')[0];
20
- g.type='text/javascript';
21
- g.defer=true;
22
- g.async=true;
23
- g.src=u+'piwik.js';
24
- s.parentNode.insertBefore(g,s);
25
- })();
26
- </script>
27
- <!-- End Piwik Tag -->
28
- code
29
- else
30
- <<-code
31
- <!-- Piwik -->
32
- <script type="text/javascript">
33
- var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{Config.url}" : "http://#{Config.url}");
34
- document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
35
- </script><script type="text/javascript">
36
- try {
37
- var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{Config.id_site});
38
- piwikTracker.trackPageView();
39
- piwikTracker.enableLinkTracking();
40
- } catch( err ) {}
41
- </script>
42
- <!-- End Piwik Tag -->
43
- code
44
- end
45
- end
46
-
47
- def add_piwik_analytics_tracking
48
- if Config.enabled? request.format
49
- if Config.use_async
50
- self.response.body= response.body.sub /<\/[hH][eE][aA][dD]>/, "#{piwik_tracking_js}</head>" if response.body.respond_to?(:sub!)
51
- else
52
- self.response.body= response.body.sub! /<\/[bB][oO][dD][yY]>/, "#{piwik_tracking_js}</body>" if response.body.respond_to?(:sub!)
53
- end
54
- end
55
- end
56
- end
57
-
58
- class PiwikAnalyticsConfigurationError < StandardError; end
59
-
60
- class Config
61
- @@id_site = nil
62
- cattr_accessor :id_site
63
-
64
- @@url = nil
65
- cattr_accessor :url
66
-
67
- @@use_async = false
68
- cattr_accessor :use_async
69
-
70
- @@environments = ["production"]
71
- cattr_accessor :environments
4
+ require 'piwik_analytics/railtie' if defined?(Rails)
72
5
 
73
- @@formats = [:html, :all]
74
- cattr_accessor :formats
6
+ class <<self
7
+ attr_writer :configuration
75
8
 
76
- def self.enabled?(format)
77
- raise PiwikAnalytics::PiwikAnalsyticsConfigurationError if id_site.blank? || url.blank?
78
- environments.include?(Rails.env) && formats.include?(format.to_sym)
9
+ def configuration
10
+ @configuration ||= PiwikAnalytics::Configuration.new
79
11
  end
80
12
  end
81
13
  end
@@ -0,0 +1,74 @@
1
+
2
+ module PiwikAnalytics
3
+ class Configuration
4
+
5
+ #
6
+ # The url of the Piwik instance
7
+ # Defaults to localhost
8
+ #
9
+ def url
10
+ @url ||= (user_configuration_from_key('url') || 'localhost')
11
+ end
12
+
13
+ #
14
+ # The ID of the tracked website
15
+ # Defaults to 1
16
+ #
17
+ def id_site
18
+ @id_size ||= (user_configuration_from_key('id_size') || 1)
19
+ end
20
+
21
+ #
22
+ # Whether or not to use the async tracking
23
+ # Defaults to false
24
+ #
25
+ def use_async?
26
+ @use_async ||= (user_configuration_from_key('use_async') || false)
27
+ end
28
+
29
+ #
30
+ # Whether or not to disable Piwik.
31
+ # Defaults to false.
32
+ #
33
+ def disabled?
34
+ @disabled ||= (user_configuration_from_key('disabled') || false)
35
+ end
36
+
37
+ private
38
+
39
+ #
40
+ # return a specific key from the user configuration in config/piwik.yml
41
+ #
42
+ # ==== Returns
43
+ #
44
+ # Mixed:: requested_key or nil
45
+ #
46
+ def user_configuration_from_key( *keys )
47
+ keys.inject(user_configuration) do |hash, key|
48
+ hash[key] if hash
49
+ end
50
+ end
51
+
52
+ #
53
+ # Memoized hash of configuration options for the current Rails environment
54
+ # as specified in config/piwik.yml
55
+ #
56
+ # ==== Returns
57
+ #
58
+ # Hash:: configuration options for current environment
59
+ #
60
+ def user_configuration
61
+ @user_configuration ||= begin
62
+ path = File.join(::Rails.root, 'config', 'piwik.yml')
63
+ if File.exist?(path)
64
+ File.open(path) do |file|
65
+ processed = ERB.new(file.read).result
66
+ YAML.load(processed)[::Rails.env]['piwik']
67
+ end
68
+ else
69
+ {}
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,49 @@
1
+ module PiwikAnalytics
2
+ module Helpers
3
+ def piwik_tracking_tag
4
+ config = PiwikAnalytics.configuration
5
+ return if config.disabled?
6
+
7
+ if config.use_async?
8
+ tag = <<-CODE
9
+ <!-- Piwik -->
10
+ <script type="text/javascript">
11
+ var _paq = _paq || [];
12
+ (function(){
13
+ var u=(("https:" == document.location.protocol) ? "https://#{config.url}" : "http://#{config.url}");
14
+ _paq.push(['setSiteId', #{config.id_site}]);
15
+ _paq.push(['setTrackerUrl', u+'piwik.php']);
16
+ _paq.push(['trackPageView']);
17
+ var d=document,
18
+ g=d.createElement('script'),
19
+ s=d.getElementsByTagName('script')[0];
20
+ g.type='text/javascript';
21
+ g.defer=true;
22
+ g.async=true;
23
+ g.src=u+'piwik.js';
24
+ s.parentNode.insertBefore(g,s);
25
+ })();
26
+ </script>
27
+ <!-- End Piwik Tag -->
28
+ CODE
29
+ tag.html_safe
30
+ else
31
+ tag = <<-CODE
32
+ <!-- Piwik -->
33
+ <script type="text/javascript">
34
+ var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{config.url}" : "http://#{config.url}");
35
+ document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
36
+ </script><script type="text/javascript">
37
+ try {
38
+ var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{config.id_site});
39
+ piwikTracker.trackPageView();
40
+ piwikTracker.enableLinkTracking();
41
+ } catch( err ) {}
42
+ </script>
43
+ <!-- End Piwik Tag -->
44
+ CODE
45
+ tag.html_safe
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1 @@
1
+ ActionView::Base.send :include, PiwikAnalytics::Helpers
@@ -0,0 +1,18 @@
1
+ # PiwikAnalytics
2
+ require 'active_support'
3
+
4
+ module PiwikAnalytics
5
+
6
+ class Railtie < Rails::Railtie
7
+ railtie_name :piwik_analytics
8
+
9
+ generators do
10
+ load "generators/piwik_analytics.rb"
11
+ end
12
+
13
+ initializer "piwik_analytics.init", :before=> :load_config_initializers do
14
+ require "piwik_analytics/helpers"
15
+ load "piwik_analytics/init.rb"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = 'piwik_analytics'
3
+ s.version = '1.0.0'
4
+ s.date = Time.now.strftime "%Y-%m-%d"
5
+ s.author = 'Fabian Becker'
6
+ s.email = 'halfdan@xnorfz.de'
7
+ s.homepage = 'https://github.com/halfdan/piwik_analytics/'
8
+ s.summary = "[Rails] Easily include Piwik tracking in your Rails application."
9
+
10
+ s.description = <<-DESC
11
+ The piwik_analytics gem provides an easy way to include Piwik into your application.
12
+ By default it will output the synchronous piwik tracking code for every page
13
+ (given that it is configured correctly).
14
+ DESC
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency 'rails', '>= 3.0.0'
21
+ s.add_dependency 'actionpack'
22
+ s.add_dependency 'activesupport'
23
+ end
24
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piwik_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,27 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2010-12-30 00:00:00.000000000 +01:00
13
- default_executable:
12
+ date: 2012-10-10 00:00:00.000000000 Z
14
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
15
30
  - !ruby/object:Gem::Dependency
16
31
  name: actionpack
17
- requirement: &74842040 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
18
33
  none: false
19
34
  requirements:
20
35
  - - ! '>='
@@ -22,10 +37,15 @@ dependencies:
22
37
  version: '0'
23
38
  type: :runtime
24
39
  prerelease: false
25
- version_requirements: *74842040
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
26
46
  - !ruby/object:Gem::Dependency
27
47
  name: activesupport
28
- requirement: &74841820 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
29
49
  none: false
30
50
  requirements:
31
51
  - - ! '>='
@@ -33,15 +53,15 @@ dependencies:
33
53
  version: '0'
34
54
  type: :runtime
35
55
  prerelease: false
36
- version_requirements: *74841820
37
- description: ! 'By default this gem will output piwik analytics code forevery page
38
- automatically, if it''s configured correctly.This is done by adding:
39
-
40
- PiwikAnalytics.IdSite = 1
41
-
42
- PiwikAnalytics.URL = <Piwik-URL>
43
-
44
- '
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! " The piwik_analytics gem provides an easy way to include Piwik
63
+ into your application.\n By default it will output the synchronous piwik tracking
64
+ code for every page\n (given that it is configured correctly). \n"
45
65
  email: halfdan@xnorfz.de
46
66
  executables: []
47
67
  extensions: []
@@ -50,10 +70,18 @@ files:
50
70
  - MIT-LICENSE
51
71
  - README.rdoc
52
72
  - Rakefile
73
+ - init.rb
74
+ - lib/generators/piwik_analytics.rb
75
+ - lib/generators/piwik_analytics/install/install_generator.rb
76
+ - lib/generators/piwik_analytics/install/templates/config/piwik.yml
77
+ - lib/piwik_analytics.rb
78
+ - lib/piwik_analytics/configuration.rb
79
+ - lib/piwik_analytics/helpers.rb
80
+ - lib/piwik_analytics/init.rb
81
+ - lib/piwik_analytics/railtie.rb
82
+ - piwik_analytics.gemspec
53
83
  - test/piwik_analytics_test.rb
54
84
  - test/test_helper.rb
55
- - lib/piwik_analytics.rb
56
- has_rdoc: true
57
85
  homepage: https://github.com/halfdan/piwik_analytics/
58
86
  licenses: []
59
87
  post_install_message:
@@ -74,8 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
102
  version: '0'
75
103
  requirements: []
76
104
  rubyforge_project:
77
- rubygems_version: 1.6.2
105
+ rubygems_version: 1.8.24
78
106
  signing_key:
79
107
  specification_version: 3
80
- summary: ! '[Rails] Easily enable Google Analytics support in your Rails application.'
108
+ summary: ! '[Rails] Easily include Piwik tracking in your Rails application.'
81
109
  test_files: []