This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.rb
119 lines (106 loc) · 2.88 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# name: Segment.io
# about: Import your Discourse data to your Segment.io warehouse
# version: 0.0.1
# authors: Kyle Welsby <kyle@mekyle.com>
gem 'commander', '4.4.0' # , require: false # for analytics-ruby
gem 'analytics-ruby', '2.2.2', require: false # 'segment/analytics'
after_initialize do
require 'segment/analytics'
SEGMENT_IO_KEY = ENV['SEGMENT_IO_KEY']
unless SEGMENT_IO_KEY
raise StandardError, 'Segment.io WriteKey must be defined as an environment variable `export SEGMENT_IO_KEY={YOUR_KEY_HERE}`'
end
Analytics = Segment::Analytics.new(
write_key: SEGMENT_IO_KEY,
on_error: proc { |_status, msg| print msg }
)
require_dependency 'jobs/base'
module ::Jobs
class EmitSegmentUserIdentify < Jobs::Base
def execute(args)
user = User.find_by_id(args[:user_id])
user.emit_segment_user_identify if user
end
end
end
User.find_each do |user|
Jobs.enqueue(:emit_segment_user_identify, user_id: user.id)
end
require_dependency 'user'
class ::User
after_create :emit_segment_user_identify
after_create :emit_segment_user_created
def emit_segment_user_identify
Analytics.identify(
user_id: id,
traits: {
name: name,
username: username,
email: email,
created_at: created_at
},
context: {
ip: ip_address
}
)
end
def emit_segment_user_created
Analytics.track(
user_id: id,
event: 'Signed Up'
)
end
end
require_dependency 'application_controller'
class ::ApplicationController
before_filter :emit_segment_user_tracker
def emit_segment_user_tracker
if current_user && !segment_common_controller_actions?
Analytics.page(
user_id: current_user.id,
name: "#{controller_name}##{action_name}",
properties: {
url: request.original_url
},
context: {
ip: request.ip,
userAgent: request.user_agent
}
)
end
end
def segment_common_controller_actions?
controller_name == 'stylesheets' || controller_name == 'user_avatars' || (controller_name == 'about' && action_name == 'live_post_counts')
end
end
require_dependency 'post'
class ::Post
after_create :emit_segment_post_created
def emit_segment_post_created
Analytics.track(
user_id: user_id,
event: 'Post Created',
properties: {
slug: topic.slug,
title: topic.title,
url: topic.url
}
)
end
end
require_dependency 'topic'
class ::Topic
after_create :emit_segment_topic_created
def emit_segment_topic_created
Analytics.track(
user_id: user_id,
event: 'Topic Created',
properties: {
slug: slug,
title: title,
url: url
}
)
end
end
end