analytic 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,25 +3,31 @@
3
3
  module Analytic
4
4
  module Trackable
5
5
  # @param params [Hash]
6
- def analytic_track!(params: {})
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
- host: request.host,
12
- path: request.path,
13
- params:
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!(params: {})
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
- host: request.host,
23
- path: request.path,
24
- params:
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
@@ -7,6 +7,7 @@ module Analytic
7
7
  validates :session_id, presence: true
8
8
  validates :path, presence: true
9
9
  validates :host, presence: true
10
+ validates :ip, presence: true
10
11
 
11
12
  scope :within, ->(range) { where(timestamp: range) if range }
12
13
 
data/bin/rails CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- ENGINE_ROOT = File.expand_path("..", __dir__)
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
- ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
8
- require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
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
- require "rails/all"
11
- require "rails/engine/commands"
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
@@ -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
@@ -3,5 +3,9 @@
3
3
  module Analytic
4
4
  class Engine < ::Rails::Engine
5
5
  isolate_namespace Analytic
6
+
7
+ config.generators do |g|
8
+ g.test_framework :rspec
9
+ end
6
10
  end
7
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Analytic
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: analytic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre