yandex-metrika-rails 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/README.md +24 -5
- data/lib/yandex-metrika-rails.rb +22 -0
- data/lib/yandex-metrika/counter.rb +8 -4
- data/lib/yandex-metrika/version.rb +1 -1
- data/test/configure_metrika_test.rb +14 -0
- data/test/rails/advanced_options_test.rb +22 -0
- data/test/rails/views_helper_test.rb +9 -1
- metadata +5 -1
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
[![Coverage
|
3
3
|
Status](https://coveralls.io/repos/saratovsource/yandex-metrika-rails/badge.png?branch=master)](https://coveralls.io/r/saratovsource/yandex-metrika-rails)
|
4
4
|
|
5
|
-
|
5
|
+
Yandex Metrika gem for Rails 3.x. It based on [google-analytics-rails]: https://github.com/bgarret/google-analytics-rails gem.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -14,13 +14,32 @@ And then execute:
|
|
14
14
|
|
15
15
|
$ bundle
|
16
16
|
|
17
|
-
|
17
|
+
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
`config/environments/<environment>.rb`
|
20
20
|
|
21
|
-
|
21
|
+
YM.counter = "" # You shoul insert you Yandex Metrica code
|
22
|
+
|
23
|
+
You must leave this field blank if you do not want to display the metric
|
24
|
+
on the page for that environment.
|
25
|
+
`app/views/layouts/application.html.haml`
|
26
|
+
|
27
|
+
= metrika_init
|
28
|
+
|
29
|
+
You can manually specify those counters that you want to use.
|
30
|
+
`config/environments/<environment>.rb`
|
31
|
+
|
32
|
+
YM.counter = "" # You shoul insert you Yandex Metrica code
|
33
|
+
YM.set_counters :webvisor, :trackLinks
|
34
|
+
|
35
|
+
You can adjust the metric for the request asynchronously.
|
36
|
+
|
37
|
+
= metrika_init(async: true) # By default
|
38
|
+
|
39
|
+
Or synchronously...
|
40
|
+
|
41
|
+
= metrika_init(async: false)
|
22
42
|
|
23
|
-
TODO: Write usage instructions here
|
24
43
|
|
25
44
|
## Contributing
|
26
45
|
|
data/lib/yandex-metrika-rails.rb
CHANGED
@@ -2,6 +2,11 @@ require 'yandex-metrika/version'
|
|
2
2
|
require 'yandex-metrika/counter'
|
3
3
|
|
4
4
|
module YandexMetrika
|
5
|
+
DEFAULTS = {
|
6
|
+
webvisor: true,
|
7
|
+
clickmap: true,
|
8
|
+
trackLinks: true
|
9
|
+
}
|
5
10
|
|
6
11
|
class << self
|
7
12
|
def counter
|
@@ -19,6 +24,23 @@ module YandexMetrika
|
|
19
24
|
def invalid_counter?
|
20
25
|
counter.nil? || counter == ""
|
21
26
|
end
|
27
|
+
|
28
|
+
def set_counters(*counters)
|
29
|
+
counters.each{|c| add_counter(c)}
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_counter(counter)
|
33
|
+
@options ||= {}
|
34
|
+
@options[counter] = true
|
35
|
+
end
|
36
|
+
|
37
|
+
def options
|
38
|
+
@options ||= DEFAULTS
|
39
|
+
end
|
40
|
+
|
41
|
+
def reset!
|
42
|
+
@options = nil
|
43
|
+
end
|
22
44
|
end
|
23
45
|
|
24
46
|
end
|
@@ -9,13 +9,14 @@ module YandexMetrika
|
|
9
9
|
autoload :TemplateOptions, 'yandex-metrika/counter/template_options'
|
10
10
|
|
11
11
|
def initialize(args = {})
|
12
|
-
@
|
12
|
+
@options_types ||= []
|
13
13
|
prepare_view_options(args)
|
14
14
|
append_counter_types(args)
|
15
|
-
@options_renderer = YandexMetrika::Counter::Renderer.new(@
|
15
|
+
@options_renderer = YandexMetrika::Counter::Renderer.new(@options_types)
|
16
16
|
end
|
17
17
|
|
18
18
|
def to_s
|
19
|
+
return if YM.invalid_counter?
|
19
20
|
template_name = @async ? "async" : "sync"
|
20
21
|
@template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{template_name}.erb", __FILE__)
|
21
22
|
@template.result(template_options.instance_eval { binding }).html_safe
|
@@ -33,13 +34,16 @@ module YandexMetrika
|
|
33
34
|
|
34
35
|
def prepare_view_options(args)
|
35
36
|
@local = args.delete(:local) || false
|
36
|
-
@async =
|
37
|
+
@async = true
|
38
|
+
@async = args.delete(:async) if args.include? :async
|
37
39
|
@noscript = args.delete(:noscript) || true
|
38
40
|
end
|
39
41
|
|
40
42
|
def append_counter_types(args = {})
|
43
|
+
args = YM.options.merge(args)
|
44
|
+
@options_types << Kind.new(:id, YM.counter)
|
41
45
|
args.each do |k,v|
|
42
|
-
@
|
46
|
+
@options_types << Kind.new(k,v)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ConfigurationBuilderTest < TestCase
|
4
|
+
def setup
|
5
|
+
YM.reset!
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_have_specific_configuration
|
9
|
+
YM.set_counters :webvisor, :trackLinks
|
10
|
+
@options = {webvisor: true, trackLinks: true}
|
11
|
+
assert_equal @options, YM.options
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "yandex-metrika/rails/view_helpers"
|
3
|
+
|
4
|
+
class AdvancedOptionsTest < TestCase
|
5
|
+
include YandexMetrika::Rails::ViewHelpers
|
6
|
+
|
7
|
+
def setup
|
8
|
+
YM.counter = 123
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_not_render_without_counter
|
12
|
+
YM.counter = ""
|
13
|
+
metrika_init_script = metrika_init
|
14
|
+
refute_match %r{w\.yaCounter}, metrika_init_script
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_script_should_be_sync
|
18
|
+
metrika_init_script = metrika_init(async: false)
|
19
|
+
refute_match %r{w\.yaCounter}, metrika_init_script
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -4,9 +4,18 @@ require "yandex-metrika/rails/view_helpers"
|
|
4
4
|
class ViewHelpersTest < TestCase
|
5
5
|
include YandexMetrika::Rails::ViewHelpers
|
6
6
|
|
7
|
+
def setup
|
8
|
+
YM.counter = 123
|
9
|
+
YM.reset!
|
10
|
+
end
|
11
|
+
|
7
12
|
def test_showl_have_default_options
|
8
13
|
metrika_init_script = metrika_init
|
9
14
|
assert_match %r{w\.yaCounter}, metrika_init_script
|
15
|
+
assert_match %r{id:#{YM.counter}}, metrika_init_script
|
16
|
+
assert_match %r{webvisor:true}, metrika_init_script
|
17
|
+
assert_match %r{trackLinks:true}, metrika_init_script
|
18
|
+
assert_match %r{clickmap:true}, metrika_init_script
|
10
19
|
assert_match %r{\<\/noscript\>}, metrika_init_script
|
11
20
|
end
|
12
21
|
|
@@ -34,5 +43,4 @@ class ViewHelpersTest < TestCase
|
|
34
43
|
metrika_init_script = metrika_init(accurateTrackBounce: true)
|
35
44
|
assert_match %r{accurateTrackBounce:true}, metrika_init_script
|
36
45
|
end
|
37
|
-
|
38
46
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yandex-metrika-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -66,6 +66,8 @@ files:
|
|
66
66
|
- lib/yandex-metrika/templates/async.erb
|
67
67
|
- lib/yandex-metrika/templates/sync.erb
|
68
68
|
- lib/yandex-metrika/version.rb
|
69
|
+
- test/configure_metrika_test.rb
|
70
|
+
- test/rails/advanced_options_test.rb
|
69
71
|
- test/rails/views_helper_test.rb
|
70
72
|
- test/test_helper.rb
|
71
73
|
- yandex-metrika-rails.gemspec
|
@@ -95,5 +97,7 @@ signing_key:
|
|
95
97
|
specification_version: 3
|
96
98
|
summary: Rails 3 helpers to manage Yandex Metrika and other services.
|
97
99
|
test_files:
|
100
|
+
- test/configure_metrika_test.rb
|
101
|
+
- test/rails/advanced_options_test.rb
|
98
102
|
- test/rails/views_helper_test.rb
|
99
103
|
- test/test_helper.rb
|