amplitude-api 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/lib/amplitude_api/event.rb +49 -1
- data/lib/amplitude_api/version.rb +1 -1
- data/readme.md +2 -1
- data/spec/lib/amplitude_api/event_spec.rb +125 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67727755b54bfef1fc3ffa5889e11c7931420a13
|
4
|
+
data.tar.gz: 52feec8f51522d7f2c17b0db39d4e033f76efe2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3b3bf19725cda0082c7cf0acb7ca40614430a63ff5d929e803c3074955d9b35e5674987745a5fb17d2cc0e030fb7319a5903e012d40c98f5e171efea8f3b0f6
|
7
|
+
data.tar.gz: 09b6ca4ddb4ec2bdbed9980a41356b6f0096feace6819209c98ce37eb2a83722f509ca60762ccd7731a10308693bdce1eddf88f9e5cb27338fdf92be84dffd74
|
data/.travis.yml
CHANGED
data/lib/amplitude_api/event.rb
CHANGED
@@ -20,13 +20,38 @@ class AmplitudeAPI
|
|
20
20
|
# @return [ String ] IP address of the user
|
21
21
|
attr_accessor :ip
|
22
22
|
|
23
|
+
# @!attribute [ rw ] insert_id
|
24
|
+
# @return [ String ] the unique identifier to be sent to Amplitude
|
25
|
+
attr_accessor :insert_id
|
26
|
+
|
27
|
+
# @!attribute [ rw ] price
|
28
|
+
# @return [ String ] (required for revenue data) price of the item purchased
|
29
|
+
attr_accessor :price
|
30
|
+
|
31
|
+
# @!attribute [ rw ] quantity
|
32
|
+
# @return [ String ] (required for revenue data, defaults to 1 if not specified) quantity of the item purchased
|
33
|
+
attr_accessor :quantity
|
34
|
+
|
35
|
+
# @!attribute [ rw ] product_id
|
36
|
+
# @return [ String ] an identifier for the product. (Note: you must send a price and quantity with this field)
|
37
|
+
attr_accessor :product_id
|
38
|
+
|
39
|
+
# @!attribute [ rw ] revenue_type
|
40
|
+
# @return [ String ] type of revenue. (Note: you must send a price and quantity with this field)
|
41
|
+
attr_accessor :revenue_type
|
42
|
+
|
23
43
|
# Create a new Event
|
24
44
|
#
|
25
45
|
# @param [ String ] user_id a user_id to associate with the event
|
26
46
|
# @param [ String ] event_type a name for the event
|
27
47
|
# @param [ Hash ] event_properties various properties to attach to the event
|
28
48
|
# @param [ Time ] Time that the event occurred (defaults to now)
|
49
|
+
# @param [ Double ] price (optional, but required for revenue data) price of the item purchased
|
50
|
+
# @param [ Integer ] quantity (optional, but required for revenue data) quantity of the item purchased
|
51
|
+
# @param [ String ] product_id (optional) an identifier for the product.
|
52
|
+
# @param [ String ] revenue_type (optional) type of revenue
|
29
53
|
# @param [ String ] IP address of the user
|
54
|
+
# @param [ String ] insert_id a unique identifier for the event
|
30
55
|
def initialize(options = {})
|
31
56
|
self.user_id = options.fetch(:user_id, '')
|
32
57
|
self.event_type = options.fetch(:event_type, '')
|
@@ -34,6 +59,8 @@ class AmplitudeAPI
|
|
34
59
|
self.user_properties = options.fetch(:user_properties, {})
|
35
60
|
self.time = options[:time]
|
36
61
|
self.ip = options.fetch(:ip, '')
|
62
|
+
self.insert_id = options[:insert_id]
|
63
|
+
validate_revenue_arguments(options)
|
37
64
|
end
|
38
65
|
|
39
66
|
def user_id=(value)
|
@@ -56,7 +83,9 @@ class AmplitudeAPI
|
|
56
83
|
serialized_event[:user_properties] = user_properties
|
57
84
|
serialized_event[:time] = formatted_time if time
|
58
85
|
serialized_event[:ip] = ip if ip
|
59
|
-
serialized_event
|
86
|
+
serialized_event[:insert_id] = insert_id if insert_id
|
87
|
+
|
88
|
+
serialized_event.merge(revenue_hash)
|
60
89
|
end
|
61
90
|
|
62
91
|
# @return [ true, false ]
|
@@ -75,5 +104,24 @@ class AmplitudeAPI
|
|
75
104
|
def formatted_time
|
76
105
|
time.to_i * 1_000
|
77
106
|
end
|
107
|
+
|
108
|
+
def validate_revenue_arguments(options)
|
109
|
+
self.price = options[:price]
|
110
|
+
self.quantity = options[:quantity] || 1 if price
|
111
|
+
self.product_id = options[:product_id]
|
112
|
+
self.revenue_type = options[:revenue_type]
|
113
|
+
return if price
|
114
|
+
raise ArgumentError, 'You must provide a price in order to use the product_id' if product_id
|
115
|
+
raise ArgumentError, 'You must provide a price in order to use the revenue_type' if revenue_type
|
116
|
+
end
|
117
|
+
|
118
|
+
def revenue_hash
|
119
|
+
revenue_hash = {}
|
120
|
+
revenue_hash[:productId] = product_id if product_id
|
121
|
+
revenue_hash[:revenueType] = revenue_type if revenue_type
|
122
|
+
revenue_hash[:quantity] = quantity if quantity
|
123
|
+
revenue_hash[:price] = price if price
|
124
|
+
revenue_hash
|
125
|
+
end
|
78
126
|
end
|
79
127
|
end
|
data/readme.md
CHANGED
@@ -21,6 +21,7 @@ event = AmplitudeAPI::Event.new({
|
|
21
21
|
user_id: "123",
|
22
22
|
event_type: "clicked on home",
|
23
23
|
time: Time.now,
|
24
|
+
insert_id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
24
25
|
event_properties: {
|
25
26
|
cause: "button",
|
26
27
|
arbitrary: "properties"
|
@@ -40,7 +41,7 @@ Currently, we are using this in Rails and using ActiveJob to dispatch events asy
|
|
40
41
|
## Other useful resources
|
41
42
|
* [Amplitude HTTP Api Documentation](https://amplitude.zendesk.com/hc/en-us/articles/204771828)
|
42
43
|
* [Segment.io Amplitude integration](https://segment.com/docs/integrations/amplitude/)
|
43
|
-
|
44
|
+
|
44
45
|
## Contributing
|
45
46
|
|
46
47
|
I'd love to hear how you're using this. Please check out the [issues](https://github.com/toothrot/amplitude-api/issues).
|
@@ -39,6 +39,30 @@ describe AmplitudeAPI::Event do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
describe 'init' do
|
43
|
+
context 'the user does not send in a price' do
|
44
|
+
it 'raises an error if the user sends in a product_id' do
|
45
|
+
expect do
|
46
|
+
described_class.new(
|
47
|
+
user_id: 123,
|
48
|
+
event_type: 'bad event',
|
49
|
+
product_id: 'hopscotch.4lyfe'
|
50
|
+
)
|
51
|
+
end.to raise_error(ArgumentError)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'raises an error if the user sends in a revenue_type' do
|
55
|
+
expect do
|
56
|
+
described_class.new(
|
57
|
+
user_id: 123,
|
58
|
+
event_type: 'bad event',
|
59
|
+
revenue_type: 'tax return'
|
60
|
+
)
|
61
|
+
end.to raise_error(ArgumentError)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
42
66
|
describe '#body' do
|
43
67
|
it 'includes the event type' do
|
44
68
|
event = described_class.new(
|
@@ -57,22 +81,109 @@ describe AmplitudeAPI::Event do
|
|
57
81
|
expect(event.to_hash[:event_properties]).to eq(abc: :def)
|
58
82
|
end
|
59
83
|
|
60
|
-
|
61
|
-
time
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
84
|
+
describe 'time' do
|
85
|
+
it 'includes a time for the event' do
|
86
|
+
time = Time.parse('2016-01-01 00:00:00 -0000')
|
87
|
+
event = described_class.new(
|
88
|
+
user_id: 123,
|
89
|
+
event_type: 'clicked on home',
|
90
|
+
time: time
|
91
|
+
)
|
92
|
+
expect(event.to_hash[:time]).to eq(1_451_606_400_000)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'does not include time if it is not set' do
|
96
|
+
event = described_class.new(
|
97
|
+
user_id: 123,
|
98
|
+
event_type: 'clicked on home'
|
99
|
+
)
|
100
|
+
expect(event.to_hash).not_to have_key(:time)
|
101
|
+
end
|
68
102
|
end
|
69
103
|
|
70
|
-
|
71
|
-
event
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
104
|
+
describe 'insert_id' do
|
105
|
+
it 'includes an insert_id for the event' do
|
106
|
+
event = described_class.new(
|
107
|
+
user_id: 123,
|
108
|
+
event_type: 'clicked on home',
|
109
|
+
insert_id: 'foo-bar'
|
110
|
+
)
|
111
|
+
expect(event.to_hash[:insert_id]).to eq('foo-bar')
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'does not include insert_id if it is not set' do
|
115
|
+
event = described_class.new(
|
116
|
+
user_id: 123,
|
117
|
+
event_type: 'clicked on home'
|
118
|
+
)
|
119
|
+
expect(event.to_hash).not_to have_key(:insert_id)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'revenue params' do
|
124
|
+
it 'includes the price if it is set' do
|
125
|
+
price = 100_000.99
|
126
|
+
event = described_class.new(
|
127
|
+
user_id: 123,
|
128
|
+
event_type: 'clicked on home',
|
129
|
+
price: price
|
130
|
+
)
|
131
|
+
expect(event.to_hash[:price]).to eq(price)
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'sets the quantity to 1 if the price is set and the quantity is not' do
|
135
|
+
price = 100_000.99
|
136
|
+
event = described_class.new(
|
137
|
+
user_id: 123,
|
138
|
+
event_type: 'clicked on home',
|
139
|
+
price: price
|
140
|
+
)
|
141
|
+
expect(event.to_hash[:quantity]).to eq(1)
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'includes the quantity if it is set' do
|
145
|
+
quantity = 100
|
146
|
+
event = described_class.new(
|
147
|
+
user_id: 123,
|
148
|
+
event_type: 'clicked on home',
|
149
|
+
quantity: quantity,
|
150
|
+
price: 10.99
|
151
|
+
)
|
152
|
+
expect(event.to_hash[:quantity]).to eq(quantity)
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'includes the productID if set' do
|
156
|
+
product_id = 'hopscotch.subscriptions.rule'
|
157
|
+
event = described_class.new(
|
158
|
+
user_id: 123,
|
159
|
+
event_type: 'clicked on home',
|
160
|
+
price: 199.99,
|
161
|
+
product_id: product_id
|
162
|
+
)
|
163
|
+
expect(event.to_hash[:productId]).to eq(product_id)
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'includes the revenueType if set' do
|
167
|
+
revenue_type = 'income'
|
168
|
+
event = described_class.new(
|
169
|
+
user_id: 123,
|
170
|
+
event_type: 'clicked on home',
|
171
|
+
price: 199.99,
|
172
|
+
revenue_type: revenue_type
|
173
|
+
)
|
174
|
+
expect(event.to_hash[:revenueType]).to eq(revenue_type)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'does not include revenue params if they are not set' do
|
178
|
+
event = described_class.new(
|
179
|
+
user_id: 123,
|
180
|
+
event_type: 'clicked on home'
|
181
|
+
)
|
182
|
+
expect(event.to_hash).not_to have_key(:quantity)
|
183
|
+
expect(event.to_hash).not_to have_key(:revenueType)
|
184
|
+
expect(event.to_hash).not_to have_key(:productId)
|
185
|
+
expect(event.to_hash).not_to have_key(:price)
|
186
|
+
end
|
76
187
|
end
|
77
188
|
end
|
78
189
|
end
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rakoczy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|