6
6
gem 'commander' , '4.4.0' # , require: false # for analytics-ruby
7
7
gem 'analytics-ruby' , '2.2.2' , require : false # 'segment/analytics'
8
8
9
+ enabled_site_setting :segment_io_enabled
10
+
9
11
after_initialize do
10
12
require 'segment/analytics'
11
13
12
- SEGMENT_IO_KEY = ENV [ 'SEGMENT_IO_KEY' ]
13
- unless SEGMENT_IO_KEY
14
- raise StandardError , 'Segment.io WriteKey must be defined as an environment variable `export SEGMENT_IO_KEY={YOUR_KEY_HERE}`'
14
+ class Analytics
15
+ def self . method_missing ( method , *args )
16
+ return unless SiteSetting . segment_io_enabled
17
+ analytics = Segment ::Analytics . new (
18
+ write_key : SiteSetting . segment_io_write_key
19
+ )
20
+ super ( method , *args ) unless analytics . respond_to? ( method )
21
+ analytics . send ( method , *args )
22
+ analytics . flush
23
+ end
15
24
end
16
- Analytics = Segment ::Analytics . new (
17
- write_key : SEGMENT_IO_KEY ,
18
- on_error : proc { |_status , msg | print msg }
19
- )
20
25
21
26
require_dependency 'jobs/base'
22
27
module ::Jobs
23
28
class EmitSegmentUserIdentify < Jobs ::Base
24
29
def execute ( args )
30
+ return unless SiteSetting . segment_io_enabled?
25
31
user = User . find_by_id ( args [ :user_id ] )
26
32
user . emit_segment_user_identify if user
27
33
end
@@ -36,14 +42,16 @@ def execute(args)
36
42
class ::User
37
43
after_create :emit_segment_user_identify
38
44
after_create :emit_segment_user_created
45
+
39
46
def emit_segment_user_identify
40
47
Analytics . identify (
41
48
user_id : id ,
42
49
traits : {
43
50
name : name ,
44
51
username : username ,
45
52
email : email ,
46
- created_at : created_at
53
+ created_at : created_at ,
54
+ internal : email . ends_with? ( '@pagerduty.com' )
47
55
} ,
48
56
context : {
49
57
ip : ip_address
@@ -62,6 +70,14 @@ def emit_segment_user_created
62
70
require_dependency 'application_controller'
63
71
class ::ApplicationController
64
72
before_filter :emit_segment_user_tracker
73
+
74
+ SEGMENT_IO_EXCLUDES = {
75
+ 'stylesheets' => :all ,
76
+ 'user_avatars' => :all ,
77
+ 'about' => [ 'live_post_counts' ] ,
78
+ 'topics' => [ 'timings' ]
79
+ } . freeze
80
+
65
81
def emit_segment_user_tracker
66
82
if current_user && !segment_common_controller_actions?
67
83
Analytics . page (
@@ -79,7 +95,9 @@ def emit_segment_user_tracker
79
95
end
80
96
81
97
def segment_common_controller_actions?
82
- controller_name == 'stylesheets' || controller_name == 'user_avatars' || ( controller_name == 'about' && action_name == 'live_post_counts' )
98
+ SEGMENT_IO_EXCLUDES . keys . include? ( controller_name ) &&
99
+ ( SEGMENT_IO_EXCLUDES [ controller_name ] == :all ||
100
+ SEGMENT_IO_EXCLUDES [ controller_name ] . include? ( action_name ) )
83
101
end
84
102
end
85
103
@@ -92,9 +110,11 @@ def emit_segment_post_created
92
110
user_id : user_id ,
93
111
event : 'Post Created' ,
94
112
properties : {
95
- slug : topic . slug ,
96
- title : topic . title ,
97
- url : topic . url
113
+ topic_id : topic_id ,
114
+ post_number : post_number ,
115
+ created_at : created_at ,
116
+ since_topic_created : ( created_at - topic . created_at ) . to_i ,
117
+ reply_to_post_number : reply_to_post_number
98
118
}
99
119
)
100
120
end
@@ -116,4 +136,36 @@ def emit_segment_topic_created
116
136
)
117
137
end
118
138
end
139
+
140
+ require_dependency 'topic_tag'
141
+ class ::TopicTag
142
+ after_create :emit_segment_topic_tagged
143
+
144
+ def emit_segment_topic_tagged
145
+ Analytics . track (
146
+ anonymous_id : -1 ,
147
+ event : 'Topic Tag Created' ,
148
+ properties : {
149
+ topic_id : topic_id ,
150
+ tag_name : tag . name
151
+ }
152
+ )
153
+ end
154
+ end
155
+
156
+ require_dependency 'user_action'
157
+ class ::UserAction
158
+ after_create :emit_segment_post_liked , if : -> { self . action_type == UserAction ::LIKE }
159
+
160
+ def emit_segment_post_liked
161
+ Analytics . track (
162
+ user_id : user_id ,
163
+ event : 'Post Liked' ,
164
+ properties : {
165
+ post_id : target_post_id ,
166
+ topic_id : target_topic_id
167
+ }
168
+ )
169
+ end
170
+ end
119
171
end
0 commit comments