bg-common 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +5 -219
- data/lib/bg/common/constraints/api_constraint.rb +30 -0
- data/lib/bg/common/version.rb +1 -1
- metadata +4 -21
- data/app/jobs/bg/common/analytics/ga_event_job.rb +0 -15
- data/app/jobs/bg/common/analytics/intercom_create_user_job.rb +0 -17
- data/app/jobs/bg/common/analytics/intercom_event_job.rb +0 -15
- data/app/jobs/bg/common/analytics/intercom_export_users_job.rb +0 -17
- data/app/jobs/bg/common/analytics/intercom_update_users_job.rb +0 -19
- data/app/jobs/bg/common/analytics/keen_event_job.rb +0 -15
- data/lib/bg/common/analytics.rb +0 -64
- data/lib/bg/common/analytics/ga.rb +0 -12
- data/lib/bg/common/analytics/google_analytics/client.rb +0 -31
- data/lib/bg/common/analytics/google_analytics/events.rb +0 -40
- data/lib/bg/common/analytics/intercom.rb +0 -14
- data/lib/bg/common/analytics/intercom/client.rb +0 -43
- data/lib/bg/common/analytics/intercom/events.rb +0 -36
- data/lib/bg/common/analytics/intercom/user.rb +0 -67
- data/lib/bg/common/analytics/keen.rb +0 -12
- data/lib/bg/common/analytics/keen/client.rb +0 -20
- data/lib/bg/common/analytics/keen/events.rb +0 -31
- data/lib/bg/common/analytics/tracker.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af8e5e82e86e21e0398d5ce8826558ae66fce078a79b4ec4c5fa0a30ea8fe9c8
|
4
|
+
data.tar.gz: 54a11768cd478ec1ecadc11889b2f41495b4e27df51a668dda922e3a185a48d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1ecdaab2503247fb753056ce088b06fef9e5c22a407dd7fe1e575e2c44313425627d4c05d10c297d7ae01e3bcbbac841c788dfa333caa9c4930907050f65e8c
|
7
|
+
data.tar.gz: 1733373879f401eb0e90cf57dce3614a815c9daa919597c51f7d48f63d6af7dd928ec2b5ed3100a3881293f5c722af1529331771b88225f891dae13be32b5452
|
data/README.md
CHANGED
@@ -6,15 +6,11 @@ A Ruby library of common behaviors, especially for Ruby on Rails applications.
|
|
6
6
|
|
7
7
|
<!-- MarkdownTOC depth=4 autolink=true bracket=round -->
|
8
8
|
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
12
|
-
|
13
|
-
|
14
|
-
- [Intercom](#intercom)
|
15
|
-
- [Keen.io](#keenio)
|
16
|
-
- [Development](#development)
|
17
|
-
- [Contributing](#contributing)
|
9
|
+
- Installation
|
10
|
+
- Usage
|
11
|
+
- I. Basic Auth
|
12
|
+
- Development
|
13
|
+
- Contributing
|
18
14
|
|
19
15
|
<!-- /MarkdownTOC -->
|
20
16
|
|
@@ -52,216 +48,6 @@ BASIC_AUTH_PASSWORD=password
|
|
52
48
|
|
53
49
|
make sure to change `user` and `password` to something a bit more secure!
|
54
50
|
|
55
|
-
### II. Analytics
|
56
|
-
|
57
|
-
This library provides a common interface for sending events to different analytics/user tracking platforms (eg. Google Analytics, Intercom, Keen.io).
|
58
|
-
|
59
|
-
**Caveat:** Although the interface is the same, the data sent to each one is different. Some accept custom attributes, and others don't. So for that reason, you will still need to have knowledge of what each platform accepts as params. Those are explained below for each one of them.
|
60
|
-
|
61
|
-
To track an event on all of the supported platforms, all you need to do is call the `Tracker` class, with the proper params.
|
62
|
-
|
63
|
-
```ruby
|
64
|
-
require 'bg/common/analytics'
|
65
|
-
|
66
|
-
user = current_user # or whatever the logged in user object is in your case.
|
67
|
-
|
68
|
-
# Track on the various analytics tools:
|
69
|
-
tracker_data = {
|
70
|
-
ga: {
|
71
|
-
category: 'Users',
|
72
|
-
action: 'User Logged In',
|
73
|
-
label: user.email,
|
74
|
-
value: nil,
|
75
|
-
bounce: false,
|
76
|
-
},
|
77
|
-
intercom: {
|
78
|
-
event_name: 'user-logged-in',
|
79
|
-
created_at: Time.now.to_i,
|
80
|
-
email: user.email,
|
81
|
-
},
|
82
|
-
keen: {
|
83
|
-
event: 'user_logged_in',
|
84
|
-
data: {
|
85
|
-
user_id: user.id,
|
86
|
-
email: user.email,
|
87
|
-
date: Time.now.utc.to_i, # Cannot be a time object, so cast as
|
88
|
-
# Integer or String because of the following:
|
89
|
-
# ActiveJob::SerializationError
|
90
|
-
# (Unsupported argument type: Time)
|
91
|
-
}
|
92
|
-
}
|
93
|
-
}
|
94
|
-
|
95
|
-
BG::Common::Analytics::Tracker.new.call(tracker_data)
|
96
|
-
```
|
97
|
-
|
98
|
-
The call above detects whether you have background jobs enabled by checking if the `ActiveJob::Base` class is defined. If it is, it will execute the calls to each platform by creating a background job for each one. This is beneficial to make sure that these non critical third party calls do not block the user action.
|
99
|
-
|
100
|
-
If `ActiveJob::Base` is not defined, the calls would be perform inline.
|
101
|
-
|
102
|
-
**What if I do not want Google to creep on my users???**
|
103
|
-
|
104
|
-
Well that's easy, do not enable it. For each supported platform, the gem determines if it's enabled by checking on a couple of things (see the sections below for more detail) and if the checks pass, then that platform is included in the tracker call. Otherwise, that platform is ignored.
|
105
|
-
|
106
|
-
**Oh boy! That's a long namespace!**
|
107
|
-
|
108
|
-
`BG::Common::Analytics::Tracker` is a lot to type, but we expect that you will setup your class that will inherit from this one, and create your own methods that determine how the data is structured for each event. Something like this:
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
class MyAnalytics < BG::Common::Analytics::Tracker
|
112
|
-
def track_login user
|
113
|
-
tracker_data = {
|
114
|
-
ga: {
|
115
|
-
category: 'Users',
|
116
|
-
action: 'User Logged In',
|
117
|
-
label: user.email,
|
118
|
-
value: nil,
|
119
|
-
bounce: false,
|
120
|
-
},
|
121
|
-
intercom: {
|
122
|
-
event_name: 'user-logged-in',
|
123
|
-
created_at: Time.now.to_i,
|
124
|
-
email: user.email,
|
125
|
-
},
|
126
|
-
keen: {
|
127
|
-
event: 'user_logged_in',
|
128
|
-
data: {
|
129
|
-
user_id: user.id,
|
130
|
-
email: user.email,
|
131
|
-
date: Time.now.utc.to_i,
|
132
|
-
}
|
133
|
-
}
|
134
|
-
}
|
135
|
-
|
136
|
-
call tracker_data
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
MyAnalytics.new.track_login user
|
141
|
-
```
|
142
|
-
|
143
|
-
or if you want to build something more extendable:
|
144
|
-
|
145
|
-
```ruby
|
146
|
-
# lib/my_app/analytics/base.rb
|
147
|
-
module MyApp
|
148
|
-
module Analytics
|
149
|
-
class Base < ::BG::Common::Analytics::Tracker
|
150
|
-
def initialize user:, additional_data:
|
151
|
-
super()
|
152
|
-
|
153
|
-
@user = user
|
154
|
-
@additional_data = additional_data
|
155
|
-
end
|
156
|
-
|
157
|
-
def call
|
158
|
-
default_keen_data = extract_default_keen_data user: @user, additional_data: @additional_data
|
159
|
-
extra_keen_data = extract_extra_keen_data additional_data: @additional_data
|
160
|
-
|
161
|
-
tracker_data = {
|
162
|
-
ga: { ... },
|
163
|
-
intercom: { ... },
|
164
|
-
keen: {
|
165
|
-
event: event_name,
|
166
|
-
data: default_keen_data.merge(extra_keen_data),
|
167
|
-
},
|
168
|
-
}
|
169
|
-
|
170
|
-
super tracker_data
|
171
|
-
end
|
172
|
-
|
173
|
-
private
|
174
|
-
|
175
|
-
def extract_extra_keen_data additional_data:
|
176
|
-
raise NotImplementedError
|
177
|
-
end
|
178
|
-
|
179
|
-
def event_name
|
180
|
-
raise NotImplementedError
|
181
|
-
end
|
182
|
-
|
183
|
-
def extract_default_keen_data user:, additional_data:
|
184
|
-
{
|
185
|
-
user_id: user.id,
|
186
|
-
user_email: user.email,
|
187
|
-
datestamp: Time.now.utc.to_i,
|
188
|
-
}
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
# lib/my_app/analytics/comment.rb
|
195
|
-
module MyApp
|
196
|
-
module Analytics
|
197
|
-
module Comment
|
198
|
-
class Create < Base
|
199
|
-
private
|
200
|
-
|
201
|
-
def extract_extra_keen_data additional_data:
|
202
|
-
{
|
203
|
-
comment_id: additional_data[:comment_id]
|
204
|
-
}
|
205
|
-
end
|
206
|
-
|
207
|
-
def event_name
|
208
|
-
'new_comment'
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
# in comments_controller.rb
|
216
|
-
MyApp::Analytics::Comment::Create.new(user: current_user, additional_data: {
|
217
|
-
comment_id: @comment.id,
|
218
|
-
}).call
|
219
|
-
```
|
220
|
-
|
221
|
-
#### Google Analytics
|
222
|
-
|
223
|
-
All you need to have GA support is to add the `gabba` gem to your `Gemfile`, and the following `ENV` variables:
|
224
|
-
|
225
|
-
```ruby
|
226
|
-
# Gemfile
|
227
|
-
gem 'gabba'
|
228
|
-
```
|
229
|
-
|
230
|
-
```
|
231
|
-
# .env
|
232
|
-
GA_TRACKER_CODE=...
|
233
|
-
GA_DOMAIN=...
|
234
|
-
```
|
235
|
-
|
236
|
-
which are required to configure the GA client.
|
237
|
-
|
238
|
-
#### Intercom
|
239
|
-
|
240
|
-
All you need to have Intercom support is to include the `intercom` gem:
|
241
|
-
|
242
|
-
```ruby
|
243
|
-
# Gemfile
|
244
|
-
gem 'intercom'
|
245
|
-
```
|
246
|
-
|
247
|
-
**PS:** make sure you configure the gem in the initializer as specified by the Intercom docs.
|
248
|
-
|
249
|
-
#### Keen.io
|
250
|
-
|
251
|
-
All you need to have GA support is to add the `keen` gem to your `Gemfile`, and the following `ENV` variables:
|
252
|
-
|
253
|
-
```ruby
|
254
|
-
# Gemfile
|
255
|
-
gem 'keen'
|
256
|
-
```
|
257
|
-
|
258
|
-
```
|
259
|
-
# .env
|
260
|
-
KEEN_PROJECT_ID=...
|
261
|
-
```
|
262
|
-
|
263
|
-
which are required to configure the Keen client.
|
264
|
-
|
265
51
|
## Development
|
266
52
|
|
267
53
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Bg
|
2
|
+
module Common
|
3
|
+
module Constraints
|
4
|
+
class ApiConstraint
|
5
|
+
def initialize version: 1, default: true
|
6
|
+
@version = version
|
7
|
+
@default = default
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches? request
|
11
|
+
versioned_accept_header?(request) || @default
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def versioned_accept_header? request
|
17
|
+
accept = request.headers['Accept']
|
18
|
+
|
19
|
+
if accept
|
20
|
+
mime_type, version = accept.gsub(/\s/, "").split(";")
|
21
|
+
return mime_type.match(/vnd\.jbh\+json/) && version == "version=#{@version}"
|
22
|
+
end
|
23
|
+
|
24
|
+
return false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
data/lib/bg/common/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bg-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Youssef Chaker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,28 +67,11 @@ files:
|
|
67
67
|
- LICENSE
|
68
68
|
- README.md
|
69
69
|
- Rakefile
|
70
|
-
- app/jobs/bg/common/analytics/ga_event_job.rb
|
71
|
-
- app/jobs/bg/common/analytics/intercom_create_user_job.rb
|
72
|
-
- app/jobs/bg/common/analytics/intercom_event_job.rb
|
73
|
-
- app/jobs/bg/common/analytics/intercom_export_users_job.rb
|
74
|
-
- app/jobs/bg/common/analytics/intercom_update_users_job.rb
|
75
|
-
- app/jobs/bg/common/analytics/keen_event_job.rb
|
76
70
|
- bg-common.gemspec
|
77
71
|
- bin/console
|
78
72
|
- bin/setup
|
79
73
|
- lib/bg/common.rb
|
80
|
-
- lib/bg/common/
|
81
|
-
- lib/bg/common/analytics/ga.rb
|
82
|
-
- lib/bg/common/analytics/google_analytics/client.rb
|
83
|
-
- lib/bg/common/analytics/google_analytics/events.rb
|
84
|
-
- lib/bg/common/analytics/intercom.rb
|
85
|
-
- lib/bg/common/analytics/intercom/client.rb
|
86
|
-
- lib/bg/common/analytics/intercom/events.rb
|
87
|
-
- lib/bg/common/analytics/intercom/user.rb
|
88
|
-
- lib/bg/common/analytics/keen.rb
|
89
|
-
- lib/bg/common/analytics/keen/client.rb
|
90
|
-
- lib/bg/common/analytics/keen/events.rb
|
91
|
-
- lib/bg/common/analytics/tracker.rb
|
74
|
+
- lib/bg/common/constraints/api_constraint.rb
|
92
75
|
- lib/bg/common/engine.rb
|
93
76
|
- lib/bg/common/middlewares/basic_auth.rb
|
94
77
|
- lib/bg/common/rails.rb
|
@@ -112,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
95
|
version: '0'
|
113
96
|
requirements: []
|
114
97
|
rubyforge_project:
|
115
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.7.6
|
116
99
|
signing_key:
|
117
100
|
specification_version: 4
|
118
101
|
summary: A Ruby library of common behaviors, especially for Ruby on Rails applications.
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'bg/common/analytics'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class IntercomCreateUserJob < ::ActiveJob::Base
|
7
|
-
queue_as :analytics
|
8
|
-
|
9
|
-
def perform data
|
10
|
-
BG::Common::Analytics::Intercom::Client.new.make_intercom_call do |client|
|
11
|
-
client.users.create data
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'bg/common/analytics'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class IntercomEventJob < ::ActiveJob::Base
|
7
|
-
queue_as :analytics
|
8
|
-
|
9
|
-
def perform data
|
10
|
-
BG::Common::Analytics::Intercom::Client.call data
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'bg/common/analytics'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class IntercomExportUsersJob < ::ActiveJob::Base
|
7
|
-
queue_as :analytics
|
8
|
-
|
9
|
-
def perform data
|
10
|
-
BG::Common::Analytics::Intercom::Client.new.make_intercom_call do |client|
|
11
|
-
client.users.submit_bulk_job(create_items: data)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'bg/common/analytics'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class IntercomSyncUsersJob < ::ActiveJob::Base
|
7
|
-
queue_as :analytics
|
8
|
-
|
9
|
-
def perform
|
10
|
-
users = User.all
|
11
|
-
|
12
|
-
users.each do |user|
|
13
|
-
BG::Common::Analytics::Intercom.update_user user
|
14
|
-
end unless users.blank?
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'bg/common/analytics'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class KeenEventJob < ::ActiveJob::Base
|
7
|
-
queue_as :analytics
|
8
|
-
|
9
|
-
def perform event, data
|
10
|
-
BG::Common::Analytics::Keen::Client.call event, data
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/lib/bg/common/analytics.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require_relative 'analytics/tracker'
|
2
|
-
|
3
|
-
module BG
|
4
|
-
module Common
|
5
|
-
module Analytics
|
6
|
-
class << self
|
7
|
-
def load!
|
8
|
-
load_ga if ga?
|
9
|
-
load_intercom if intercom?
|
10
|
-
load_keen if keen?
|
11
|
-
end
|
12
|
-
|
13
|
-
def ga?
|
14
|
-
begin
|
15
|
-
require 'gabba'
|
16
|
-
defined?(::Gabba) && ENV['GA_TRACKER_CODE'] && ENV['GA_DOMAIN']
|
17
|
-
rescue LoadError
|
18
|
-
BG::Common.logger.info 'GA is not loaded'
|
19
|
-
|
20
|
-
return false
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def intercom?
|
25
|
-
begin
|
26
|
-
require 'intercom'
|
27
|
-
defined?(::Intercom) && ENV['INTERCOM_ACCESS_TOKEN']
|
28
|
-
rescue LoadError
|
29
|
-
BG::Common.logger.info 'Intercom is not loaded'
|
30
|
-
|
31
|
-
return false
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def keen?
|
36
|
-
begin
|
37
|
-
require 'keen'
|
38
|
-
defined?(::Keen) && ENV['KEEN_PROJECT_ID']
|
39
|
-
rescue LoadError
|
40
|
-
BG::Common.logger.info 'Keen is not loaded'
|
41
|
-
|
42
|
-
return false
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def load_ga
|
49
|
-
require_relative 'analytics/ga'
|
50
|
-
end
|
51
|
-
|
52
|
-
def load_intercom
|
53
|
-
require_relative 'analytics/intercom'
|
54
|
-
end
|
55
|
-
|
56
|
-
def load_keen
|
57
|
-
require_relative 'analytics/keen'
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
BG::Common::Analytics.load!
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module BG
|
2
|
-
module Common
|
3
|
-
module Analytics
|
4
|
-
module GA
|
5
|
-
class Client
|
6
|
-
def initialize
|
7
|
-
@ga = ::Gabba::Gabba.new(ENV['GA_TRACKER_CODE'], ENV['GA_DOMAIN'])
|
8
|
-
end
|
9
|
-
|
10
|
-
def ga
|
11
|
-
@ga ||= ::Gabba::Gabba.new(ENV['GA_TRACKER_CODE'], ENV['GA_DOMAIN'])
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.call data
|
15
|
-
self.new.call data
|
16
|
-
end
|
17
|
-
|
18
|
-
def call data
|
19
|
-
ga.event(
|
20
|
-
data[:category],
|
21
|
-
data[:action],
|
22
|
-
data[:label],
|
23
|
-
data[:value],
|
24
|
-
data[:bounce] || false
|
25
|
-
)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Module that wraps the GA events API to create specific events related
|
3
|
-
# to actions from the app.
|
4
|
-
#
|
5
|
-
module BG
|
6
|
-
module Common
|
7
|
-
module Analytics
|
8
|
-
module GA
|
9
|
-
module Events
|
10
|
-
#
|
11
|
-
# Creates an event in GA
|
12
|
-
#
|
13
|
-
# @param data, Hash
|
14
|
-
#
|
15
|
-
# The data param includes the information for the event to register on GA.
|
16
|
-
# It should have the following attributes:
|
17
|
-
#
|
18
|
-
# category: String
|
19
|
-
# action: String
|
20
|
-
# label: String
|
21
|
-
# value: Integer
|
22
|
-
# bounce: Boolean
|
23
|
-
#
|
24
|
-
# more info here:
|
25
|
-
# https://support.google.com/analytics/answer/1033068#Anatomy
|
26
|
-
# https://developers.google.com/analytics/devguides/collection/analyticsjs/events
|
27
|
-
#
|
28
|
-
def create_event data
|
29
|
-
if BG::Common.active_job?
|
30
|
-
GAEventJob.perform_later data
|
31
|
-
else
|
32
|
-
BG::Common::Analytics::GA::Client.new.call data
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module BG
|
2
|
-
module Common
|
3
|
-
module Analytics
|
4
|
-
module Intercom
|
5
|
-
class Client
|
6
|
-
def initialize
|
7
|
-
@intercom = ::Intercom::Client.new token: ENV['INTERCOM_ACCESS_TOKEN']
|
8
|
-
end
|
9
|
-
|
10
|
-
def intercom
|
11
|
-
@intercom ||= ::Intercom::Client.new token: ENV['INTERCOM_ACCESS_TOKEN']
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.call data
|
15
|
-
self.new.call data
|
16
|
-
end
|
17
|
-
|
18
|
-
def call data
|
19
|
-
make_intercom_call do |client|
|
20
|
-
client.events.create data
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def make_intercom_call &block
|
25
|
-
begin
|
26
|
-
yield self.intercom
|
27
|
-
rescue ::Intercom::IntercomError => e
|
28
|
-
# TODO: report this to newrelic so we won't lose any important
|
29
|
-
# errors that we might have to fix.
|
30
|
-
BG::Common.logger.debug "Intercom call failed: #{e.inspect}"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def config
|
37
|
-
::IntercomRails.config
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Module that wraps the Intercom events API to create specific
|
3
|
-
# events related to app actions.
|
4
|
-
#
|
5
|
-
module BG
|
6
|
-
module Common
|
7
|
-
module Analytics
|
8
|
-
module Intercom
|
9
|
-
module Events
|
10
|
-
#
|
11
|
-
# Creates an event in Intercom
|
12
|
-
#
|
13
|
-
# @param data, Hash
|
14
|
-
#
|
15
|
-
# The data param includes the information for the event to register on
|
16
|
-
# Intercom. It should have the following attributes:
|
17
|
-
#
|
18
|
-
# event_name: String
|
19
|
-
# created_at: Timestamp
|
20
|
-
# email: String
|
21
|
-
# metadata: Object
|
22
|
-
#
|
23
|
-
# more info here: https://doc.intercom.io/api/#submitting-events
|
24
|
-
#
|
25
|
-
def create_event data
|
26
|
-
if BG::Common.active_job?
|
27
|
-
IntercomEventJob.perform_later data
|
28
|
-
else
|
29
|
-
BG::Common::Analytics::Intercom::Client.new.call data
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Module that wraps the Intercom users API.
|
3
|
-
#
|
4
|
-
module BG
|
5
|
-
module Common
|
6
|
-
module Analytics
|
7
|
-
module Intercom
|
8
|
-
module User
|
9
|
-
#
|
10
|
-
# Exports users to Intercom.
|
11
|
-
#
|
12
|
-
# @param limit, Integer, Sets the limit of users to export.
|
13
|
-
#
|
14
|
-
def export_users limit=nil
|
15
|
-
users = ::User.all
|
16
|
-
|
17
|
-
if limit
|
18
|
-
users = users.limit(limit)
|
19
|
-
end
|
20
|
-
|
21
|
-
data = users.map { |user| intercom_user_object user }
|
22
|
-
|
23
|
-
IntercomExportUsersJob.perform_later data
|
24
|
-
end
|
25
|
-
|
26
|
-
#
|
27
|
-
# Creates user in Intercom.
|
28
|
-
#
|
29
|
-
# @param user, User
|
30
|
-
#
|
31
|
-
def create_user user
|
32
|
-
data = intercom_user_object user
|
33
|
-
|
34
|
-
IntercomCreateUserJob.perform_later data
|
35
|
-
end
|
36
|
-
|
37
|
-
#
|
38
|
-
# Updates user in Intercom.
|
39
|
-
#
|
40
|
-
# @param user, User
|
41
|
-
#
|
42
|
-
def update_user user
|
43
|
-
create_user user
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def intercom_user_object user
|
49
|
-
{
|
50
|
-
user_id: user.id,
|
51
|
-
email: user.username,
|
52
|
-
name: user.name,
|
53
|
-
signed_up_at: user.created_at.to_i,
|
54
|
-
custom_attributes: custom_attributes(user)
|
55
|
-
}
|
56
|
-
end
|
57
|
-
|
58
|
-
def custom_attributes user
|
59
|
-
# Override this method in your app to provide custom attributes
|
60
|
-
# specific to your app to be passed on to Intercom.
|
61
|
-
{ }
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module BG
|
2
|
-
module Common
|
3
|
-
module Analytics
|
4
|
-
module Keen
|
5
|
-
class Client
|
6
|
-
def initialize
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.call event, data = {}
|
10
|
-
self.new.call event, data
|
11
|
-
end
|
12
|
-
|
13
|
-
def call event, data = {}
|
14
|
-
::Keen.publish event.to_sym, data
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Module that wraps the Keen events API to create specific
|
3
|
-
# events related to app actions.
|
4
|
-
#
|
5
|
-
module BG
|
6
|
-
module Common
|
7
|
-
module Analytics
|
8
|
-
module Keen
|
9
|
-
module Events
|
10
|
-
#
|
11
|
-
# Creates an event in Keen
|
12
|
-
#
|
13
|
-
# @param data, Hash
|
14
|
-
#
|
15
|
-
# The data param includes the information for the event to register on
|
16
|
-
# Keen. It can be any valid Ruby hash.
|
17
|
-
#
|
18
|
-
# more info here: https://github.com/keenlabs/keen-gem#synchronous-publishing
|
19
|
-
#
|
20
|
-
def create_event event, data
|
21
|
-
if BG::Common.active_job?
|
22
|
-
BG::Common::Analytics::KeenEventJob.perform_later event, data
|
23
|
-
else
|
24
|
-
BG::Common::Analytics::Keen::Client.call event, data
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module BG
|
2
|
-
module Common
|
3
|
-
module Analytics
|
4
|
-
class Tracker
|
5
|
-
def initialize
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.call data
|
9
|
-
self.new.call data
|
10
|
-
end
|
11
|
-
|
12
|
-
def call data
|
13
|
-
if BG::Common::Analytics.ga? and data[:ga]
|
14
|
-
BG::Common::Analytics::GA.create_event data[:ga]
|
15
|
-
end
|
16
|
-
|
17
|
-
if BG::Common::Analytics.intercom? and data[:intercom]
|
18
|
-
BG::Common::Analytics::Intercom.create_event data[:intercom]
|
19
|
-
end
|
20
|
-
|
21
|
-
if BG::Common::Analytics.keen? and data[:keen]
|
22
|
-
BG::Common::Analytics::Keen.create_event data[:keen][:event], data[:keen][:data]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|