eff_matomo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0919a5f8c95ff41737d832f9055262230a4d377ad1ed4ff4d4c1549e6cd49021
4
+ data.tar.gz: bd14a94d8484777a0081de23e7366513f17aab67cd5a7bccab4726ee6b405a32
5
+ SHA512:
6
+ metadata.gz: 0adcbe9d7bf799fb9e11f0a7cb48d0d2ff7543a8b770dc96780082aebe76c8b3109518bfa9e5f1be382e87a0ad78b17715f527f8495b60cd070d881ad5ed2dbf
7
+ data.tar.gz: 6aa1698f1643997c1134933783fafa56faee12fedbeb0a7d2c0c0b09a18ce2f1757102f94801fbfd64706d7a5dc64b487ade41f13e51aa75d6711e393bcc75b2
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in eff_matomo.gemspec
6
+ gemspec
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ eff_matomo (0.1.0)
5
+ activesupport
6
+ httparty
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ addressable (2.5.2)
17
+ public_suffix (>= 2.0.2, < 4.0)
18
+ concurrent-ruby (1.0.5)
19
+ crack (0.4.3)
20
+ safe_yaml (~> 1.0.0)
21
+ diff-lcs (1.3)
22
+ hashdiff (0.3.7)
23
+ httparty (0.16.2)
24
+ multi_xml (>= 0.5.2)
25
+ i18n (1.1.0)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.11.3)
28
+ multi_xml (0.6.0)
29
+ public_suffix (3.0.3)
30
+ rake (10.5.0)
31
+ rspec (3.8.0)
32
+ rspec-core (~> 3.8.0)
33
+ rspec-expectations (~> 3.8.0)
34
+ rspec-mocks (~> 3.8.0)
35
+ rspec-core (3.8.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-expectations (3.8.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-mocks (3.8.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-support (3.8.0)
44
+ safe_yaml (1.0.4)
45
+ thread_safe (0.3.6)
46
+ tzinfo (1.2.5)
47
+ thread_safe (~> 0.1)
48
+ vcr (4.0.0)
49
+ webmock (3.4.2)
50
+ addressable (>= 2.3.6)
51
+ crack (>= 0.3.2)
52
+ hashdiff
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ bundler (~> 1.16)
59
+ eff_matomo!
60
+ rake (~> 10.0)
61
+ rspec (~> 3.0)
62
+ vcr
63
+ webmock
64
+
65
+ BUNDLED WITH
66
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 vivian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # EFF Matomo
2
+
3
+ The EFF Matomo gem provides utilities for integrating our Ruby applications with our analytics tool, Matomo.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'eff_matomo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install eff_matomo
20
+
21
+ ## Usage
22
+
23
+ ### Configuration
24
+
25
+ This gem reads two environment variables:
26
+ * `MATOMO_SITE_ID`: The ID in Matomo of the app being tracked. **Required**.
27
+ * `MATOMO_BASE_URL`: The URL where Matomo is being hosted. Defaults to "https://anon-stats.eff.org".
28
+
29
+ ### Adding the Matomo tracking embed to a Rails app
30
+
31
+ Add `<%= matomo_tracking_embed %>` to the footer of your application layout template.
32
+
33
+ ### Displaying Matomo data
34
+
35
+ This gem provides allows users to import site usage data from Matomo to display in their application. It currently supports two types of data:
36
+
37
+ **Referrers** show how users are reaching the application. Usage example:
38
+ ```
39
+ # Get the top five referrers for the site
40
+ referrers = Matomo::Referrer.top
41
+
42
+ # Scope referrers by date range
43
+ referrers = Matomo::Referrer.where(start_date: Time.now - 1.month, end_date: Time.now)
44
+
45
+ # Only show referrers for a certain page within the app
46
+ referrers = Matomo::Referrer.where(path: "/action/my-important-action")
47
+
48
+ # Access information about each referrers
49
+ referrers.each() do |referrer|
50
+ puts referrer.label # eg. "facebook.com"
51
+ puts referrer.visits # Number of times a visit came from this referrer
52
+ puts referrer.actions_per_visit # Average number of actions that occured during a visit
53
+ end
54
+ ```
55
+
56
+ **Visited Pages** show the top pages within the application, both in terms of unique page views and overall number of hits. Usage example:
57
+ ```
58
+ # Get the top pages under a certain path, for example under "/articles"
59
+ pages = Matomo::VisitedPage.where(base_path: "/articles")
60
+
61
+ # Access information about each page
62
+ pages.each() do |page|
63
+ puts page.label # eg. "/harm-reduction"
64
+ puts page.path # eg. "/acticles/harm-reduction"
65
+ puts page.hits # Overall number of hits on the page
66
+ puts page.visits # The number of distinct visits to the page
67
+ end
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "matomo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "matomo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eff_matomo"
8
+ spec.version = Matomo::VERSION
9
+ spec.authors = ["vivian"]
10
+ spec.email = ["vivian@eff.org"]
11
+
12
+ spec.summary = %q{Utilities for EFF applications accessing the Matomo API}
13
+ spec.homepage = "https://github.com/efforg/eff-matomo"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "activesupport"
26
+ spec.add_dependency "httparty"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "webmock"
32
+ spec.add_development_dependency "vcr"
33
+ end
@@ -0,0 +1,156 @@
1
+ require "matomo/version"
2
+ require 'matomo/railtie' if defined?(Rails)
3
+ require "active_support/core_ext/hash"
4
+ require "active_support/core_ext/string"
5
+ require "httparty"
6
+
7
+ module Matomo
8
+ class Referrer
9
+ attr_accessor :label, :visits
10
+
11
+ def initialize(params)
12
+ @label = params["label"]
13
+ @visits = params["nb_visits"]
14
+ @actions = params["nb_actions"]
15
+ end
16
+
17
+ def actions_per_visit
18
+ return 0 unless @actions and @visits
19
+ (@actions/@visits.to_f).round(1)
20
+ end
21
+
22
+ def self.top() where end
23
+
24
+ def self.where(**args)
25
+ params = { method: "Referrers.getAll" }.merge(
26
+ Matomo.date_range_params(args[:start_date], args[:end_date])
27
+ )
28
+ if args[:path]
29
+ params[:segment] = "pageUrl==#{Matomo.tracked_site_url}#{args[:path]}"
30
+ end
31
+ resp = Matomo.get(params)
32
+ return [] if resp.response.code != "200"
33
+ resp.map{ |x| new(x) }
34
+ end
35
+ end
36
+
37
+ class VisitedPage
38
+ attr_accessor :label, :hits, :visits
39
+
40
+ def initialize(base_path, params)
41
+ @base_path = base_path
42
+ @label = params["label"].sub!(/^\//, "") if params["label"]
43
+ @hits = params["nb_hits"]
44
+ @visits = params["nb_visits"]
45
+ end
46
+
47
+ def path
48
+ "/#{@base_path}/#{@label}"
49
+ end
50
+
51
+ def self.where(**args)
52
+ get_subtables unless @subtables
53
+ # Remove leading and trailing slashes to match Matomo label format.
54
+ base_path = args[:base_path].gsub(/^\/|\/$/, "")
55
+ return [] unless @subtables[base_path]
56
+
57
+ resp = Matomo.get({
58
+ method: "Actions.getPageUrls",
59
+ idSubtable: @subtables[base_path],
60
+ })
61
+ return [] if resp.response.code != "200"
62
+ resp.map{ |x| new(base_path, x) }
63
+ end
64
+
65
+ def self.get_subtables
66
+ # Get a mapping from resource paths to Matomo page view subtable ids
67
+ resp = Matomo.get({
68
+ method: "Actions.getPageUrls",
69
+ filter_limit: 50,
70
+ })
71
+ if resp.response.code == "200"
72
+ @subtables = resp.map{|x| [x["label"], x["idsubdatatable"]]}.to_h
73
+ else
74
+ @subtables = {}
75
+ end
76
+ end
77
+ end
78
+
79
+ def self.visits_graph_url
80
+ base_url + "?" + default_api_params.merge({
81
+ method: "ImageGraph.get",
82
+ apiModule: "VisitsSummary",
83
+ apiAction: "get",
84
+ token_auth: "anonymous",
85
+ width: 800,
86
+ height: 400,
87
+ period: "day",
88
+ }).to_query
89
+ end
90
+
91
+ def self.top_pages_url
92
+ base_portal_url+"&category=General_Actions&subcategory=General_Pages"
93
+ end
94
+
95
+ def self.top_referrers_url
96
+ base_portal_url+"&category=Referrers_Referrers&subcategory=Referrers_WidgetGetAll"
97
+ end
98
+
99
+ private
100
+
101
+ def self.get(params)
102
+ url = base_url + "?" + default_api_params.merge(params).to_query
103
+ HTTParty.get(url)
104
+ end
105
+
106
+ def self.base_url
107
+ ENV["MATOMO_BASE_URL"] || "https://anon-stats.eff.org"
108
+ end
109
+
110
+ def self.site_id
111
+ ENV["MATOMO_SITE_ID"]
112
+ end
113
+
114
+ # ##
115
+ # Gnarly base url for finding pages on the Matomo web portal
116
+ def self.base_portal_url
117
+ "#{base_url}/index.php?module=CoreHome&action=index&"\
118
+ "idSite=#{site_id}&period=#{default_api_params[:period]}&date=#{default_api_params[:date]}&updated=1#?"\
119
+ "idSite=#{site_id}&period=#{default_api_params[:period]}&date=#{default_api_params[:date]}"\
120
+ end
121
+
122
+ def self.default_api_params
123
+ {
124
+ module: "API",
125
+ idSite: site_id,
126
+ format: "JSON",
127
+ period: "range",
128
+ date: "last30",
129
+ filter_limit: 5
130
+ }
131
+ end
132
+
133
+ def self.date_range_params(start_date, end_date)
134
+ date_format = "%Y-%m-%d"
135
+ end_date = end_date || Date.today
136
+ start_date = start_date || end_date - 30.days
137
+ {
138
+ period: "range",
139
+ date: start_date.strftime(date_format) + "," + end_date.strftime(date_format)
140
+ }
141
+ end
142
+
143
+ ##
144
+ # The full url of a tracked page is sometimes required by the API, eg. when
145
+ # scoping by page. We can load the base url from the environment or by
146
+ # making an API call.
147
+ def self.tracked_site_url
148
+ return ENV["MATOMO_TRACKED_SITE_URL"] if ENV["MATOMO_TRACKED_SITE_URL"]
149
+ return @tracked_site_url if @tracked_site_url
150
+ resp = get({
151
+ method: "SitesManager.getSiteUrlsFromId"
152
+ })
153
+ return nil if resp.response.code != "200" || resp.length == 0
154
+ @tracked_site_url = resp[0]
155
+ end
156
+ end
@@ -0,0 +1,6 @@
1
+ require 'matomo/view_helpers.rb'
2
+ module Matomo
3
+ class Railtie < Rails::Railtie
4
+ ActionView::Base.send :include, Matomo::ViewHelpers
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Matomo
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ module Matomo
2
+ module ViewHelpers
3
+ def matomo_tracking_url
4
+ "https://anon-stats.eff.org/js/?" + {
5
+ idsite: Matomo.site_id,
6
+ rec: 1,
7
+ action_name: page_title,
8
+ url: request.original_url
9
+ }.to_param
10
+ end
11
+
12
+ def matomo_tracking_embed
13
+ content_tag(:div, id: "anon-stats") do
14
+ content_tag(:noscript) do
15
+ content_tag(:img, src: matomo_tracking_url, style: "border:0", alt: "")
16
+ end
17
+ javascript_tag do
18
+ "document.getElementById('anon-stats').innerHTML = '<img src=\"#{matomo_tracking_url}\"?urlref=' + encodeURIComponent(document.referrer) + 'style=\"border:0\" alt=\"\" />';".html_safe
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eff_matomo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - vivian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - vivian@eff.org
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - eff_matomo.gemspec
129
+ - lib/matomo.rb
130
+ - lib/matomo/railtie.rb
131
+ - lib/matomo/version.rb
132
+ - lib/matomo/view_helpers.rb
133
+ homepage: https://github.com/efforg/eff-matomo
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.7.7
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Utilities for EFF applications accessing the Matomo API
157
+ test_files: []