yandex-metrica-rails 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ repo_token: 7XAqRDzxWmghPK1B8UcBIhHnDxULfAuzj
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yandex-metrika-rails.gemspec
4
+ gemspec
5
+
6
+ gem 'tconsole'
7
+ gem 'simplecov'
8
+ gem 'activesupport'
9
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kirillov Alexander
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Yandex::Metrica::Rails
2
+ [![Coverage
3
+ Status](https://coveralls.io/repos/saratovsource/yandex-metrica-rails/badge.png?branch=master)](https://coveralls.io/r/saratovsource/yandex-metrica-rails)
4
+
5
+ Yandex Metrica gem for Rails 3.x. It based on [google-analytics-rails]: https://github.com/bgarret/google-analytics-rails gem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'yandex-metrica-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ `config/environments/<environment>.rb`
20
+
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
+ = metrica_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
+ = metrica_init(async: true) # By default
38
+
39
+ Or synchronously...
40
+
41
+ = metrica_init(async: false)
42
+
43
+ #### Available settings:
44
+
45
+ = metrica_init(async: false, clickmap: true, noscript: false)
46
+
47
+ `webvisor` : WebVisor
48
+ `clickmap` : Click map
49
+ `trackLinks` : External links, file downloads and "Share" button report
50
+ `trackHash` : Hash tracking in the browser address window
51
+ `accurateTrackBounce` : Accurate bounce rate
52
+ `noscript` : Show `noscript` tag at the page. `true` by default.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,52 @@
1
+ require 'yandex-metrica/version'
2
+ require 'yandex-metrica/counter'
3
+
4
+ module YandexMetrica
5
+ DEFAULTS = {
6
+ webvisor: true,
7
+ clickmap: true,
8
+ trackLinks: true
9
+ }
10
+
11
+ class << self
12
+ def counter
13
+ @counter || nil
14
+ end
15
+
16
+ def counter=(counter)
17
+ @counter = counter
18
+ end
19
+
20
+ def valid_counter?
21
+ !invalid_counter?
22
+ end
23
+
24
+ def invalid_counter?
25
+ counter.nil? || counter == ""
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
44
+ end
45
+
46
+ end
47
+
48
+ YM = YandexMetrica
49
+ if defined?(Rails)
50
+ require 'yandex-metrica/rails/railtie'
51
+ YMR = YandexMetrica::Rails
52
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_support/core_ext/string/output_safety'
2
+ require 'erb'
3
+ module YandexMetrica
4
+ class Counter
5
+ attr_reader :local, :counter_types, :noscript, :async
6
+
7
+ autoload :Kind, 'yandex-metrica/counter/kind'
8
+ autoload :Renderer, 'yandex-metrica/counter/renderer'
9
+ autoload :TemplateOptions, 'yandex-metrica/counter/template_options'
10
+
11
+ def initialize(args = {})
12
+ @options_types ||= []
13
+ prepare_view_options(args)
14
+ append_counter_types(args)
15
+ @options_renderer = YandexMetrica::Counter::Renderer.new(@options_types)
16
+ end
17
+
18
+ def to_s
19
+ return if YM.invalid_counter?
20
+ template_name = @async ? "async" : "sync"
21
+ @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{template_name}.erb", __FILE__)
22
+ @template.result(template_options.instance_eval { binding }).html_safe
23
+ end
24
+ alias_method :render, :to_s
25
+
26
+ protected
27
+
28
+ def template_options
29
+ TemplateOptions.new({
30
+ counter_name: "yaCounter#{YM.counter}",
31
+ counter_options: @options_renderer.to_s,
32
+ noscript: @noscript
33
+ })
34
+ end
35
+
36
+ def prepare_view_options(args)
37
+ @local = args.delete(:local) || false
38
+ @async = true
39
+ @async = args.delete(:async) if args.include? :async
40
+ @noscript = true
41
+ @noscript = args.delete(:noscript) if args.include? :noscript
42
+ end
43
+
44
+ def append_counter_types(args = {})
45
+ args = YM.options.merge(args)
46
+ @options_types << Kind.new(:id, YM.counter)
47
+ args.each do |k,v|
48
+ @options_types << Kind.new(k,v)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ module YandexMetrica
2
+ class Counter::Kind
3
+ attr_reader :name, :value
4
+ def initialize(name, value)
5
+ @name, @value = name, value
6
+ end
7
+
8
+ def to_s
9
+ "#{@name}:#{@value}"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module YandexMetrica
2
+ class Counter::Renderer
3
+ attr_reader :counter_types_collection
4
+ DELIMITER = ', '
5
+ def initialize(counter_types_collection)
6
+ @counter_types_collection = counter_types_collection
7
+ end
8
+
9
+ def to_s
10
+ @counter_types_collection.map{|ct| ct.to_s}.join(DELIMITER)
11
+ end
12
+ alias_method :render, :to_s
13
+
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module YandexMetrica
2
+ class Counter::TemplateOptions < OpenStruct
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module YandexMetrica::Rails
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "yandex-metrica-rails" do
4
+ ActionView::Base.send :include, ViewHelpers
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module YandexMetrica::Rails
2
+ module ViewHelpers
3
+
4
+ def metrica_init(options = {})
5
+ YandexMetrica::Counter.new(options).to_s
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ <script type="text/javascript">
2
+ (function (d, w, c) {
3
+ (w[c] = w[c] || []).push(function() {
4
+ try { w.<%= counter_name %> = new Ya.Metrika(<%= counter_options %>);
5
+ } catch(e) { }
6
+ });
7
+
8
+ var n = d.getElementsByTagName("script")[0],
9
+ s = d.createElement("script"),
10
+ f = function () { n.parentNode.insertBefore(s, n); };
11
+ s.type = "text/javascript";
12
+ s.async = true;
13
+ s.src = (d.location.protocol == "https:" ? "https:" : "http:") + "//mc.yandex.ru/metrika/watch.js";
14
+
15
+ if (w.opera == "[object Opera]") {
16
+ d.addEventListener("DOMContentLoaded", f, false);
17
+ } else { f(); }
18
+ })(document, window, "yandex_metrika_callbacks");
19
+ </script>
20
+ <% if noscript %>
21
+ <noscript><div><img src="//mc.yandex.ru/watch/<%= counter %>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
22
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <script src="//mc.yandex.ru/metrika/watch.js" type="text/javascript"></script>
2
+ <script type="text/javascript">
3
+ try { var <%= counter_name %> = new Ya.Metrika(<%= counter_options %>);
4
+ } catch(e) { }
5
+ </script>
6
+ <% if noscript %>
7
+ <noscript><div><img src="//mc.yandex.ru/watch/<%= counter %>" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
8
+ <% end %>
@@ -0,0 +1,3 @@
1
+ module YandexMetrica
2
+ VERSION = "0.1.3"
3
+ 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-metrica/rails/view_helpers"
3
+
4
+ class AdvancedOptionsTest < TestCase
5
+ include YandexMetrica::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
+ metrica_init_script = metrica_init
14
+ refute_match %r{w\.yaCounter}, metrica_init_script
15
+ end
16
+
17
+ def test_script_should_be_sync
18
+ metrica_init_script = metrica_init(async: false)
19
+ refute_match %r{w\.yaCounter}, metrica_init_script
20
+ end
21
+
22
+ end
@@ -0,0 +1,51 @@
1
+ require "test_helper"
2
+ require "yandex-metrica/rails/view_helpers"
3
+
4
+ class ViewHelpersTest < TestCase
5
+ include YandexMetrica::Rails::ViewHelpers
6
+
7
+ def setup
8
+ YM.counter = 123
9
+ YM.reset!
10
+ end
11
+
12
+ def test_showl_have_default_options
13
+ metrica_init_script = metrica_init
14
+ assert_match %r{w\.yaCounter}, metrica_init_script
15
+ assert_match %r{id:#{YM.counter}}, metrica_init_script
16
+ assert_match %r{webvisor:true}, metrica_init_script
17
+ assert_match %r{trackLinks:true}, metrica_init_script
18
+ assert_match %r{clickmap:true}, metrica_init_script
19
+ assert_match %r{\<\/noscript\>}, metrica_init_script
20
+ end
21
+
22
+ def test_metrica_should_not_have_noscript_tag
23
+ metrica_init_script = metrica_init(noscript: false)
24
+ refute_match %r{\<\/noscript\>}, metrica_init_script
25
+ end
26
+
27
+ def test_metrica_should_have_webvisor
28
+ metrica_init_script = metrica_init(webvisor: true)
29
+ assert_match %r{webvisor:true}, metrica_init_script
30
+ end
31
+
32
+ def test_metrica_should_have_track_links
33
+ metrica_init_script = metrica_init(trackLinks: true)
34
+ assert_match %r{trackLinks:true}, metrica_init_script
35
+ end
36
+
37
+ def test_metrica_should_have_track_hash
38
+ metrica_init_script = metrica_init(trackHash: true)
39
+ assert_match %r{trackHash:true}, metrica_init_script
40
+ end
41
+
42
+ def test_metrica_should_have_clickmap
43
+ metrica_init_script = metrica_init(clickmap: true)
44
+ assert_match %r{clickmap:true}, metrica_init_script
45
+ end
46
+
47
+ def test_metrica_should_have_accurate_track_bounce
48
+ metrica_init_script = metrica_init(accurateTrackBounce: true)
49
+ assert_match %r{accurateTrackBounce:true}, metrica_init_script
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'yandex-metrica-rails'
3
+ require 'test/unit'
4
+ require 'simplecov'
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+ ENV["COVERAGE"] = "true"
9
+ SimpleCov.start if ENV["COVERAGE"]
10
+
11
+ YM.counter = "123"
12
+ class TestCase < MiniTest::Unit::TestCase
13
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yandex-metrica/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yandex-metrica-rails"
8
+ spec.version = YandexMetrica::VERSION
9
+ spec.authors = ["Kirillov Alexander"]
10
+ spec.email = ["saratovsource@gmail.com"]
11
+ spec.description = %q{Rails 3 helpers to manage Yandex Metrica and other services.}
12
+ spec.summary = %q{Rails 3 helpers to manage Yandex Metrica and other services.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yandex-metrica-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kirillov Alexander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-18 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: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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
+ description: Rails 3 helpers to manage Yandex Metrica and other services.
47
+ email:
48
+ - saratovsource@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .coveralls.yml
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/yandex-metrica-rails.rb
60
+ - lib/yandex-metrica/counter.rb
61
+ - lib/yandex-metrica/counter/kind.rb
62
+ - lib/yandex-metrica/counter/renderer.rb
63
+ - lib/yandex-metrica/counter/template_options.rb
64
+ - lib/yandex-metrica/rails/railtie.rb
65
+ - lib/yandex-metrica/rails/view_helpers.rb
66
+ - lib/yandex-metrica/templates/async.erb
67
+ - lib/yandex-metrica/templates/sync.erb
68
+ - lib/yandex-metrica/version.rb
69
+ - test/configure_metrica_test.rb
70
+ - test/rails/advanced_options_test.rb
71
+ - test/rails/views_helper_test.rb
72
+ - test/test_helper.rb
73
+ - yandex-metrica-rails.gemspec
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.25
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Rails 3 helpers to manage Yandex Metrica and other services.
99
+ test_files:
100
+ - test/configure_metrica_test.rb
101
+ - test/rails/advanced_options_test.rb
102
+ - test/rails/views_helper_test.rb
103
+ - test/test_helper.rb