active_analytics 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +88 -15
  3. data/app/controllers/active_analytics/application_controller.rb +23 -5
  4. data/app/controllers/active_analytics/assets_controller.rb +29 -0
  5. data/app/controllers/active_analytics/pages_controller.rb +9 -10
  6. data/app/controllers/active_analytics/referrers_controller.rb +9 -5
  7. data/app/controllers/active_analytics/sites_controller.rb +4 -4
  8. data/app/models/active_analytics/views_per_day.rb +22 -3
  9. data/app/views/active_analytics/assets/_charts.css +249 -0
  10. data/app/{assets/stylesheets/active_analytics/style.css → views/active_analytics/assets/_style.css} +36 -26
  11. data/app/views/active_analytics/assets/application.css.erb +2 -0
  12. data/app/{assets/javascripts/active_analytics → views/active_analytics/assets}/application.js +1 -3
  13. data/app/{assets/stylesheets/active_analytics → views/active_analytics/assets}/ariato.css +237 -234
  14. data/app/views/active_analytics/pages/_table.html.erb +6 -17
  15. data/app/views/active_analytics/pages/index.html.erb +1 -1
  16. data/app/views/active_analytics/pages/show.html.erb +7 -4
  17. data/app/views/active_analytics/referrers/_table.html.erb +9 -2
  18. data/app/views/active_analytics/referrers/index.html.erb +2 -2
  19. data/app/views/active_analytics/referrers/show.html.erb +6 -3
  20. data/app/views/active_analytics/sites/_histogram.html.erb +9 -2
  21. data/app/views/active_analytics/sites/_histogram_header.html.erb +10 -0
  22. data/app/views/active_analytics/sites/show.html.erb +2 -2
  23. data/app/views/layouts/active_analytics/_footer.html.erb +3 -3
  24. data/app/views/layouts/active_analytics/_header.html.erb +1 -3
  25. data/app/views/layouts/active_analytics/application.html.erb +5 -3
  26. data/config/routes.rb +2 -1
  27. data/lib/active_analytics/engine.rb +0 -4
  28. data/lib/active_analytics/version.rb +1 -1
  29. data/lib/active_analytics.rb +41 -3
  30. metadata +11 -9
  31. data/app/assets/stylesheets/active_analytics/application.css +0 -15
  32. data/app/assets/stylesheets/active_analytics/charts.css +0 -296
  33. /data/app/{assets/javascripts/active_analytics → views/active_analytics/assets}/ariato.js +0 -0
@@ -1,4 +1,3 @@
1
-
2
1
  <% if pages.empty? %>
3
2
  <div class="is-empty">
4
3
  <span>no data</span>
@@ -8,26 +7,16 @@
8
7
  <% for page in pages %>
9
8
  <tr>
10
9
  <td>
11
- <% if page.host == params[:site] %>
12
- <% if page.path.present? %>
13
- <%= link_to page.path, page_path(site: page.host, page: page_to_params(page.path), from: params[:from], to: params[:to]) %>
14
- <% else %>
15
- <%= site_icon page.host %>
16
- <%= link_to page.host, site_path(site: page.host, from: params[:from], to: params[:to]) %>
17
- <small>(page not provided <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy#strict-origin-when-cross-origin">?</a>)</small>
18
- <% end %>
19
- <% elsif page.host.present? && page.path.present? %>
20
- <%= link_to(referrer_path(site: params[:site], referrer: page.host, from: params[:from], to: params[:to])) { site_icon page.host } %>
21
- <%= link_to page.url, referrer_path(site: params[:site], referrer: page.host, from: params[:from], to: params[:to]) %>
22
- <% elsif page.host.present? %>
23
- <%= site_icon page.host %>
24
- <%= page.host %>
10
+ <% if page.path.present? %>
11
+ <%= link_to page.path, page_path(site: page.host, page: page_to_params(page.path), from: params[:from], to: params[:to]) %>
25
12
  <% else %>
26
- (None or direct)
13
+ <%= site_icon page.host %>
14
+ <%= link_to page.host, site_path(site: page.host, from: params[:from], to: params[:to]) %>
15
+ <small>(page not provided <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy#strict-origin-when-cross-origin">?</a>)</small>
27
16
  <% end %>
28
17
  </td>
29
18
  <td class="number"><%= format_view_count page.total %></td>
30
19
  </tr>
31
20
  <% end %>
32
21
  </table>
33
- <% end %>
22
+ <% end %>
@@ -1,5 +1,5 @@
1
1
  <section class="card">
2
- <h3><%= format_view_count @histogram.total %> views</h3>
2
+ <%= render "/active_analytics/sites/histogram_header", histogram: @histogram, previous_histogram: @previous_histogram %>
3
3
  <%= render "/active_analytics/sites/histogram", histogram: @histogram %>
4
4
  </section>
5
5
 
@@ -1,9 +1,12 @@
1
1
  <section>
2
- <h2><%= page_from_params %></h2>
2
+ <h2>
3
+ <%= page_from_params %>
4
+ <%= link_to "", File.join("https://", params[:site], page_from_params), target: "_blank" %>
5
+ </h2>
3
6
  </section>
4
7
 
5
8
  <section class="card">
6
- <h3><%= format_view_count @histogram.total %> views</h3>
9
+ <%= render "/active_analytics/sites/histogram_header", histogram: @histogram, previous_histogram: @previous_histogram %>
7
10
  <%= render "/active_analytics/sites/histogram", histogram: @histogram %>
8
11
  </section>
9
12
 
@@ -11,7 +14,7 @@
11
14
  <li class="card">
12
15
  <section>
13
16
  <h3>Previous page</h3>
14
- <%= render "table", pages: @previous_pages %>
17
+ <%= render "/active_analytics/referrers/table", referrers: @previous_pages %>
15
18
  </section>
16
19
  </li>
17
20
 
@@ -19,4 +22,4 @@
19
22
  <h3>Next page</h3>
20
23
  <%= render "table", pages: @next_pages %>
21
24
  </li>
22
- </ul>
25
+ </ul>
@@ -7,11 +7,18 @@
7
7
  <% for referrer in referrers %>
8
8
  <tr>
9
9
  <td>
10
- <% if referrer.host %>
10
+ <% if referrer.try(:path) %>
11
+ <%= site_icon referrer.host %>
12
+ <% if referrer.host == params[:site] %>
13
+ <%= link_to referrer.path, page_path(site: referrer.host, page: page_to_params(referrer.path), from: params[:from], to: params[:to]) %>
14
+ <% else %>
15
+ <%= link_to referrer.url, referrer_path(site: params[:site], referrer: referrer.url.chomp("/"), from: params[:from], to: params[:to]) %>
16
+ <% end %>
17
+ <% elsif referrer.host %>
11
18
  <%= site_icon referrer.host %>
12
19
  <%= link_to referrer.host, referrer_path(site: params[:site], referrer: referrer.host, from: params[:from], to: params[:to]) %>
13
20
  <% else %>
14
- <%= referrer.host %>
21
+ (None or direct)
15
22
  <% end %>
16
23
  </td>
17
24
  <td class="number"><%= format_view_count referrer.total %></td>
@@ -1,9 +1,9 @@
1
1
  <section class="card">
2
- <h3><%= format_view_count @histogram.total %> views</h3>
2
+ <%= render "/active_analytics/sites/histogram_header", histogram: @histogram, previous_histogram: @previous_histogram %>
3
3
  <%= render "/active_analytics/sites/histogram", histogram: @histogram %>
4
4
  </section>
5
5
 
6
6
  <section class="card">
7
7
  <h3>Top sources</h3>
8
8
  <%= render "table", referrers: @referrers %>
9
- </section>
9
+ </section>
@@ -1,15 +1,18 @@
1
1
  <section>
2
- <h2>Source: <%= params[:referrer] %></h2>
2
+ <h2>
3
+ Source: <%= params[:referrer] %>
4
+ <%= link_to "", File.join("https://", params[:referrer]), target: "_blank" %>
5
+ </h2>
3
6
  </section>
4
7
 
5
8
  <section class="card">
6
- <h3><%= format_view_count @histogram.total %> views</h3>
9
+ <%= render "/active_analytics/sites/histogram_header", histogram: @histogram, previous_histogram: @previous_histogram %>
7
10
  <%= render "/active_analytics/sites/histogram", histogram: @histogram %>
8
11
  </section>
9
12
  <ul class="grid-auto">
10
13
  <li class="card">
11
14
  <h3>Source page</h3>
12
- <%= render "/active_analytics/pages/table", pages: @previous_pages %>
15
+ <%= render "/active_analytics/referrers/table", referrers: @previous_pages %>
13
16
  </li>
14
17
 
15
18
  <li class="card">
@@ -3,11 +3,18 @@
3
3
  <tbody>
4
4
  <% for bar in histogram.bars %>
5
5
  <tr>
6
- <th scope="row"><%= bar.label.day %></td>
7
- <td style="--size: <%= bar.height %>; --color: rgba(var(--color-grey-100), 1);">
6
+ <th scope="row">
7
+ <% if bar.height > 0 %>
8
+ <%= link_to bar.label.day, url_for(params.merge(from: bar.label.to_date, to: bar.label.to_date).to_unsafe_hash) %>
9
+ <% else %>
10
+ <%= bar.label.day %>
11
+ <% end %>
12
+ </th>
13
+ <td style="--size: <%= bar.height %>; --color: rgba(var(--color-grey-100), 1);">
8
14
  <span class="data"><%= bar.value.to_i %></span>
9
15
  <span class="tooltip"><small class="tooltip-date"><%= l bar.label, format: :long%></small><br/><%= format_view_count bar.value %> views</span>
10
16
  </td>
17
+ </a>
11
18
  </tr>
12
19
  <% end %>
13
20
  </tbody>
@@ -0,0 +1,10 @@
1
+ <h3>
2
+ <%= format_view_count current_total = histogram.total %> views
3
+ <% if (previous_total = previous_histogram.total) > 0 %>
4
+ <% if (diff = current_total - previous_total) > 0 %>
5
+ &nbsp;<small class="is-success">+<%= number_to_percentage diff * 100.0 / previous_total, precision: 0 %></small>
6
+ <% elsif (diff = current_total - previous_total) < 0 %>
7
+ &nbsp;<small class="is-danger"><%= number_to_percentage diff * 100.0 / previous_total, precision: 0 %></small>
8
+ <% end %>
9
+ <% end %>
10
+ </h3>
@@ -1,5 +1,5 @@
1
1
  <section class="card">
2
- <h3><%= format_view_count @histogram.total %> views</h3>
2
+ <%= render "/active_analytics/sites/histogram_header", histogram: @histogram, previous_histogram: @previous_histogram %>
3
3
  <%= render "/active_analytics/sites/histogram", histogram: @histogram %>
4
4
  </section>
5
5
 
@@ -13,4 +13,4 @@
13
13
  <h3>Top pages <%= link_to "view all", pages_path(from: params[:from], to: params[:to]) %></h3>
14
14
  <%= render "/active_analytics/pages/table", pages: @pages %>
15
15
  </li>
16
- </ul>
16
+ </ul>
@@ -37,12 +37,12 @@
37
37
  <header id="custom-range-title">Custom range</header>
38
38
  <%= form_tag url_for(params.except(:from, :to).to_unsafe_hash), method: "get", id: "dateRangeForm" do %>
39
39
  <div role="group" class="is-block">
40
- <label>From <%= text_field_tag :from, params[:from] %></label>
41
- <label>To <%= text_field_tag :to, params[:to] %></label>
40
+ <label>From <%= date_field_tag :from, params[:from] %></label>
41
+ <label>To <%= date_field_tag :to, params[:to] %></label>
42
42
  </div>
43
43
  <% end %>
44
44
  <footer>
45
45
  <%= submit_tag "Change date range", form: "dateRangeForm" %>
46
46
  <button type="reset" onclick="Ariato.Dialog.close(this)">Close</button>
47
47
  </footer>
48
- </div>
48
+ </div>
@@ -1,7 +1,5 @@
1
1
  <header data-ariato="ActiveAnalytics.Header">
2
- <nav aria-label="site"> <!-- implicit role="navigation", aria label "site" is announced = "site navigation" -->
3
-
4
- <%#= link_to "Analytics", active_analytics_path, role: "button" %>
2
+ <nav aria-label="site">
5
3
  <%= link_to active_analytics_path, class: 'logo' do %>
6
4
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 11 11">
7
5
  <path class="a" d="M.5,5.5s0,2,1,2,1-2,1-2,0-2,1-2,1,2,1,2,0,2,1,2,1-2,1-2,0-2,1-2,1,2,1,2,0,2,1,2,1-2,1-2"/>
@@ -1,13 +1,15 @@
1
1
  <!DOCTYPE html>
2
- <html>
2
+ <html class="active-analytics">
3
3
  <head>
4
4
  <title>Active analytics</title>
5
5
  <meta name=viewport content="width=device-width, initial-scale=1">
6
6
  <%= csrf_meta_tags %>
7
7
  <%= csp_meta_tag %>
8
8
 
9
- <%= stylesheet_link_tag "active_analytics/application", media: "all" %>
10
- <%= javascript_include_tag "active_analytics/application" %>
9
+ <%= tag.link rel: "stylesheet", href: asset_path(:ariato, format: :css) %>
10
+ <%= tag.link rel: "stylesheet", href: asset_path(:application, format: :css) %>
11
+ <%= tag.script "", src: asset_path(:ariato, format: :js) %>
12
+ <%= tag.script "", src: asset_path(:application, format: :js) %>
11
13
  </head>
12
14
 
13
15
  <body>
data/config/routes.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  ActiveAnalytics::Engine.routes.draw do
2
+ resources :assets, only: [:show]
2
3
  get "/:site", to: "sites#show", as: :site, constraints: {site: /[^\/]+/}
3
4
 
4
5
  # Referrers
5
6
  get "/:site/referrers", to: "referrers#index", constraints: {site: /[^\/]+/}, as: :referrers
6
- get "/:site/referrers/:referrer", to: "referrers#show", constraints: {site: /[^\/]+/, referrer: /[^\/]+/}, as: :referrer
7
+ get "/:site/referrers/*referrer", to: "referrers#show", as: :referrer, constraints: {site: /[^\/]+/, referrer: /.+/}
7
8
 
8
9
  # Pages
9
10
  get "/:site/pages", to: "pages#index", constraints: {site: /[^\/]+/}, as: :pages
@@ -1,9 +1,5 @@
1
1
  module ActiveAnalytics
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace ActiveAnalytics
4
-
5
- initializer "active_analytics.assets.precompile" do |app|
6
- app.config.assets.precompile += %w[active_analytics/application.js active_analytics/application.css]
7
- end
8
4
  end
9
5
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveAnalytics
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -2,6 +2,22 @@ require "active_analytics/version"
2
2
  require "active_analytics/engine"
3
3
 
4
4
  module ActiveAnalytics
5
+ def self.redis_url=(string)
6
+ @redis_url = string
7
+ end
8
+
9
+ def self.redis_url
10
+ @redis_url ||= ENV["ACTIVE_ANALYTICS_REDIS_URL"] || ENV["REDIS_URL"] || "redis://localhost"
11
+ end
12
+
13
+ def self.redis=(connection)
14
+ @redis = connection
15
+ end
16
+
17
+ def self.redis
18
+ @redis ||= Redis.new(url: redis_url)
19
+ end
20
+
5
21
  def self.record_request(request)
6
22
  params = {
7
23
  site: request.host,
@@ -9,9 +25,7 @@ module ActiveAnalytics
9
25
  date: Date.today,
10
26
  }
11
27
  if request.referrer.present?
12
- referrer_uri = URI(request.referrer)
13
- params[:referrer_host] = referrer_uri.host
14
- params[:referrer_path] = referrer_uri.path
28
+ params[:referrer_host], params[:referrer_path] = ViewsPerDay.split_referrer(request.referrer)
15
29
  end
16
30
  ViewsPerDay.append(params)
17
31
  rescue => ex
@@ -22,4 +36,28 @@ module ActiveAnalytics
22
36
  Rails.logger.error(ex.backtrace.join("\n"))
23
37
  end
24
38
  end
39
+
40
+ SEPARATOR = "|"
41
+ QUEUE = "ActiveAnalytics::Queue"
42
+ OLD_QUEUE = "ActiveAnalytics::OldQueue"
43
+
44
+ def self.queue_request(request)
45
+ keys = [request.host, request.path]
46
+ if request.referrer.present?
47
+ keys.concat(ViewsPerDay.split_referrer(request.referrer))
48
+ end
49
+ redis.hincrby(QUEUE, keys.join(SEPARATOR).downcase, 1)
50
+ end
51
+
52
+ def self.flush_queue
53
+ return if !redis.exists?(QUEUE)
54
+ cursor = 0
55
+ date = Date.today
56
+ redis.rename(QUEUE, OLD_QUEUE)
57
+ redis.hscan_each(OLD_QUEUE) do |key, count|
58
+ site, page, referrer_host, referrer_path = key.split(SEPARATOR)
59
+ ViewsPerDay.append(date: date, site: site, page: page, referrer_host: referrer_host, referrer_path: referrer_path, total: count.to_i)
60
+ end
61
+ redis.del(OLD_QUEUE)
62
+ end
25
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-09-25 00:00:00.000000000 Z
12
+ date: 2023-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -39,13 +39,8 @@ files:
39
39
  - app/assets/config/active_analytics_manifest.js
40
40
  - app/assets/images/active_analytics.png
41
41
  - app/assets/images/active_analytics_screenshot.png
42
- - app/assets/javascripts/active_analytics/application.js
43
- - app/assets/javascripts/active_analytics/ariato.js
44
- - app/assets/stylesheets/active_analytics/application.css
45
- - app/assets/stylesheets/active_analytics/ariato.css
46
- - app/assets/stylesheets/active_analytics/charts.css
47
- - app/assets/stylesheets/active_analytics/style.css
48
42
  - app/controllers/active_analytics/application_controller.rb
43
+ - app/controllers/active_analytics/assets_controller.rb
49
44
  - app/controllers/active_analytics/pages_controller.rb
50
45
  - app/controllers/active_analytics/referrers_controller.rb
51
46
  - app/controllers/active_analytics/sites_controller.rb
@@ -57,6 +52,12 @@ files:
57
52
  - app/mailers/active_analytics/application_mailer.rb
58
53
  - app/models/active_analytics/application_record.rb
59
54
  - app/models/active_analytics/views_per_day.rb
55
+ - app/views/active_analytics/assets/_charts.css
56
+ - app/views/active_analytics/assets/_style.css
57
+ - app/views/active_analytics/assets/application.css.erb
58
+ - app/views/active_analytics/assets/application.js
59
+ - app/views/active_analytics/assets/ariato.css
60
+ - app/views/active_analytics/assets/ariato.js
60
61
  - app/views/active_analytics/pages/_table.html.erb
61
62
  - app/views/active_analytics/pages/index.html.erb
62
63
  - app/views/active_analytics/pages/show.html.erb
@@ -64,6 +65,7 @@ files:
64
65
  - app/views/active_analytics/referrers/index.html.erb
65
66
  - app/views/active_analytics/referrers/show.html.erb
66
67
  - app/views/active_analytics/sites/_histogram.html.erb
68
+ - app/views/active_analytics/sites/_histogram_header.html.erb
67
69
  - app/views/active_analytics/sites/index.html.erb
68
70
  - app/views/active_analytics/sites/show.html.erb
69
71
  - app/views/layouts/active_analytics/_footer.html.erb
@@ -98,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
100
  - !ruby/object:Gem::Version
99
101
  version: '0'
100
102
  requirements: []
101
- rubygems_version: 3.1.2
103
+ rubygems_version: 3.2.22
102
104
  signing_key:
103
105
  specification_version: 4
104
106
  summary: First-party, privacy-focused traffic analytics for Ruby on Rails applications
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */