amplitude-api 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/amplitude-api.rb +55 -7
- data/lib/amplitude-api/identification.rb +49 -0
- data/lib/amplitude-api/version.rb +1 -1
- data/spec/lib/amplitude-api/identification_spec.rb +44 -0
- data/spec/lib/amplitude_api_spec.rb +56 -4
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a052a8eba1f08e0c286851d27a6bf2ce7e93cabe
|
4
|
+
data.tar.gz: 420237d4d92990243c11fce24998ba314178258d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19691aa9910e89f15bab32bf9ea73a3f051a8a50d6340cee1ab7e6e0d6fef1874c7cc23f80fb838773b32b6072479b9843813655875ed77d9fb2c0d2198a72cb
|
7
|
+
data.tar.gz: 36882567908c18fba0cd0b1a2f4a60b389bf84a7c2c9610b89bd7622674e79437687f7c52f25bd3841659412a0e761b794e5deaddc9e2ddb22f27025e8f66d95
|
data/lib/amplitude-api.rb
CHANGED
@@ -2,9 +2,11 @@ require 'json'
|
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'typhoeus'
|
4
4
|
require 'amplitude-api/event'
|
5
|
+
require 'amplitude-api/identification'
|
5
6
|
|
6
7
|
class AmplitudeAPI
|
7
|
-
|
8
|
+
TRACK_URI_STRING = "https://api.amplitude.com/httpapi"
|
9
|
+
IDENTIFY_URI_STRING = "https://api.amplitude.com/identify"
|
8
10
|
|
9
11
|
USER_WITH_NO_ACCOUNT = "user who doesn't have an account"
|
10
12
|
|
@@ -13,6 +15,8 @@ class AmplitudeAPI
|
|
13
15
|
# @return [ String ] an Amplitude API Key
|
14
16
|
attr_accessor :api_key
|
15
17
|
|
18
|
+
# ==== Event Tracking related methods
|
19
|
+
|
16
20
|
# Send a single event immediately to the AmplitudeAPI
|
17
21
|
#
|
18
22
|
# @param [ String ] event_name a string that describes the event, e.g. "clicked on Home"
|
@@ -25,37 +29,81 @@ class AmplitudeAPI
|
|
25
29
|
track(event)
|
26
30
|
end
|
27
31
|
|
28
|
-
|
32
|
+
|
33
|
+
# @overload track_body(event)
|
29
34
|
# @param [ AmplitudeAPI::Event ]
|
30
35
|
#
|
31
|
-
# @overload
|
36
|
+
# @overload track_body([events])
|
32
37
|
# @param [ Array<AmplitudeAPI::Event> ]
|
33
38
|
#
|
34
39
|
# @return [ Hash ]
|
35
40
|
#
|
36
41
|
# Converts a series of AmplitudeAPI::Event objects into a body
|
37
42
|
# suitable for the Amplitude API
|
38
|
-
def
|
43
|
+
def track_body(*events)
|
39
44
|
event_body = events.flatten.map do |event|
|
40
45
|
event.to_hash
|
41
46
|
end
|
42
|
-
|
47
|
+
|
48
|
+
{
|
43
49
|
api_key: self.api_key,
|
44
50
|
event: JSON.generate(event_body)
|
45
51
|
}
|
46
52
|
end
|
47
53
|
|
54
|
+
|
48
55
|
# @overload track(event)
|
49
56
|
# @param [ AmplitudeAPI::Event ] Send a single event to the Amplitude API
|
50
57
|
#
|
51
58
|
# @overload track([events])
|
52
59
|
# @param [ Array<AmplitudeAPI::Event> ] Send an array of events in a single request to Amplitude
|
53
60
|
#
|
54
|
-
# @return [ Typhoeus::Response ]
|
61
|
+
# @return [ Typhoeus::Response ]
|
55
62
|
#
|
56
63
|
# Send one or more Events to the Amplitude API
|
57
64
|
def track(*events)
|
58
|
-
Typhoeus.post(
|
65
|
+
Typhoeus.post(TRACK_URI_STRING, body: track_body(events))
|
66
|
+
end
|
67
|
+
|
68
|
+
# ==== Identification related methods
|
69
|
+
|
70
|
+
def send_identify(user_id, user_properties = {})
|
71
|
+
identification = AmplitudeAPI::Identification.new(user_id: user_id, user_properties: user_properties)
|
72
|
+
identify(identification)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @overload identify_body(identification)
|
76
|
+
# @param [ AmplitudeAPI::Identification ]
|
77
|
+
#
|
78
|
+
# @overload identify_body([identifications])
|
79
|
+
# @param [ Array<AmplitudeAPI::Identification> ]
|
80
|
+
#
|
81
|
+
# @return [ Hash ]
|
82
|
+
#
|
83
|
+
# Converts a series of AmplitudeAPI::Identification objects into a body
|
84
|
+
# suitable for the Amplitude Identify API
|
85
|
+
def identify_body(*identifications)
|
86
|
+
identification_body = identifications.flatten.map do |identification|
|
87
|
+
identification.to_hash
|
88
|
+
end
|
89
|
+
|
90
|
+
{
|
91
|
+
api_key: self.api_key,
|
92
|
+
identification: JSON.generate(identification_body)
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
# @overload identify(identification)
|
97
|
+
# @param [ AmplitudeAPI::Identify ] Send a single identify to the Amplitude API
|
98
|
+
#
|
99
|
+
# @overload identify([identifications])
|
100
|
+
# @param [ Array<AmplitudeAPI::Identify> ] Send an array of identifications in a single request to Amplitude
|
101
|
+
#
|
102
|
+
# @return [ Typhoeus::Response ]
|
103
|
+
#
|
104
|
+
# Send one or more Identifications to the Amplitude Identify API
|
105
|
+
def identify(*identifications)
|
106
|
+
Typhoeus.post(IDENTIFY_URI_STRING, body: identify_body(identifications))
|
59
107
|
end
|
60
108
|
end
|
61
109
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class AmplitudeAPI
|
2
|
+
class Identification
|
3
|
+
# @!attribute [ rw ] user_id
|
4
|
+
# @return [ String ] the user_id to be sent to Amplitude
|
5
|
+
attr_accessor :user_id
|
6
|
+
# @!attribute [ rw ] user_properties
|
7
|
+
# @return [ String ] the user_properties to be attached to the Amplitude Identify
|
8
|
+
attr_accessor :user_properties
|
9
|
+
|
10
|
+
# Create a new Identification
|
11
|
+
#
|
12
|
+
# @param [ String ] user_id a user_id to associate with the identification
|
13
|
+
# @param [ Hash ] user_properties various properties to attach to the user identification
|
14
|
+
def initialize(user_id: "", user_properties: {})
|
15
|
+
self.user_id = user_id
|
16
|
+
self.user_properties = user_properties
|
17
|
+
end
|
18
|
+
|
19
|
+
def user_id=(value)
|
20
|
+
@user_id =
|
21
|
+
if value.respond_to?(:id)
|
22
|
+
value.id
|
23
|
+
else
|
24
|
+
value || AmplitudeAPI::USER_WITH_NO_ACCOUNT
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [ Hash ] A serialized Event
|
29
|
+
#
|
30
|
+
# Used for serialization and comparison
|
31
|
+
def to_hash
|
32
|
+
{
|
33
|
+
user_id: self.user_id,
|
34
|
+
user_properties: self.user_properties
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [ true, false ]
|
39
|
+
#
|
40
|
+
# Compares +to_hash+ for equality
|
41
|
+
def ==(other)
|
42
|
+
if other.respond_to?(:to_hash)
|
43
|
+
self.to_hash == other.to_hash
|
44
|
+
else
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AmplitudeAPI::Identification do
|
4
|
+
User = Struct.new(:id)
|
5
|
+
|
6
|
+
context "with a user object" do
|
7
|
+
describe "#body" do
|
8
|
+
it "populates with the user's id" do
|
9
|
+
identification = AmplitudeAPI::Identification.new(user_id: User.new(123))
|
10
|
+
expect(identification.to_hash[:user_id]).to eq(123)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with a user id" do
|
16
|
+
describe "#body" do
|
17
|
+
it "populates with the user's id" do
|
18
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123)
|
19
|
+
expect(identification.to_hash[:user_id]).to eq(123)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "without a user" do
|
25
|
+
describe "#body" do
|
26
|
+
it "populates with the unknown user" do
|
27
|
+
identification = AmplitudeAPI::Identification.new(user_id: nil)
|
28
|
+
expect(identification.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#body' do
|
34
|
+
it "includes the user id" do
|
35
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123)
|
36
|
+
expect(identification.to_hash[:user_id]).to eq(123)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "includes arbitrary user properties" do
|
40
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123, user_properties: {first_name: 'John', last_name: 'Doe'})
|
41
|
+
expect(identification.to_hash[:user_properties]).to eq(first_name: 'John', last_name: 'Doe')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -11,7 +11,7 @@ describe AmplitudeAPI do
|
|
11
11
|
event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on sign up')
|
12
12
|
body = {api_key: AmplitudeAPI.api_key, event: JSON.generate([event.to_hash])}
|
13
13
|
|
14
|
-
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::
|
14
|
+
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
|
15
15
|
|
16
16
|
AmplitudeAPI.track(event)
|
17
17
|
end
|
@@ -23,13 +23,38 @@ describe AmplitudeAPI do
|
|
23
23
|
event2 = AmplitudeAPI::Event.new(user_id: 456, event_type: 'liked a widget')
|
24
24
|
body = {api_key: AmplitudeAPI.api_key, event: JSON.generate([event.to_hash, event2.to_hash])}
|
25
25
|
|
26
|
-
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::
|
26
|
+
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
|
27
27
|
|
28
28
|
AmplitudeAPI.track([event, event2])
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
describe ".identify" do
|
34
|
+
context "with a single identification" do
|
35
|
+
it "sends the identification to Amplitude" do
|
36
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123, user_properties: {first_name: 'John', last_name: 'Doe'})
|
37
|
+
body = {api_key: AmplitudeAPI.api_key, identification: JSON.generate([identification.to_hash])}
|
38
|
+
|
39
|
+
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
|
40
|
+
|
41
|
+
AmplitudeAPI.identify(identification)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with multiple identifications" do
|
46
|
+
it "sends all identifications in a single request" do
|
47
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123, user_properties: {first_name: 'Julian', last_name: 'Ponce'})
|
48
|
+
identification2 = AmplitudeAPI::Identification.new(user_id: 456, user_properties: {first_name: 'John', last_name: 'Doe'})
|
49
|
+
body = {api_key: AmplitudeAPI.api_key, identification: JSON.generate([identification.to_hash, identification2.to_hash])}
|
50
|
+
|
51
|
+
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
|
52
|
+
|
53
|
+
AmplitudeAPI.identify([identification, identification2])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
33
58
|
describe ".initializer " do
|
34
59
|
it "initializes event without parameter" do
|
35
60
|
event = AmplitudeAPI::Event.new()
|
@@ -77,16 +102,43 @@ describe AmplitudeAPI do
|
|
77
102
|
end
|
78
103
|
end
|
79
104
|
|
105
|
+
describe ".send_identify" do
|
106
|
+
it "sends an identify to AmplitudeAPI" do
|
107
|
+
identification = AmplitudeAPI::Identification.new(user_id: @user, user_properties: {first_name: 'John', last_name: 'Doe'})
|
108
|
+
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
109
|
+
|
110
|
+
AmplitudeAPI.send_identify(@user, {first_name: 'John', last_name: 'Doe'})
|
111
|
+
end
|
112
|
+
|
113
|
+
context "the user is nil" do
|
114
|
+
it "sends an identify with the no account user" do
|
115
|
+
identification = AmplitudeAPI::Identification.new(user_id: nil, user_properties: {first_name: 'John', last_name: 'Doe'})
|
116
|
+
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
117
|
+
|
118
|
+
AmplitudeAPI.send_identify(nil, {first_name: 'John', last_name: 'Doe'})
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "the user is a user_id" do
|
123
|
+
it "sends an identify to AmplitudeAPI" do
|
124
|
+
identification = AmplitudeAPI::Identification.new(user_id: 123, user_properties: {first_name: 'John', last_name: 'Doe'})
|
125
|
+
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
126
|
+
|
127
|
+
AmplitudeAPI.send_identify(@user.id, {first_name: 'John', last_name: 'Doe'})
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
80
132
|
describe "#body" do
|
81
133
|
it "should add an api key" do
|
82
134
|
event = AmplitudeAPI::Event.new(user_id: @user, event_type: "test_event", event_properties: {test_property: 1})
|
83
|
-
body = AmplitudeAPI.
|
135
|
+
body = AmplitudeAPI.track_body(event)
|
84
136
|
expect(body[:api_key]).to eq('stub api key')
|
85
137
|
end
|
86
138
|
|
87
139
|
it "should create an event" do
|
88
140
|
event = AmplitudeAPI::Event.new(user_id: 23, event_type: "test_event", event_properties: {foo: "bar"})
|
89
|
-
body = AmplitudeAPI.
|
141
|
+
body = AmplitudeAPI.track_body(event)
|
90
142
|
|
91
143
|
expected = JSON.generate([{event_type: "test_event", user_id: 23, event_properties: {foo: "bar"}}])
|
92
144
|
expect(body[:event]).to eq(expected)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amplitude-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rakoczy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -96,9 +96,11 @@ files:
|
|
96
96
|
- amplitude-api.gemspec
|
97
97
|
- lib/amplitude-api.rb
|
98
98
|
- lib/amplitude-api/event.rb
|
99
|
+
- lib/amplitude-api/identification.rb
|
99
100
|
- lib/amplitude-api/version.rb
|
100
101
|
- readme.md
|
101
102
|
- spec/lib/amplitude-api/event_spec.rb
|
103
|
+
- spec/lib/amplitude-api/identification_spec.rb
|
102
104
|
- spec/lib/amplitude_api_spec.rb
|
103
105
|
- spec/spec_helper.rb
|
104
106
|
homepage: https://github.com/toothrot/amplitude-api
|
@@ -121,11 +123,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
123
|
version: '0'
|
122
124
|
requirements: []
|
123
125
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.4.5
|
126
|
+
rubygems_version: 2.4.5.1
|
125
127
|
signing_key:
|
126
128
|
specification_version: 4
|
127
129
|
summary: Send events to the Amplitude API
|
128
130
|
test_files:
|
129
131
|
- spec/lib/amplitude-api/event_spec.rb
|
132
|
+
- spec/lib/amplitude-api/identification_spec.rb
|
130
133
|
- spec/lib/amplitude_api_spec.rb
|
131
134
|
- spec/spec_helper.rb
|