analytic 0.2.0 → 0.3.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 +2 -0
- data/app/assets/builds/analytic/application.js +4943 -53662
- data/app/assets/builds/analytic/application.js.map +4 -4
- data/app/controllers/concerns/analytic/trackable.rb +49 -8
- data/app/jobs/analytic/track_job.rb +3 -1
- data/app/models/analytic/view.rb +1 -0
- data/bin/rails +9 -7
- data/db/migrate/20240805210911_create_analytic_views.rb +4 -0
- data/lib/analytic/config.rb +18 -0
- data/lib/analytic/engine.rb +4 -0
- data/lib/analytic/version.rb +1 -1
- metadata +1 -1
@@ -3,25 +3,31 @@
|
|
3
3
|
module Analytic
|
4
4
|
module Trackable
|
5
5
|
# @param params [Hash]
|
6
|
-
def analytic_track!
|
6
|
+
def analytic_track!
|
7
7
|
Analytic::View.create!(
|
8
8
|
timestamp: Time.current,
|
9
9
|
session_id: analytic_session_id,
|
10
10
|
visitor_id: analytic_visitor_id,
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
ip: analytic_ip,
|
12
|
+
host: analytic_host,
|
13
|
+
path: analytic_path,
|
14
|
+
referrer: analytic_referrer,
|
15
|
+
user_agent: request.user_agent,
|
16
|
+
params: analytic_params
|
14
17
|
)
|
15
18
|
end
|
16
19
|
|
17
20
|
# @param params [Hash]
|
18
|
-
def analytic_enqueue_track_job!
|
21
|
+
def analytic_enqueue_track_job!
|
19
22
|
Analytic::TrackJob.perform_later(
|
20
23
|
session_id: analytic_session_id,
|
21
24
|
visitor_id: analytic_visitor_id,
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
ip: analytic_ip,
|
26
|
+
host: analytic_host,
|
27
|
+
path: analytic_path,
|
28
|
+
referrer: analytic_referrer,
|
29
|
+
user_agent: request.user_agent,
|
30
|
+
params: analytic_params
|
25
31
|
)
|
26
32
|
end
|
27
33
|
|
@@ -34,5 +40,40 @@ module Analytic
|
|
34
40
|
def analytic_visitor_id
|
35
41
|
cookies.permanent[:analytic_visitor_id] ||= SecureRandom.uuid
|
36
42
|
end
|
43
|
+
|
44
|
+
# @return [IPAddr]
|
45
|
+
def analytic_ip
|
46
|
+
ip_addr = IPAddr.new(request.remote_ip)
|
47
|
+
|
48
|
+
return ip_addr.mask(Analytic.config.ip_v4_mask) if Analytic.config.ip_v4_mask? && ip_addr.ipv4?
|
49
|
+
return ip_addr.mask(Analytic.config.ip_v6_mask) if Analytic.config.ip_v6_mask? && ip_addr.ipv6?
|
50
|
+
|
51
|
+
ip_addr
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [String]
|
55
|
+
def analytic_host
|
56
|
+
request.host
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [String]
|
60
|
+
def analytic_path
|
61
|
+
request.path
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [String]
|
65
|
+
def analytic_referrer
|
66
|
+
request.referer
|
67
|
+
end
|
68
|
+
|
69
|
+
# @return [String]
|
70
|
+
def analytic_user_agent
|
71
|
+
request.user_agent
|
72
|
+
end
|
73
|
+
|
74
|
+
# @return [Hash]
|
75
|
+
def analytic_params
|
76
|
+
params.permit(:utm_source, :utm_medium, :utm_campaign, :utm_term, :utm_content)
|
77
|
+
end
|
37
78
|
end
|
38
79
|
end
|
@@ -10,12 +10,14 @@ module Analytic
|
|
10
10
|
# @param host [String]
|
11
11
|
# @param path [String]
|
12
12
|
# @param params [Hash]
|
13
|
-
def perform(session_id:, visitor_id:, host:, path:, timestamp: Time.current, params: {})
|
13
|
+
def perform(session_id:, visitor_id:, host:, path:, ip:, timestamp: Time.current, params: {})
|
14
14
|
Analytic::View.create!(
|
15
|
+
timestamp:,
|
15
16
|
session_id:,
|
16
17
|
visitor_id:,
|
17
18
|
host:,
|
18
19
|
path:,
|
20
|
+
ip:,
|
19
21
|
params:
|
20
22
|
)
|
21
23
|
end
|
data/app/models/analytic/view.rb
CHANGED
data/bin/rails
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
ENGINE_PATH = File.expand_path("../lib/analytic/engine", __dir__)
|
5
|
-
APP_PATH = File.expand_path("../spec/dummy/config/application", __dir__)
|
3
|
+
# frozen_string_literal: true
|
6
4
|
|
7
|
-
|
8
|
-
|
5
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
6
|
+
ENGINE_PATH = File.expand_path('../lib/analytic/engine', __dir__)
|
7
|
+
APP_PATH = File.expand_path('../spec/dummy/config/application', __dir__)
|
9
8
|
|
10
|
-
|
11
|
-
require
|
9
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
10
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
11
|
+
|
12
|
+
require 'rails/all'
|
13
|
+
require 'rails/engine/commands'
|
@@ -5,16 +5,20 @@ class CreateAnalyticViews < ActiveRecord::Migration[7.1]
|
|
5
5
|
create_table :analytic_views do |t|
|
6
6
|
t.uuid :visitor_id, null: false
|
7
7
|
t.uuid :session_id, null: false
|
8
|
+
t.inet :ip, null: false
|
8
9
|
t.string :path, null: false
|
9
10
|
t.string :host, null: false
|
10
11
|
t.jsonb :params, null: false, default: {}
|
11
12
|
t.datetime :timestamp, null: false
|
13
|
+
t.text :referrer, null: true
|
14
|
+
t.text :user_agent, null: true
|
12
15
|
|
13
16
|
t.timestamps
|
14
17
|
|
15
18
|
t.index %i[timestamp path]
|
16
19
|
t.index %i[timestamp visitor_id]
|
17
20
|
t.index %i[timestamp session_id]
|
21
|
+
t.index %i[timestamp referrer]
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
data/lib/analytic/config.rb
CHANGED
@@ -5,8 +5,26 @@ module Analytic
|
|
5
5
|
# @return [String]
|
6
6
|
attr_accessor :timezone
|
7
7
|
|
8
|
+
# @return [Integer]
|
9
|
+
attr_accessor :ip_v4_mask
|
10
|
+
|
11
|
+
# @return [Integer]
|
12
|
+
attr_accessor :ip_v6_mask
|
13
|
+
|
8
14
|
def initialize
|
9
15
|
@timezone = Time.zone
|
16
|
+
@ip_v4_mask = 24 # e.g. 255.255.255.255 => '255.255.255.0/255.255.255.0'
|
17
|
+
@ip_v6_mask = 48 # e.g. 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' => 'ffff:ffff:ffff:0000:0000:0000:0000:0000'
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Boolean]
|
21
|
+
def ip_v4_mask?
|
22
|
+
@ip_v4_mask.present?
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Boolean]
|
26
|
+
def ip_v6_mask?
|
27
|
+
@ip_v6_mask.present?
|
10
28
|
end
|
11
29
|
end
|
12
30
|
end
|
data/lib/analytic/engine.rb
CHANGED
data/lib/analytic/version.rb
CHANGED