amplitude-api 0.1.0 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -4
- data/.rubocop.yml +19 -1
- data/.travis.yml +2 -3
- data/Changelog.md +32 -0
- data/Gemfile +3 -12
- data/Rakefile +7 -5
- data/amplitude-api.gemspec +17 -15
- data/lib/amplitude-api.rb +3 -4
- data/lib/amplitude_api.rb +71 -45
- data/lib/amplitude_api/config.rb +27 -5
- data/lib/amplitude_api/event.rb +77 -16
- data/lib/amplitude_api/identification.rb +3 -1
- data/lib/amplitude_api/version.rb +3 -1
- data/readme.md +38 -6
- data/spec/lib/amplitude_api/event_spec.rb +212 -112
- data/spec/lib/amplitude_api/identification_spec.rb +19 -17
- data/spec/lib/amplitude_api_spec.rb +328 -188
- data/spec/spec_helper.rb +7 -5
- metadata +6 -7
- data/Gemfile.lock +0 -61
@@ -1,10 +1,12 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe AmplitudeAPI::Identification do
|
4
6
|
user = Struct.new(:id)
|
5
7
|
|
6
|
-
context
|
7
|
-
describe
|
8
|
+
context "with a user object" do
|
9
|
+
describe "#body" do
|
8
10
|
it "populates with the user's id" do
|
9
11
|
identification = described_class.new(user_id: user.new(123))
|
10
12
|
expect(identification.to_hash[:user_id]).to eq(123)
|
@@ -12,8 +14,8 @@ describe AmplitudeAPI::Identification do
|
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
context
|
16
|
-
describe
|
17
|
+
context "with a user id" do
|
18
|
+
describe "#body" do
|
17
19
|
it "populates with the user's id" do
|
18
20
|
identification = described_class.new(user_id: 123)
|
19
21
|
expect(identification.to_hash[:user_id]).to eq(123)
|
@@ -21,35 +23,35 @@ describe AmplitudeAPI::Identification do
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
context
|
25
|
-
describe
|
26
|
-
it
|
26
|
+
context "without a user" do
|
27
|
+
describe "#body" do
|
28
|
+
it "populates with the unknown user" do
|
27
29
|
identification = described_class.new(user_id: nil)
|
28
30
|
expect(identification.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
|
-
describe
|
34
|
-
it
|
35
|
+
describe "#body" do
|
36
|
+
it "includes the user id" do
|
35
37
|
identification = described_class.new(user_id: 123)
|
36
38
|
expect(identification.to_hash[:user_id]).to eq(123)
|
37
39
|
end
|
38
40
|
|
39
|
-
it
|
40
|
-
identification = described_class.new(user_id: 123, device_id:
|
41
|
-
expect(identification.to_hash[:device_id]).to eq(
|
41
|
+
it "includes the device id" do
|
42
|
+
identification = described_class.new(user_id: 123, device_id: "abc")
|
43
|
+
expect(identification.to_hash[:device_id]).to eq("abc")
|
42
44
|
end
|
43
45
|
|
44
|
-
it
|
46
|
+
it "includes arbitrary user properties" do
|
45
47
|
identification = described_class.new(
|
46
48
|
user_id: 123,
|
47
49
|
user_properties: {
|
48
|
-
first_name:
|
49
|
-
last_name:
|
50
|
+
first_name: "John",
|
51
|
+
last_name: "Doe"
|
50
52
|
}
|
51
53
|
)
|
52
|
-
expect(identification.to_hash[:user_properties]).to eq(first_name:
|
54
|
+
expect(identification.to_hash[:user_properties]).to eq(first_name: "John", last_name: "Doe")
|
53
55
|
end
|
54
56
|
end
|
55
57
|
end
|
@@ -1,95 +1,151 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe AmplitudeAPI do
|
4
6
|
let(:user) { Struct.new(:id).new(123) }
|
5
|
-
let(:device_id) {
|
7
|
+
let(:device_id) { "abcdef" }
|
8
|
+
|
9
|
+
describe ".track" do
|
10
|
+
before do
|
11
|
+
described_class.config.options = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with a single event" do
|
15
|
+
it "can send options" do
|
16
|
+
event = AmplitudeAPI::Event.new(
|
17
|
+
user_id: 123,
|
18
|
+
event_type: "clicked on sign up"
|
19
|
+
)
|
20
|
+
options = { min_id_length: 456 }
|
21
|
+
described_class.config.options = options
|
6
22
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
23
|
+
allow(Faraday).to receive(:post)
|
24
|
+
|
25
|
+
described_class.track(event)
|
26
|
+
|
27
|
+
headers = { "Content-Type" => "application/json" }
|
28
|
+
body = JSON.generate(
|
29
|
+
api_key: described_class.api_key,
|
30
|
+
events: [event.to_hash],
|
31
|
+
options: options
|
32
|
+
)
|
33
|
+
expect(Faraday).to have_received(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with only user_id" do
|
37
|
+
it "sends the event to Amplitude" do
|
11
38
|
event = AmplitudeAPI::Event.new(
|
12
39
|
user_id: 123,
|
13
|
-
event_type:
|
40
|
+
event_type: "clicked on sign up"
|
14
41
|
)
|
15
|
-
body =
|
42
|
+
body = JSON.generate(
|
16
43
|
api_key: described_class.api_key,
|
17
|
-
|
18
|
-
|
44
|
+
events: [event.to_hash]
|
45
|
+
)
|
46
|
+
headers = { "Content-Type" => "application/json" }
|
19
47
|
|
20
|
-
expect(
|
48
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
21
49
|
|
22
50
|
described_class.track(event)
|
23
51
|
end
|
24
52
|
end
|
25
53
|
|
26
|
-
context
|
27
|
-
it
|
54
|
+
context "with only device_id" do
|
55
|
+
it "sends the event to Amplitude" do
|
28
56
|
event = AmplitudeAPI::Event.new(
|
29
57
|
device_id: device_id,
|
30
|
-
event_type:
|
58
|
+
event_type: "clicked on sign up"
|
31
59
|
)
|
32
|
-
body =
|
60
|
+
body = JSON.generate(
|
33
61
|
api_key: described_class.api_key,
|
34
|
-
|
35
|
-
|
62
|
+
events: [event.to_hash]
|
63
|
+
)
|
64
|
+
headers = { "Content-Type" => "application/json" }
|
36
65
|
|
37
|
-
expect(
|
66
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
38
67
|
|
39
68
|
described_class.track(event)
|
40
69
|
end
|
41
70
|
end
|
42
71
|
|
43
|
-
context
|
44
|
-
it
|
72
|
+
context "with both user_id and device_id" do
|
73
|
+
it "sends the event to Amplitude" do
|
45
74
|
event = AmplitudeAPI::Event.new(
|
46
75
|
user_id: 123,
|
47
76
|
device_id: device_id,
|
48
|
-
event_type:
|
77
|
+
event_type: "clicked on sign up"
|
49
78
|
)
|
50
|
-
body =
|
79
|
+
body = JSON.generate(
|
51
80
|
api_key: described_class.api_key,
|
52
|
-
|
53
|
-
|
81
|
+
events: [event.to_hash]
|
82
|
+
)
|
83
|
+
headers = { "Content-Type" => "application/json" }
|
54
84
|
|
55
|
-
expect(
|
85
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
56
86
|
|
57
87
|
described_class.track(event)
|
58
88
|
end
|
59
89
|
end
|
60
90
|
end
|
61
91
|
|
62
|
-
context
|
63
|
-
it
|
92
|
+
context "with multiple events" do
|
93
|
+
it "can send options" do
|
64
94
|
event = AmplitudeAPI::Event.new(
|
65
95
|
user_id: 123,
|
66
|
-
event_type:
|
96
|
+
event_type: "clicked on sign up"
|
67
97
|
)
|
68
98
|
event2 = AmplitudeAPI::Event.new(
|
69
99
|
user_id: 456,
|
70
|
-
event_type:
|
100
|
+
event_type: "liked a widget"
|
71
101
|
)
|
72
|
-
|
102
|
+
options = { min_id_length: 456 }
|
103
|
+
described_class.config.options = options
|
104
|
+
|
105
|
+
allow(Faraday).to receive(:post)
|
106
|
+
|
107
|
+
described_class.track([event, event2])
|
108
|
+
|
109
|
+
headers = { "Content-Type" => "application/json" }
|
110
|
+
body = JSON.generate(
|
73
111
|
api_key: described_class.api_key,
|
74
|
-
|
75
|
-
|
112
|
+
events: [event.to_hash, event2.to_hash],
|
113
|
+
options: options
|
114
|
+
)
|
115
|
+
expect(Faraday).to have_received(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "sends all events in a single request" do
|
119
|
+
event = AmplitudeAPI::Event.new(
|
120
|
+
user_id: 123,
|
121
|
+
event_type: "clicked on sign up"
|
122
|
+
)
|
123
|
+
event2 = AmplitudeAPI::Event.new(
|
124
|
+
user_id: 456,
|
125
|
+
event_type: "liked a widget"
|
126
|
+
)
|
127
|
+
body = JSON.generate(
|
128
|
+
api_key: described_class.api_key,
|
129
|
+
events: [event.to_hash, event2.to_hash]
|
130
|
+
)
|
131
|
+
headers = { "Content-Type" => "application/json" }
|
76
132
|
|
77
|
-
expect(
|
133
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body, headers)
|
78
134
|
|
79
135
|
described_class.track([event, event2])
|
80
136
|
end
|
81
137
|
end
|
82
138
|
end
|
83
139
|
|
84
|
-
describe
|
85
|
-
context
|
86
|
-
context
|
87
|
-
it
|
140
|
+
describe ".identify" do
|
141
|
+
context "with a single identification" do
|
142
|
+
context "with only user_id" do
|
143
|
+
it "sends the identification to Amplitude" do
|
88
144
|
identification = AmplitudeAPI::Identification.new(
|
89
145
|
user_id: 123,
|
90
146
|
user_properties: {
|
91
|
-
first_name:
|
92
|
-
last_name:
|
147
|
+
first_name: "John",
|
148
|
+
last_name: "Doe"
|
93
149
|
}
|
94
150
|
)
|
95
151
|
body = {
|
@@ -97,19 +153,19 @@ describe AmplitudeAPI do
|
|
97
153
|
identification: JSON.generate([identification.to_hash])
|
98
154
|
}
|
99
155
|
|
100
|
-
expect(
|
156
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
|
101
157
|
|
102
158
|
described_class.identify(identification)
|
103
159
|
end
|
104
160
|
end
|
105
161
|
|
106
|
-
context
|
107
|
-
it
|
162
|
+
context "with only device_id" do
|
163
|
+
it "sends the identification to Amplitude" do
|
108
164
|
identification = AmplitudeAPI::Identification.new(
|
109
165
|
device_id: device_id,
|
110
166
|
user_properties: {
|
111
|
-
first_name:
|
112
|
-
last_name:
|
167
|
+
first_name: "John",
|
168
|
+
last_name: "Doe"
|
113
169
|
}
|
114
170
|
)
|
115
171
|
body = {
|
@@ -117,20 +173,20 @@ describe AmplitudeAPI do
|
|
117
173
|
identification: JSON.generate([identification.to_hash])
|
118
174
|
}
|
119
175
|
|
120
|
-
expect(
|
176
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
|
121
177
|
|
122
178
|
described_class.identify(identification)
|
123
179
|
end
|
124
180
|
end
|
125
181
|
|
126
|
-
context
|
127
|
-
it
|
182
|
+
context "with both user_id and device_id" do
|
183
|
+
it "sends the identification to Amplitude" do
|
128
184
|
identification = AmplitudeAPI::Identification.new(
|
129
185
|
user_id: 123,
|
130
186
|
device_id: device_id,
|
131
187
|
user_properties: {
|
132
|
-
first_name:
|
133
|
-
last_name:
|
188
|
+
first_name: "John",
|
189
|
+
last_name: "Doe"
|
134
190
|
}
|
135
191
|
)
|
136
192
|
body = {
|
@@ -138,27 +194,27 @@ describe AmplitudeAPI do
|
|
138
194
|
identification: JSON.generate([identification.to_hash])
|
139
195
|
}
|
140
196
|
|
141
|
-
expect(
|
197
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
|
142
198
|
|
143
199
|
described_class.identify(identification)
|
144
200
|
end
|
145
201
|
end
|
146
202
|
end
|
147
203
|
|
148
|
-
context
|
149
|
-
it
|
204
|
+
context "with multiple identifications" do
|
205
|
+
it "sends all identifications in a single request" do
|
150
206
|
identification = AmplitudeAPI::Identification.new(
|
151
207
|
user_id: 123,
|
152
208
|
user_properties: {
|
153
|
-
first_name:
|
154
|
-
last_name:
|
209
|
+
first_name: "Julian",
|
210
|
+
last_name: "Ponce"
|
155
211
|
}
|
156
212
|
)
|
157
213
|
identification2 = AmplitudeAPI::Identification.new(
|
158
214
|
device_id: 456,
|
159
215
|
user_properties: {
|
160
|
-
first_name:
|
161
|
-
last_name:
|
216
|
+
first_name: "John",
|
217
|
+
last_name: "Doe"
|
162
218
|
}
|
163
219
|
)
|
164
220
|
body = {
|
@@ -166,331 +222,415 @@ describe AmplitudeAPI do
|
|
166
222
|
identification: JSON.generate([identification.to_hash, identification2.to_hash])
|
167
223
|
}
|
168
224
|
|
169
|
-
expect(
|
225
|
+
expect(Faraday).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body)
|
170
226
|
|
171
227
|
described_class.identify([identification, identification2])
|
172
228
|
end
|
173
229
|
end
|
174
230
|
end
|
175
231
|
|
176
|
-
describe
|
232
|
+
describe ".initializer" do
|
177
233
|
let(:attributes) do
|
178
234
|
{
|
179
235
|
user_id: 123,
|
180
|
-
event_type:
|
236
|
+
event_type: "test_event",
|
181
237
|
event_properties: {
|
182
238
|
test_property: 1
|
183
239
|
},
|
184
240
|
user_properties: {},
|
185
|
-
ip:
|
241
|
+
ip: "8.8.8.8"
|
186
242
|
}
|
187
243
|
end
|
188
244
|
|
189
|
-
it
|
245
|
+
it "requires event type" do
|
190
246
|
attributes.delete(:event_type)
|
191
247
|
expect { AmplitudeAPI::Event.new(attributes) }.to raise_error(ArgumentError)
|
192
248
|
end
|
193
249
|
|
194
|
-
it
|
250
|
+
it "requires user id or device id" do
|
195
251
|
expect(AmplitudeAPI::Event.new(attributes).to_h).to eq(attributes)
|
196
|
-
attributes.merge!(device_id:
|
252
|
+
attributes.merge!(device_id: "abc").delete(:user_id)
|
197
253
|
expect(AmplitudeAPI::Event.new(attributes).to_h).to eq(attributes)
|
198
254
|
attributes.delete(:device_id)
|
199
255
|
expect { AmplitudeAPI::Event.new(attributes) }.to raise_error(ArgumentError)
|
200
256
|
end
|
201
257
|
|
202
|
-
it
|
258
|
+
it "initializes event with parameter" do
|
203
259
|
expect(AmplitudeAPI::Event.new(attributes)).to eq(attributes)
|
204
260
|
end
|
205
261
|
end
|
206
262
|
|
207
|
-
describe
|
208
|
-
context
|
209
|
-
it
|
263
|
+
describe ".send_event" do
|
264
|
+
context "with only user_id" do
|
265
|
+
it "sends an event to AmplitudeAPI" do
|
210
266
|
event = AmplitudeAPI::Event.new(
|
211
267
|
user_id: user,
|
212
|
-
event_type:
|
268
|
+
event_type: "test_event",
|
213
269
|
event_properties: { test_property: 1 }
|
214
270
|
)
|
215
271
|
expect(described_class).to receive(:track).with(event)
|
216
272
|
|
217
|
-
described_class.send_event(
|
273
|
+
described_class.send_event("test_event", user, nil, event_properties: { test_property: 1 })
|
218
274
|
end
|
219
275
|
|
220
|
-
context
|
221
|
-
it
|
276
|
+
context "the user is nil" do
|
277
|
+
it "sends an event with the no account user" do
|
222
278
|
event = AmplitudeAPI::Event.new(
|
223
279
|
user_id: nil,
|
224
|
-
event_type:
|
280
|
+
event_type: "test_event",
|
225
281
|
event_properties: { test_property: 1 }
|
226
282
|
)
|
227
283
|
expect(described_class).to receive(:track).with(event)
|
228
284
|
|
229
|
-
described_class.send_event(
|
285
|
+
described_class.send_event("test_event", nil, nil, event_properties: { test_property: 1 })
|
230
286
|
end
|
231
287
|
end
|
232
288
|
|
233
|
-
context
|
234
|
-
it
|
289
|
+
context "the user is a user_id" do
|
290
|
+
it "sends an event to AmplitudeAPI" do
|
235
291
|
event = AmplitudeAPI::Event.new(
|
236
292
|
user_id: 123,
|
237
|
-
event_type:
|
293
|
+
event_type: "test_event",
|
238
294
|
event_properties: { test_property: 1 }
|
239
295
|
)
|
240
296
|
expect(described_class).to receive(:track).with(event)
|
241
297
|
|
242
|
-
described_class.send_event(
|
298
|
+
described_class.send_event("test_event", user.id, nil, event_properties: { test_property: 1 })
|
243
299
|
end
|
244
300
|
|
245
|
-
it
|
301
|
+
it "sends arbitrary user_properties to AmplitudeAPI" do
|
246
302
|
event = AmplitudeAPI::Event.new(
|
247
303
|
user_id: 123,
|
248
|
-
event_type:
|
304
|
+
event_type: "test_event",
|
249
305
|
event_properties: { test_property: 1 },
|
250
|
-
user_properties: { test_user_property:
|
306
|
+
user_properties: { test_user_property: "abc" }
|
251
307
|
)
|
252
308
|
expect(described_class).to receive(:track).with(event)
|
253
309
|
|
254
310
|
described_class.send_event(
|
255
|
-
|
311
|
+
"test_event",
|
256
312
|
user.id,
|
257
313
|
nil,
|
258
314
|
event_properties: { test_property: 1 },
|
259
|
-
user_properties: { test_user_property:
|
315
|
+
user_properties: { test_user_property: "abc" }
|
260
316
|
)
|
261
317
|
end
|
262
318
|
end
|
263
319
|
end
|
264
320
|
|
265
|
-
context
|
266
|
-
context
|
267
|
-
it
|
321
|
+
context "with device_id" do
|
322
|
+
context "the user is not nil" do
|
323
|
+
it "sends an event to AmplitudeAPI" do
|
268
324
|
event = AmplitudeAPI::Event.new(
|
269
325
|
user_id: user,
|
270
326
|
device_id: device_id,
|
271
|
-
event_type:
|
327
|
+
event_type: "test_event",
|
272
328
|
event_properties: { test_property: 1 }
|
273
329
|
)
|
274
330
|
expect(described_class).to receive(:track).with(event)
|
275
331
|
|
276
|
-
described_class.send_event(
|
332
|
+
described_class.send_event("test_event", user, device_id, event_properties: { test_property: 1 })
|
277
333
|
end
|
278
334
|
end
|
279
335
|
|
280
|
-
context
|
281
|
-
it
|
336
|
+
context "the user is nil" do
|
337
|
+
it "sends an event with the no account user" do
|
282
338
|
event = AmplitudeAPI::Event.new(
|
283
339
|
user_id: nil,
|
284
340
|
device_id: device_id,
|
285
|
-
event_type:
|
341
|
+
event_type: "test_event",
|
286
342
|
event_properties: { test_property: 1 }
|
287
343
|
)
|
288
344
|
expect(described_class).to receive(:track).with(event)
|
289
345
|
|
290
|
-
described_class.send_event(
|
346
|
+
described_class.send_event("test_event", nil, device_id, event_properties: { test_property: 1 })
|
291
347
|
end
|
292
348
|
end
|
293
349
|
end
|
294
350
|
end
|
295
351
|
|
296
|
-
describe
|
297
|
-
context
|
298
|
-
it
|
352
|
+
describe ".send_identify" do
|
353
|
+
context "with no device_id" do
|
354
|
+
it "sends an identify to AmplitudeAPI" do
|
299
355
|
identification = AmplitudeAPI::Identification.new(
|
300
356
|
user_id: user,
|
301
357
|
user_properties: {
|
302
|
-
first_name:
|
303
|
-
last_name:
|
358
|
+
first_name: "John",
|
359
|
+
last_name: "Doe"
|
304
360
|
}
|
305
361
|
)
|
306
362
|
expect(described_class).to receive(:identify).with(identification)
|
307
363
|
|
308
|
-
described_class.send_identify(user, nil, first_name:
|
364
|
+
described_class.send_identify(user, nil, first_name: "John", last_name: "Doe")
|
309
365
|
end
|
310
366
|
|
311
|
-
context
|
312
|
-
it
|
367
|
+
context "the user is nil" do
|
368
|
+
it "sends an identify with the no account user" do
|
313
369
|
identification = AmplitudeAPI::Identification.new(
|
314
370
|
user_id: nil,
|
315
371
|
user_properties: {
|
316
|
-
first_name:
|
317
|
-
last_name:
|
372
|
+
first_name: "John",
|
373
|
+
last_name: "Doe"
|
318
374
|
}
|
319
375
|
)
|
320
376
|
expect(described_class).to receive(:identify).with(identification)
|
321
377
|
|
322
|
-
described_class.send_identify(nil, nil, first_name:
|
378
|
+
described_class.send_identify(nil, nil, first_name: "John", last_name: "Doe")
|
323
379
|
end
|
324
380
|
end
|
325
381
|
|
326
|
-
context
|
327
|
-
it
|
382
|
+
context "the user is a user_id" do
|
383
|
+
it "sends an identify to AmplitudeAPI" do
|
328
384
|
identification = AmplitudeAPI::Identification.new(
|
329
385
|
user_id: 123,
|
330
386
|
user_properties: {
|
331
|
-
first_name:
|
332
|
-
last_name:
|
387
|
+
first_name: "John",
|
388
|
+
last_name: "Doe"
|
333
389
|
}
|
334
390
|
)
|
335
391
|
expect(described_class).to receive(:identify).with(identification)
|
336
392
|
|
337
|
-
described_class.send_identify(user.id, nil, first_name:
|
393
|
+
described_class.send_identify(user.id, nil, first_name: "John", last_name: "Doe")
|
338
394
|
end
|
339
395
|
end
|
340
396
|
end
|
341
397
|
|
342
|
-
context
|
343
|
-
it
|
398
|
+
context "with a device_id" do
|
399
|
+
it "sends an identify to AmplitudeAPI" do
|
344
400
|
identification = AmplitudeAPI::Identification.new(
|
345
401
|
user_id: user,
|
346
|
-
device_id:
|
402
|
+
device_id: "abc",
|
347
403
|
user_properties: {
|
348
|
-
first_name:
|
349
|
-
last_name:
|
404
|
+
first_name: "John",
|
405
|
+
last_name: "Doe"
|
350
406
|
}
|
351
407
|
)
|
352
408
|
expect(described_class).to receive(:identify).with(identification)
|
353
409
|
|
354
|
-
described_class.send_identify(user,
|
410
|
+
described_class.send_identify(user, "abc", first_name: "John", last_name: "Doe")
|
355
411
|
end
|
356
412
|
end
|
357
413
|
end
|
358
414
|
|
359
|
-
describe
|
415
|
+
describe ".segmentation" do
|
360
416
|
let(:end_time) { Time.now }
|
361
417
|
let(:start_time) { end_time - 60 * 60 * 24 } # -1 day
|
362
418
|
|
363
|
-
it
|
364
|
-
expect(
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
described_class.segmentation({ event_type:
|
374
|
-
s: [{ prop:
|
419
|
+
it "sends request to Amplitude" do
|
420
|
+
expect(Faraday).to receive(:get).with(AmplitudeAPI::SEGMENTATION_URI_STRING,
|
421
|
+
userpwd: "#{described_class.api_key}:#{described_class.secret_key}",
|
422
|
+
params: {
|
423
|
+
e: { event_type: "my event" }.to_json,
|
424
|
+
start: start_time.strftime("%Y%m%d"),
|
425
|
+
end: end_time.strftime("%Y%m%d"),
|
426
|
+
s: [{ prop: "foo", op: "is", values: %w[bar] }.to_json]
|
427
|
+
})
|
428
|
+
|
429
|
+
described_class.segmentation({ event_type: "my event" }, start_time, end_time,
|
430
|
+
s: [{ prop: "foo", op: "is", values: %w[bar] }])
|
375
431
|
end
|
376
432
|
end
|
377
433
|
|
378
|
-
describe
|
379
|
-
|
380
|
-
|
381
|
-
|
434
|
+
describe ".delete" do
|
435
|
+
let(:connection) { instance_double("Faraday::Connection", post: nil, basic_auth: nil) }
|
436
|
+
|
437
|
+
before do
|
438
|
+
allow(Faraday).to receive(:new).and_yield(connection).and_return(connection)
|
439
|
+
allow(Faraday).to receive(:post)
|
440
|
+
end
|
441
|
+
|
442
|
+
it "sends the authentification" do
|
443
|
+
api_key = described_class.config.api_key
|
444
|
+
secret_key = described_class.config.secret_key
|
445
|
+
|
446
|
+
described_class.delete(user_ids: "123")
|
447
|
+
|
448
|
+
expect(connection).to have_received(:basic_auth).with(api_key, secret_key)
|
449
|
+
end
|
450
|
+
|
451
|
+
it "sends the requester" do
|
452
|
+
requester = "privacy@gethopscotch.com"
|
453
|
+
body = {
|
454
|
+
amplitude_ids: ["123"],
|
455
|
+
requester: requester
|
456
|
+
}
|
457
|
+
|
458
|
+
described_class.delete(amplitude_ids: "123", requester: requester)
|
459
|
+
|
460
|
+
expect(connection).to have_received(:post).with(
|
461
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
462
|
+
JSON.generate(body),
|
463
|
+
"Content-Type" => "application/json"
|
464
|
+
)
|
465
|
+
end
|
466
|
+
|
467
|
+
it "sends the ignore_invalid_id flag" do
|
468
|
+
body = {
|
469
|
+
user_ids: ["123"],
|
470
|
+
ignore_invalid_id: "true"
|
471
|
+
}
|
472
|
+
|
473
|
+
described_class.delete(user_ids: "123", ignore_invalid_id: true)
|
474
|
+
|
475
|
+
expect(connection).to have_received(:post).with(
|
476
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
477
|
+
JSON.generate(body),
|
478
|
+
"Content-Type" => "application/json"
|
479
|
+
)
|
480
|
+
end
|
481
|
+
|
482
|
+
it "sends the delete_from_org flag" do
|
483
|
+
body = {
|
484
|
+
user_ids: ["123"],
|
485
|
+
delete_from_org: "true"
|
486
|
+
}
|
487
|
+
|
488
|
+
described_class.delete(user_ids: "123", delete_from_org: true)
|
489
|
+
|
490
|
+
expect(connection).to have_received(:post).with(
|
491
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
492
|
+
JSON.generate(body),
|
493
|
+
"Content-Type" => "application/json"
|
494
|
+
)
|
495
|
+
end
|
496
|
+
|
497
|
+
context "with a single user" do
|
498
|
+
it "sends the user_id to Amplitude" do
|
382
499
|
body = {
|
383
|
-
user_ids:
|
500
|
+
user_ids: ["123"]
|
384
501
|
}
|
385
502
|
|
386
|
-
|
503
|
+
described_class.delete(user_ids: "123")
|
504
|
+
|
505
|
+
expect(connection).to have_received(:post).with(
|
387
506
|
AmplitudeAPI::DELETION_URI_STRING,
|
388
|
-
|
389
|
-
|
507
|
+
JSON.generate(body),
|
508
|
+
"Content-Type" => "application/json"
|
390
509
|
)
|
391
|
-
described_class.delete(user_ids: user_ids)
|
392
510
|
end
|
393
511
|
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
body = {
|
399
|
-
user_ids: user_ids,
|
400
|
-
amplitude_ids: amplitude_ids
|
401
|
-
}
|
512
|
+
it "sends the amplitude_id to Amplitude" do
|
513
|
+
body = {
|
514
|
+
amplitude_ids: ["123"]
|
515
|
+
}
|
402
516
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
)
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
517
|
+
described_class.delete(amplitude_ids: "123")
|
518
|
+
|
519
|
+
expect(connection).to have_received(:post).with(
|
520
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
521
|
+
JSON.generate(body),
|
522
|
+
"Content-Type" => "application/json"
|
523
|
+
)
|
524
|
+
end
|
525
|
+
|
526
|
+
it "sends both user_id and amplitude_id to Amplitude" do
|
527
|
+
body = {
|
528
|
+
amplitude_ids: ["123"],
|
529
|
+
user_ids: ["456"]
|
530
|
+
}
|
531
|
+
|
532
|
+
described_class.delete(user_ids: "456", amplitude_ids: "123")
|
533
|
+
|
534
|
+
expect(connection).to have_received(:post).with(
|
535
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
536
|
+
JSON.generate(body),
|
537
|
+
"Content-Type" => "application/json"
|
538
|
+
)
|
413
539
|
end
|
414
540
|
end
|
415
541
|
|
416
|
-
context
|
417
|
-
it
|
542
|
+
context "with multiple user_ids" do
|
543
|
+
it "sends the user_ids to Amplitude" do
|
544
|
+
user_ids = [123, 456, 555]
|
545
|
+
body = {
|
546
|
+
user_ids: user_ids
|
547
|
+
}
|
548
|
+
|
549
|
+
described_class.delete(user_ids: user_ids)
|
550
|
+
|
551
|
+
expect(connection).to have_received(:post).with(
|
552
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
553
|
+
JSON.generate(body),
|
554
|
+
"Content-Type" => "application/json"
|
555
|
+
)
|
556
|
+
end
|
557
|
+
|
558
|
+
it "sends the amplitude_ids to Amplitude" do
|
418
559
|
amplitude_ids = [122, 456]
|
419
560
|
body = {
|
420
561
|
amplitude_ids: amplitude_ids
|
421
562
|
}
|
422
563
|
|
423
|
-
|
564
|
+
described_class.delete(amplitude_ids: amplitude_ids)
|
565
|
+
|
566
|
+
expect(connection).to have_received(:post).with(
|
424
567
|
AmplitudeAPI::DELETION_URI_STRING,
|
425
|
-
|
426
|
-
|
568
|
+
JSON.generate(body),
|
569
|
+
"Content-Type" => "application/json"
|
427
570
|
)
|
428
|
-
described_class.delete(amplitude_ids: amplitude_ids)
|
429
571
|
end
|
430
|
-
end
|
431
572
|
|
432
|
-
|
433
|
-
|
573
|
+
it "sends both user_ids and amplitude_ids to Amplitude" do
|
574
|
+
user_ids = [123, 456, 555]
|
434
575
|
amplitude_ids = [122, 456]
|
435
|
-
|
436
576
|
body = {
|
437
577
|
amplitude_ids: amplitude_ids,
|
438
|
-
|
578
|
+
user_ids: user_ids
|
439
579
|
}
|
440
|
-
userpwd = "#{described_class.api_key}:#{described_class.config.secret_key}"
|
441
580
|
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
581
|
+
described_class.delete(user_ids: user_ids, amplitude_ids: amplitude_ids)
|
582
|
+
|
583
|
+
expect(connection).to have_received(:post).with(
|
584
|
+
AmplitudeAPI::DELETION_URI_STRING,
|
585
|
+
JSON.generate(body),
|
586
|
+
"Content-Type" => "application/json"
|
448
587
|
)
|
449
588
|
end
|
450
589
|
end
|
451
590
|
end
|
452
591
|
|
453
|
-
describe
|
454
|
-
it
|
592
|
+
describe "#body" do
|
593
|
+
it "adds an api key" do
|
455
594
|
event = AmplitudeAPI::Event.new(
|
456
595
|
user_id: user,
|
457
|
-
event_type:
|
596
|
+
event_type: "test_event",
|
458
597
|
event_properties: {
|
459
598
|
test_property: 1
|
460
599
|
}
|
461
600
|
)
|
462
|
-
|
463
|
-
|
601
|
+
json_body = described_class.track_body(event)
|
602
|
+
body = JSON.parse(json_body)
|
603
|
+
expect(body["api_key"]).to eq("stub api key")
|
464
604
|
end
|
465
605
|
|
466
|
-
it
|
606
|
+
it "creates an event" do
|
467
607
|
event = AmplitudeAPI::Event.new(
|
468
608
|
user_id: 23,
|
469
|
-
event_type:
|
609
|
+
event_type: "test_event",
|
470
610
|
event_properties: {
|
471
|
-
foo:
|
611
|
+
foo: "bar"
|
472
612
|
},
|
473
613
|
user_properties: {
|
474
|
-
abc:
|
614
|
+
abc: "123"
|
475
615
|
},
|
476
|
-
ip:
|
616
|
+
ip: "8.8.8.8"
|
477
617
|
)
|
478
|
-
|
479
|
-
|
618
|
+
json_body = described_class.track_body(event)
|
619
|
+
body = JSON.parse(json_body, symbolize_names: true)
|
480
620
|
expected = [
|
481
621
|
{
|
482
|
-
event_type:
|
622
|
+
event_type: "test_event",
|
483
623
|
user_id: 23,
|
484
624
|
event_properties: {
|
485
|
-
foo:
|
625
|
+
foo: "bar"
|
486
626
|
},
|
487
627
|
user_properties: {
|
488
|
-
abc:
|
628
|
+
abc: "123"
|
489
629
|
},
|
490
|
-
ip:
|
630
|
+
ip: "8.8.8.8"
|
491
631
|
}
|
492
632
|
]
|
493
|
-
expect(
|
633
|
+
expect(body[:events]).to eq(expected)
|
494
634
|
end
|
495
635
|
end
|
496
636
|
end
|