amplitude-api 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59e87e342af629c6e87369ef210a272186236ec4e4ef9f9a101a2a590da59204
4
- data.tar.gz: 7e207b30db0550982c4280aa0da2d3c225b1598cfd2bcba1971e8a75238e142e
3
+ metadata.gz: ccae25c2fa30d5c9f6172ed99c802f47022495d32e444a101ccfc230352111f4
4
+ data.tar.gz: '08392b43958061be04edb9b5df142f930d096be402c8a2f0e31697690df6c584'
5
5
  SHA512:
6
- metadata.gz: 42aa5d35506dcaebb8a9db1e973081f38d0259eacc1da1eeeae6d530fc92055dfc70f749e4b19a4e28e3b726e92abae52e575207f1fadfa483bd2b6d6abba771
7
- data.tar.gz: 7e97e56ac203bee46e1740e937c0869cb310d8c4edbc88ce6c2603446c5ad0989e52ffbf327be61680813ff2e92808a9ae91e09d0de9e4de5f4cedae590c2415
6
+ metadata.gz: 701706508efb89ff8adb3ea18e063500c1e3bf83fa89c07f1fe571177d16c7856a3d5dfdc7b4f4927b22fc811a94d7b8a02c318cfac3f7b040a62eb61a5effdf
7
+ data.tar.gz: 1b24d8ab6ffda5757549867550ade78e9f5c1cfcdf2aaf149041e2a4e1822a6e84aa06afd2b22ee006f668d54c09d8b8f600c72f5adccf627d1c4a1111e7eb44
data/Changelog.md CHANGED
@@ -3,9 +3,13 @@
3
3
  We would like to think our many [contributors](https://github.com/toothrot/amplitude-api/graphs/contributors) for
4
4
  suggestions, ideas and improvements to Amplitude API.
5
5
 
6
+ ## 0.3.1 (2021-02-23)
7
+ * Allows sending options to Amplitude
8
+ * Solves an error when accessing event properties not been created yet
9
+
6
10
  ## 0.3.0 (2021-02-22)
7
11
 
8
- * Changes Typhoeus to Faraday to launch requests
12
+ * Changes Typhoeus to Faraday to launch requests (**breaking change**)
9
13
  * Adds new API fields to Event
10
14
  * Event can now include arbitrary properties, so it could be used if the API adds new ones.
11
15
 
data/lib/amplitude_api.rb CHANGED
@@ -70,10 +70,13 @@ class AmplitudeAPI
70
70
  def track_body(*events)
71
71
  event_body = events.flatten.map(&:to_hash)
72
72
 
73
- JSON.generate(
73
+ body = {
74
74
  api_key: api_key,
75
75
  events: event_body
76
- )
76
+ }
77
+ body[:options] = config.options if config.options
78
+
79
+ JSON.generate(body)
77
80
  end
78
81
 
79
82
  # @overload track(event)
@@ -8,7 +8,8 @@ class AmplitudeAPI
8
8
  include Singleton
9
9
 
10
10
  attr_accessor :api_key, :secret_key, :whitelist, :time_formatter,
11
- :event_properties_formatter, :user_properties_formatter
11
+ :event_properties_formatter, :user_properties_formatter,
12
+ :options
12
13
 
13
14
  def initialize
14
15
  self.class.defaults.each { |k, v| send("#{k}=", v) }
@@ -25,6 +25,7 @@ class AmplitudeAPI
25
25
 
26
26
  def method_missing(method_name, *args)
27
27
  super if block_given?
28
+ super unless method_name.to_s.end_with? "="
28
29
 
29
30
  property_name = method_name.to_s.delete_suffix("=")
30
31
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AmplitudeAPI
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
data/readme.md CHANGED
@@ -31,6 +31,37 @@ event = AmplitudeAPI::Event.new({
31
31
  AmplitudeAPI.track(event)
32
32
  ```
33
33
 
34
+ You can track multiple events with a single call, with the only limit of the payload
35
+ size imposed by Amplitude:
36
+
37
+ ```ruby
38
+ event_1 = AmplitudeAPI::Event.new(...)
39
+ event_2 = AmplitudeAPI::Event.new(...)
40
+
41
+ AmplitudeAPI.track(event_1, event_2)
42
+ ```
43
+
44
+ ```ruby
45
+ events = [event_1, event_2]
46
+ AmplitudeAPI.track(*events)
47
+ ```
48
+
49
+ In case you use an integer as the time, it is expected to be in seconds. Values in
50
+ the time field will be converted to milliseconds using `->(time) { time ? time.to_i * 1_000 : nil }`
51
+ You can change this behaviour and use your custom formatter. For example, in case
52
+ you wanted to use milliseconds instead of seconds you could do this:
53
+ ```ruby
54
+ AmplitudeAPI.config.time_formatter = ->(time) { time ? time.to_i : nil },
55
+ ```
56
+
57
+ You can speficy track options in the config. The options will be applied to all subsequent requests:
58
+
59
+ ```ruby
60
+ AmplitudeAPI.config.options = { min_id_length: 10 }
61
+ AmplitudeAPI.track(event)
62
+ ```
63
+
64
+
34
65
  ## User Privacy APIs
35
66
 
36
67
  The following code snippet will delete a user from amplitude
@@ -347,6 +347,12 @@ describe AmplitudeAPI::Event do
347
347
  expect(event.respond_to?(:arbitrary_property=)).to be true
348
348
  end
349
349
 
350
+ it "does not define property until assigned" do
351
+ expect {
352
+ event.undefined_property
353
+ }.to raise_error NoMethodError, /undefined_property/
354
+ end
355
+
350
356
  it "do not accepts blocks when assigning values to create properties" do
351
357
  expect do
352
358
  event.arbitrary_property { puts "whatever" }
@@ -7,7 +7,32 @@ describe AmplitudeAPI do
7
7
  let(:device_id) { "abcdef" }
8
8
 
9
9
  describe ".track" do
10
+ before do
11
+ described_class.config.options = nil
12
+ end
13
+
10
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
22
+
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
+
11
36
  context "with only user_id" do
12
37
  it "sends the event to Amplitude" do
13
38
  event = AmplitudeAPI::Event.new(
@@ -65,6 +90,31 @@ describe AmplitudeAPI do
65
90
  end
66
91
 
67
92
  context "with multiple events" do
93
+ it "can send options" do
94
+ event = AmplitudeAPI::Event.new(
95
+ user_id: 123,
96
+ event_type: "clicked on sign up"
97
+ )
98
+ event2 = AmplitudeAPI::Event.new(
99
+ user_id: 456,
100
+ event_type: "liked a widget"
101
+ )
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(
111
+ api_key: described_class.api_key,
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
+
68
118
  it "sends all events in a single request" do
69
119
  event = AmplitudeAPI::Event.new(
70
120
  user_id: 123,
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rakoczy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-22 00:00:00.000000000 Z
11
+ date: 2021-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry