action_trace 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -0
- data/app/controllers/action_trace/application_controller.rb +13 -0
- data/lib/action_trace/engine.rb +0 -4
- data/lib/action_trace/version.rb +1 -1
- data/lib/generators/action_trace/install/POST_INSTALL +13 -1
- data/lib/generators/action_trace/install/install_generator.rb +31 -1
- data/lib/generators/action_trace/install/templates/action_trace.js.tt +14 -0
- data/lib/generators/action_trace/install/templates/initializers/action_trace.rb.tt +0 -15
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 182fd04b0f3e966ecc924f62325b816114084b5fa6fc4b726718421f0b0f8b40
|
|
4
|
+
data.tar.gz: f86112724bb6802802e7e29491655f40365103c07fcc904647ea939870af0721
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6a073a53162b28c8483a37bfb4270fbb085ce2880f27e8bdca5fc2a5d03c3b2abf77e8b53b01711c2f98c56d5f7d3ebb9333c38d042fe7663036c4012475240a
|
|
7
|
+
data.tar.gz: 83de77162cf7b55ad76a002757e11daa334066191f2b8228e922c32515015fe91b9d76cb8fba57c76f1ecfc532d1d605a89674dd5dc71b51ecc87220fd0dff42
|
data/README.md
CHANGED
|
@@ -108,6 +108,26 @@ end
|
|
|
108
108
|
|
|
109
109
|
For soft-delete tracking with discard, add `include Discard::Model` alongside `DataTrackable`. The `data_destroy` event is still recorded via the `before_destroy` callback.
|
|
110
110
|
|
|
111
|
+
### JavaScript — client-side page visit tracking
|
|
112
|
+
|
|
113
|
+
The installer copies `app/javascript/action_trace.js` and imports it from `application.js`. The script reads data attributes from the `<body>` tag on every Turbo page load and fires an Ahoy `page_visit` event.
|
|
114
|
+
|
|
115
|
+
You must add those attributes to your layout manually:
|
|
116
|
+
|
|
117
|
+
```erb
|
|
118
|
+
<body
|
|
119
|
+
data-track-controller="<%= controller_name %>"
|
|
120
|
+
data-track-action="<%= action_name %>"
|
|
121
|
+
data-track-company-id="<%= current_user.company_id %>"
|
|
122
|
+
data-track-method="<%= request.method %>">
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
If the `data-track-controller` attribute is absent the script exits early and no event is sent, so pages where the user is not logged in (and `current_user` is nil) are safe as long as you omit the attributes conditionally:
|
|
126
|
+
|
|
127
|
+
```erb
|
|
128
|
+
<body <%= current_user ? %(data-track-controller="#{controller_name}" data-track-action="#{action_name}" data-track-company-id="#{current_user.company_id}" data-track-method="#{request.method}") : "" %>>
|
|
129
|
+
```
|
|
130
|
+
|
|
111
131
|
### Controllers — tracking page visits and sessions
|
|
112
132
|
|
|
113
133
|
Include `ActivityTrackable` in any controller (or `ApplicationController`) to track page visits:
|
|
@@ -2,5 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
module ActionTrace
|
|
4
4
|
class ApplicationController < ::ApplicationController
|
|
5
|
+
helper do
|
|
6
|
+
def method_missing(method_name, *args, &block)
|
|
7
|
+
if method_name.to_s.end_with?('_path', '_url') && main_app.respond_to?(method_name)
|
|
8
|
+
main_app.public_send(method_name, *args, &block)
|
|
9
|
+
else
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
15
|
+
(method_name.to_s.end_with?('_path', '_url') && main_app.respond_to?(method_name)) || super
|
|
16
|
+
end
|
|
17
|
+
end
|
|
5
18
|
end
|
|
6
19
|
end
|
data/lib/action_trace/engine.rb
CHANGED
|
@@ -12,10 +12,6 @@ module ActionTrace
|
|
|
12
12
|
g.test_framework :rspec
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
config.to_prepare do
|
|
16
|
-
ActionTrace::ApplicationController.helper Rails.application.routes.url_helpers
|
|
17
|
-
end
|
|
18
|
-
|
|
19
15
|
config.after_initialize do
|
|
20
16
|
begin
|
|
21
17
|
user_class = ActionTrace.configuration.user_class.constantize
|
data/lib/action_trace/version.rb
CHANGED
|
@@ -21,7 +21,19 @@
|
|
|
21
21
|
|
|
22
22
|
include ActionTrace::DataTrackable
|
|
23
23
|
|
|
24
|
-
5.
|
|
24
|
+
5. Add data attributes to your layout's <body> tag so the JavaScript
|
|
25
|
+
tracker can read the current controller, action, company and HTTP method:
|
|
26
|
+
|
|
27
|
+
<body
|
|
28
|
+
data-track-controller="<%= controller_name %>"
|
|
29
|
+
data-track-action="<%= action_name %>"
|
|
30
|
+
data-track-company-id="<%= current_user.company_id %>"
|
|
31
|
+
data-track-method="<%= request.method %>">
|
|
32
|
+
|
|
33
|
+
The generated app/javascript/action_trace.js reads these attributes on
|
|
34
|
+
every Turbo page load and sends a page_visit event via Ahoy.
|
|
35
|
+
|
|
36
|
+
6. (Optional) Customize views:
|
|
25
37
|
|
|
26
38
|
rails generate action_trace:views
|
|
27
39
|
rails generate action_trace:views --controller
|
|
@@ -29,7 +29,10 @@ module ActionTrace
|
|
|
29
29
|
desc: 'Skip discard generator (already installed)'
|
|
30
30
|
|
|
31
31
|
def run_ahoy_install
|
|
32
|
-
|
|
32
|
+
unless options[:skip_ahoy]
|
|
33
|
+
generate 'ahoy:install'
|
|
34
|
+
patch_ahoy_initializer
|
|
35
|
+
end
|
|
33
36
|
inject_ahoy_filter_by_company
|
|
34
37
|
end
|
|
35
38
|
|
|
@@ -52,8 +55,35 @@ module ActionTrace
|
|
|
52
55
|
template 'initializers/action_trace.rb.tt', 'config/initializers/action_trace.rb'
|
|
53
56
|
end
|
|
54
57
|
|
|
58
|
+
def pin_ahoy_for_importmap
|
|
59
|
+
return if options[:skip_ahoy]
|
|
60
|
+
return unless File.exist?('config/importmap.rb')
|
|
61
|
+
|
|
62
|
+
append_to_file 'config/importmap.rb', %(pin "ahoy", to: "ahoy.js"\n)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def import_ahoy_in_javascript
|
|
66
|
+
return if options[:skip_ahoy]
|
|
67
|
+
return unless File.exist?('app/javascript/application.js')
|
|
68
|
+
|
|
69
|
+
append_to_file 'app/javascript/application.js', %(import "ahoy"\n)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def create_javascript_tracking_file
|
|
73
|
+
return if options[:skip_ahoy]
|
|
74
|
+
return unless File.exist?('app/javascript')
|
|
75
|
+
|
|
76
|
+
template 'action_trace.js.tt', 'app/javascript/action_trace.js'
|
|
77
|
+
append_to_file 'app/javascript/application.js', %(import "./action_trace"\n) if File.exist?('app/javascript/application.js')
|
|
78
|
+
end
|
|
79
|
+
|
|
55
80
|
private
|
|
56
81
|
|
|
82
|
+
def patch_ahoy_initializer
|
|
83
|
+
gsub_file 'config/initializers/ahoy.rb', 'Ahoy.api = false', 'Ahoy.api = true'
|
|
84
|
+
append_to_file 'config/initializers/ahoy.rb', "\nAhoy.server_side_visits = false\n"
|
|
85
|
+
end
|
|
86
|
+
|
|
57
87
|
def inject_ahoy_filter_by_company
|
|
58
88
|
filter_method = <<~RUBY
|
|
59
89
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "ahoy"
|
|
2
|
+
|
|
3
|
+
document.addEventListener('turbo:load', () => {
|
|
4
|
+
const { trackController, trackAction, trackCompanyId, trackMethod } = document.body.dataset
|
|
5
|
+
if (!trackController) return
|
|
6
|
+
|
|
7
|
+
ahoy.track('page_visit', {
|
|
8
|
+
path: window.location.pathname,
|
|
9
|
+
controller: trackController,
|
|
10
|
+
action: trackAction,
|
|
11
|
+
company_id: trackCompanyId,
|
|
12
|
+
method: trackMethod
|
|
13
|
+
})
|
|
14
|
+
})
|
|
@@ -5,21 +5,6 @@ ActionTrace.configure do |config|
|
|
|
5
5
|
# config.log_retention_period = 1.year # default: 1.year
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
class Ahoy::Store < Ahoy::DatabaseStore
|
|
9
|
-
def track_visit(data)
|
|
10
|
-
super(data)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def track_event(data)
|
|
14
|
-
super(data)
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
Ahoy.api = false
|
|
19
|
-
Ahoy.geocode = false
|
|
20
|
-
Ahoy.mask_ips = true
|
|
21
|
-
Ahoy.cookies = true
|
|
22
|
-
|
|
23
8
|
PaperTrail.config do |config|
|
|
24
9
|
config.enabled = true
|
|
25
10
|
config.version_limit = 10
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: action_trace
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- gimbaro
|
|
@@ -265,6 +265,7 @@ files:
|
|
|
265
265
|
- lib/action_trace/version.rb
|
|
266
266
|
- lib/generators/action_trace/install/POST_INSTALL
|
|
267
267
|
- lib/generators/action_trace/install/install_generator.rb
|
|
268
|
+
- lib/generators/action_trace/install/templates/action_trace.js.tt
|
|
268
269
|
- lib/generators/action_trace/install/templates/initializers/action_trace.rb.tt
|
|
269
270
|
- lib/generators/action_trace/install/templates/migrations/add_version_id_to_activities.rb.tt
|
|
270
271
|
- lib/generators/action_trace/views/views_generator.rb
|