rack-yandex-metrika 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Ilya Konyukhov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Rack Yandex Metrika
2
+
3
+ Simple Rack middleware to help injecting the Yandex Metrika tracking code in your website.
4
+
5
+ This middleware injects tracking code into the correct place of any request only when the response's `Content-Type` header contains `html` (therefore `text/html` and similar).
6
+
7
+ The project code is based on Rack Google Analytics project.
8
+
9
+ ## Usage
10
+
11
+ #### Gemfile
12
+
13
+ ```ruby
14
+ gem 'rack-yandex-metrika'
15
+ ```
16
+
17
+ #### Sinatra
18
+
19
+ ```ruby
20
+ ## app.rb
21
+ use Rack::YandexMetrika, :counter_id => 00000000
22
+ ```
23
+
24
+ #### Padrino
25
+
26
+ ```ruby
27
+ ## app/app.rb
28
+ use Rack::YandexMetrika, :counter_id => 00000000
29
+ ```
30
+
31
+ #### Rails
32
+
33
+ ```ruby
34
+ ## environment.rb:
35
+ config.middleware.use Rack::YandexMetrika, :counter_id => 00000000
36
+ ```
37
+
38
+ ### Options
39
+
40
+ * `:async` - sets to use asynchronous tracker (default: true)
41
+ * `:webvisor` - sets to use webvisor, ie tool for visitors behavior analysis (default: false)
42
+ * `:clickmap` - sets to collect statistics for ClickMap tool (default: false)
43
+ * `:trackLinks` - sets to collect statistics on using external links (default: false)
44
+ * `:accurateTrackBounce` - sets non-bounce visit time to 15 sec (default: false)
45
+ * `:trackHash` - sets to track hashes in URLs for AJAX sites (default: false)
46
+
47
+ If you are not sure what's best, go with the defaults, and read here if you should opt-out.
48
+
49
+ ## Thread Safety
50
+
51
+ This middleware *should* be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation.
52
+
53
+ ## Note on Patches/Pull Requests
54
+
55
+ * Fork the project.
56
+ * Make your feature addition or bug fix.
57
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
58
+ * Commit, do not mess with rakefile, version, or history.
59
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
60
+ * Send me a pull request. Bonus points for topic branches.
61
+
62
+ ## Copyright
63
+
64
+ Copyright (c) 2013 Ilya Konyukhov. See LICENSE for details.
65
+ With thanks to [Lee Hambley](https://github.com/leehambley) for Rack Google Analytics gem.
@@ -0,0 +1 @@
1
+ require 'rack/yandex-metrika'
@@ -0,0 +1,24 @@
1
+ <script type="text/javascript">
2
+ (function (d, w, c) {
3
+ (w[c] = w[c] || []).push(function() {
4
+ try {
5
+ w.yaCounter<%= @options[:counter_id].to_i %> = new Ya.Metrika({id:<%= @options[:counter_id].to_i %><% if @options[:webvisor] %>,
6
+ webvisor:true<% end %><% if @options[:clickmap] %>,
7
+ clickmap:true<% end %><% if @options[:trackLinks] %>,
8
+ trackLinks:true<% end %><% if @options[:accurateTrackBounce] %>,
9
+ accurateTrackBounce:true<% end %><% if @options[:trackHash] %>,
10
+ trackHash:true<% end %>});
11
+ } catch(e) { }
12
+ });
13
+ var n = d.getElementsByTagName("script")[0],
14
+ s = d.createElement("script"),
15
+ f = function () { n.parentNode.insertBefore(s, n); };
16
+ s.type = "text/javascript";
17
+ s.async = true;
18
+ s.src = (d.location.protocol == "https:" ? "https:" : "http:") + "//mc.yandex.ru/metrika/watch.js";
19
+ if (w.opera == "[object Opera]") {
20
+ d.addEventListener("DOMContentLoaded", f, false);
21
+ } else { f(); }
22
+ })(document, window, "yandex_metrika_callbacks");
23
+ </script>
24
+ <noscript><div><img src="//mc.yandex.ru/watch/<%= @options[:counter_id].to_i %>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
@@ -0,0 +1,11 @@
1
+ <script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript"></script>
2
+ <script type="text/javascript">
3
+ try { var yaCounter<%= @options[:counter_id].to_i %> = new Ya.Metrika({id:<%= @options[:counter_id].to_i %><% if @options[:webvisor] %>,
4
+ webvisor:true<% end %><% if @options[:clickmap] %>,
5
+ clickmap:true<% end %><% if @options[:trackLinks] %>,
6
+ trackLinks:true<% end %><% if @options[:accurateTrackBounce] %>,
7
+ accurateTrackBounce:true<% end %><% if @options[:trackHash] %>,
8
+ trackHash:true<% end %>});
9
+ } catch(e) { }
10
+ </script>
11
+ <noscript><div><img src="//mc.yandex.ru/watch/<%= @options[:counter_id].to_i %>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
@@ -0,0 +1,34 @@
1
+ require 'rack'
2
+ require 'erb'
3
+
4
+ module Rack
5
+ class YandexMetrika
6
+ DEFAULT = { :async => true }
7
+
8
+ def initialize(app, options = {})
9
+ raise ArgumentError, "Counter_id must be set!" unless options[:counter_id] and options[:counter_id] != 0
10
+ @app, @options = app, DEFAULT.merge(options)
11
+ end
12
+
13
+ def call(env); dup._call(env); end
14
+
15
+ def _call(env)
16
+ @status, @headers, @body = @app.call(env)
17
+ return [@status, @headers, @body] unless html?
18
+ response = Rack::Response.new([], @status, @headers)
19
+ @body.each { |fragment| response.write inject(fragment) }
20
+ @body.close if @body.respond_to?(:close)
21
+ response.finish
22
+ end
23
+
24
+ private
25
+
26
+ def html?; @headers['Content-Type'] =~ /html/; end
27
+
28
+ def inject(response)
29
+ file = @options[:async] ? 'async' : 'sync'
30
+ @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb", __FILE__)
31
+ response.gsub(%r{</body>}, @template.result(binding) + "</body>")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class YandexMetrika
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-yandex-metrika
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ilya Konyukhov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: test-unit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Simple Rack middleware for implementing Yandex Metrika tracking in your
95
+ Ruby-Rack based project. Supports synchronous and asynchronous insertion and configurable
96
+ load options.
97
+ email:
98
+ - ilya@konyukhov.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - lib/rack/yandex-metrika.rb
104
+ - lib/rack/templates/async.erb
105
+ - lib/rack/templates/sync.erb
106
+ - lib/rack/yandex-metrika/version.rb
107
+ - lib/rack-yandex-metrika.rb
108
+ - README.md
109
+ - LICENSE
110
+ homepage: https://github.com/ilkon/rack-yandex-metrika
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.24
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Rack middleware to inject Yandex Metrika tracking code into outgoing responses.
134
+ test_files: []