rails_local_analytics 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbb7ca6534efc3666a302a04271092906ec9b2fc65a316d82b7811e2dd962668
4
- data.tar.gz: 2cc17d06cf61d6b4b7fd2c559079965fa6c250b12d901b05a3317389e2d5b69d
3
+ metadata.gz: f0a0bf463e77ac16f76c751e5664ae847146c8e2e7b2fbbef76093512d966af1
4
+ data.tar.gz: d5c9028dcf9cc3307bb39441f107a0a07afa8f115c013b0edc9512d9ccc0d7e8
5
5
  SHA512:
6
- metadata.gz: 8d9e8dea022bb79486a8bc6395106841a4f9d056accf68f3534072bac1e205f40df426664ed071abd54715d38b0a00ef879aaa080b4a0a51f6ba3e31b207101e
7
- data.tar.gz: e249aa1fb3f83ccaf75ffdb774192de6549889b8fb38b3537f5a99cc5edc6aab7da7d1b01caccb6012ffa4c4695739f2a15107ecbc49804db9e3e61c856ea13f
6
+ metadata.gz: faa7bf7a4c0ee569aae5873e93358cea48ff0060b9caf14c9b7516390a71698bd42b2308233b87438fce0d85d3804990ee0254bb66090024394caf674c5e0f79
7
+ data.tar.gz: 71662ed4b26b97d416d9dd8341ccc3576f0e6725af6ece920adef1727fd276871cac65023d8d5d2279d734ce3ee6a6d2f32c0e2c7f75b92856d157d4fe2c8112
data/README.md CHANGED
@@ -73,6 +73,7 @@ class CreateAnalyticsTables < ActiveRecord::Migration[6.0]
73
73
  end
74
74
  end
75
75
  ```
76
+ The reason we store our anayltics in two separate tables to keep the cardinality of our data low. You may use only a single table but I recommend you try the 2 table approach first and see if it meets your needs. See the performance optimization section for more details.
76
77
 
77
78
  Add the route for the analytics dashboard at the desired endpoint:
78
79
 
@@ -81,11 +82,11 @@ Add the route for the analytics dashboard at the desired endpoint:
81
82
  mount RailsLocalAnalytics::Engine, at: "/admin/analytics"
82
83
  ```
83
84
 
84
- Its generally recomended to use a background job (especially since we now have [`solid_queue`](https://github.com/rails/solid_queue/)). If you would like to disable background jobs you can use the following config:
85
+ Its generally recomended to use a background job (especially since we now have [`solid_queue`](https://github.com/rails/solid_queue/)). If you would like to disable background jobs you can use the following config option:
85
86
 
86
87
  ```ruby
87
88
  # config/initializers/rails_local_analytics.rb
88
- RailsLocalAnalytics.config.background_jobs = false # defaults to true
89
+ # RailsLocalAnalytics.config.background_jobs = false # defaults to true
89
90
  ```
90
91
 
91
92
  The next step is to collect traffic.
@@ -157,6 +158,7 @@ There are a few techniques that you can use to tailor the database for your part
157
158
  * Consider just storing "local" or nil instead if the request originated from your website
158
159
  - `platform` and `browser_engine` columns
159
160
  * Consider dropping either of these if you do not need this information
161
+ - If you want to store everything in one table (which I dont think most people actually need) then you can simply only create one table (I recommend `tracked_requests_by_day_page`) with all of the fields from both tables. This gem will automatically populate all the same fields. You should NOT need to use `:custom_attributes` in this scenario.
160
162
 
161
163
  ## Usage where a request object is not available
162
164
 
File without changes
@@ -3,6 +3,7 @@ module RailsLocalAnalytics
3
3
  PER_PAGE_LIMIT = 1000
4
4
 
5
5
  helper_method :pagination_page_number
6
+ helper_method :display_columns
6
7
 
7
8
  def index
8
9
  params[:type] ||= "page"
@@ -17,17 +18,13 @@ module RailsLocalAnalytics
17
18
  return
18
19
  end
19
20
 
20
- if params[:group_by].present? && !@klass.display_columns.include?(params[:group_by])
21
- raise ArgumentError
22
- end
23
-
24
21
  if params[:start_date].present?
25
22
  @start_date = Date.parse(params[:start_date])
26
23
  else
27
24
  @start_date = Date.today
28
25
  end
29
26
 
30
- if params[:end_date]
27
+ if params[:end_date].present?
31
28
  @end_date = Date.parse(params[:end_date])
32
29
  else
33
30
  @end_date = Date.today
@@ -63,20 +60,18 @@ module RailsLocalAnalytics
63
60
 
64
61
  prev_start_date, prev_end_date = get_prev_dates(start_date, end_date)
65
62
 
66
- where_conditions = params.require(:conditions).permit(*@klass.display_columns)
63
+ difference_where_conditions = params.require(:conditions).permit(*display_columns)
67
64
 
68
65
  current_total = fetch_records(
69
66
  start_date,
70
67
  end_date,
71
- where_conditions: where_conditions,
72
- pluck_columns: ["SUM(total)"],
68
+ difference_where_conditions: difference_where_conditions,
73
69
  ).first
74
70
 
75
71
  prev_total = fetch_records(
76
72
  prev_start_date,
77
73
  prev_end_date,
78
- where_conditions: where_conditions,
79
- pluck_columns: ["SUM(total)"],
74
+ difference_where_conditions: difference_where_conditions,
80
75
  ).first
81
76
 
82
77
  if prev_total
@@ -90,13 +85,17 @@ module RailsLocalAnalytics
90
85
 
91
86
  private
92
87
 
93
- def fetch_records(start_date, end_date, where_conditions: nil, pluck_columns: nil)
88
+ def fetch_records(start_date, end_date, difference_where_conditions: nil)
89
+ aggregate_sql_field = "SUM(total)"
90
+
94
91
  tracked_requests = @klass
95
92
  .where("day >= ?", start_date)
96
93
  .where("day <= ?", end_date)
97
- .order(total: :desc)
94
+ .order("#{aggregate_sql_field} DESC")
98
95
 
99
- if where_conditions.nil? && pluck_columns.nil?
96
+ if difference_where_conditions
97
+ tracked_requests = tracked_requests.where(difference_where_conditions)
98
+ else
100
99
  tracked_requests = tracked_requests
101
100
  .limit(PER_PAGE_LIMIT)
102
101
  .offset(PER_PAGE_LIMIT * (pagination_page_number-1))
@@ -104,7 +103,7 @@ module RailsLocalAnalytics
104
103
  if params[:filter].present?
105
104
  col, val = params[:filter].split("==")
106
105
 
107
- if @klass.display_columns.include?(col)
106
+ if display_columns.include?(col)
108
107
  tracked_requests = tracked_requests.where(col => val)
109
108
  else
110
109
  raise ArgumentError
@@ -112,24 +111,43 @@ module RailsLocalAnalytics
112
111
  end
113
112
  end
114
113
 
115
- if where_conditions
116
- tracked_requests = tracked_requests.where(where_conditions)
117
- end
118
-
119
114
  if params[:search].present?
120
115
  tracked_requests = tracked_requests.multi_search(params[:search])
121
116
  end
122
117
 
123
- if params[:group_by].present?
124
- group_by_columns = [params[:group_by]]
125
- pluck_columns = [params[:group_by], "SUM(total)"]
118
+ if params[:group_by].blank?
119
+ pluck_columns = display_columns.dup
126
120
  else
127
- group_by_columns = @klass.display_columns
128
- pluck_columns ||= @klass.display_columns + ["SUM(total)"]
121
+ case params[:group_by]
122
+ when "url_hostname_and_path"
123
+ if display_columns.include?("url_hostname") && display_columns.include?("url_path")
124
+ pluck_columns = [:url_hostname, :url_path]
125
+ else
126
+ raise ArgumentError
127
+ end
128
+ when "referrer_hostname_and_path"
129
+ if display_columns.include?("referrer_hostname") && display_columns.include?("referrer_path")
130
+ pluck_columns = [:referrer_hostname, :referrer_path]
131
+ else
132
+ raise ArgumentError
133
+ end
134
+ when *display_columns
135
+ pluck_columns = [params[:group_by]]
136
+ else
137
+ raise ArgumentError
138
+ end
139
+ end
140
+
141
+ group_by = pluck_columns.dup
142
+
143
+ if difference_where_conditions
144
+ pluck_columns = [aggregate_sql_field]
145
+ else
146
+ pluck_columns << aggregate_sql_field
129
147
  end
130
148
 
131
149
  tracked_requests
132
- .group(*group_by_columns)
150
+ .group(*group_by)
133
151
  .pluck(*pluck_columns)
134
152
  end
135
153
 
@@ -151,5 +169,9 @@ module RailsLocalAnalytics
151
169
  return [prev_start_date, prev_end_date]
152
170
  end
153
171
 
172
+ def display_columns
173
+ @display_columns ||= @klass.display_columns
174
+ end
175
+
154
176
  end
155
177
  end
@@ -1,9 +1,41 @@
1
- <% data_columns = params[:group_by].present? ? [params[:group_by]] : @klass.display_columns %>
1
+ <%
2
+ data_columns = params[:group_by].present? ? [params[:group_by]] : display_columns
3
+
4
+ if data_columns.first == "url_hostname_and_path"
5
+ data_columns = ["URL Hostname", "URL Path"]
6
+ elsif data_columns.first == "referrer_hostname_and_path"
7
+ data_columns = ["Referrer Hostname", "Referrer Path"]
8
+ end
9
+ %>
10
+
11
+ <%
12
+ group_by_opts = [
13
+ ["All", nil],
14
+ ]
15
+
16
+ group_by_opts += display_columns.map{|x| [x.titleize.sub("Url ", "URL "), x] }
17
+
18
+ if display_columns.include?("url_hostname") && display_columns.include?("url_path")
19
+ index = group_by_opts.index(["URL Hostname", "url_hostname"])
20
+ group_by_opts.insert(
21
+ index,
22
+ ["URL Hostname and Path", "url_hostname_and_path"],
23
+ )
24
+ end
25
+
26
+ if display_columns.include?("referrer_hostname") && display_columns.include?("referrer_path")
27
+ index = group_by_opts.index(["Referrer Hostname", "referrer_hostname"])
28
+ group_by_opts.insert(
29
+ index,
30
+ ["Referrer Hostname and Path", "referrer_hostname_and_path"],
31
+ )
32
+ end
33
+ %>
2
34
 
3
35
  <div class="well well-sm">
4
36
  <%= form_tag url_for(params.except(:start_date, :to).to_unsafe_hash), method: "get", id: "search-form" do %>
5
37
  <div>
6
- <label style="margin-right: 10px;">Group By: <%= select_tag :group_by, options_for_select([["All", nil]] + @klass.display_columns.map{|x| [x.titleize.sub("Url ", "URL "), x]} , params[:group_by]) %></label>
38
+ <label style="margin-right: 10px;">Group By: <%= select_tag :group_by, options_for_select(group_by_opts, params[:group_by]) %></label>
7
39
 
8
40
  <label style="margin-right: 10px;">From: <%= date_field_tag :start_date, @start_date %></label>
9
41
  <label style="margin-right: 10px;">To: <%= date_field_tag :end_date, @end_date %></label>
@@ -1,3 +1,3 @@
1
1
  module RailsLocalAnalytics
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_local_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-05 00:00:00.000000000 Z
11
+ date: 2024-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-html-matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: database_cleaner
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +106,8 @@ files:
92
106
  - README.md
93
107
  - Rakefile
94
108
  - app/assets/config/rails_local_analytics_manifest.js
109
+ - app/assets/images/rails_local_analytics/.keep
110
+ - app/assets/javascripts/rails_local_analytics/.keep
95
111
  - app/assets/stylesheets/rails_local_analytics/application.css
96
112
  - app/controllers/rails_local_analytics/application_controller.rb
97
113
  - app/controllers/rails_local_analytics/dashboard_controller.rb