action_trace 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50505b302371033a0ac105ce7869c021d6f3a4a5daa94f6cb76a59660b936f9a
4
- data.tar.gz: 75b6e578c378f66b768cf75afd74a1fc64adc42f4fe62a8c7602098b483877f5
3
+ metadata.gz: 182fd04b0f3e966ecc924f62325b816114084b5fa6fc4b726718421f0b0f8b40
4
+ data.tar.gz: f86112724bb6802802e7e29491655f40365103c07fcc904647ea939870af0721
5
5
  SHA512:
6
- metadata.gz: e3f7030e563afeae48d1cc263aacecb0c3b0817d4200e4de8b4a8b8757b414e33c22223b06a10a0faf3d321ec7894939d0530f90e86637665ce8736518a2f760
7
- data.tar.gz: 49ddfef35c96aaf48600f69d7851f3b38aab650bd0f684d9683d6bb967bc6e90aafb2951544d42f8999895bff398308d2d77812f42183cfa94b29cd5e6809c11
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:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionTrace
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -21,7 +21,19 @@
21
21
 
22
22
  include ActionTrace::DataTrackable
23
23
 
24
- 5. (Optional) Customize views:
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
- generate 'ahoy:install' unless options[:skip_ahoy]
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.6.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