amplitude-api 0.0.9 → 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
- SHA1:
3
- metadata.gz: 2613d299e9d407475b3a03eecb1633f0a5359b40
4
- data.tar.gz: 7a48cebc3d4c592cb894c5d2bf9b814f0ff55a70
2
+ SHA256:
3
+ metadata.gz: ccae25c2fa30d5c9f6172ed99c802f47022495d32e444a101ccfc230352111f4
4
+ data.tar.gz: '08392b43958061be04edb9b5df142f930d096be402c8a2f0e31697690df6c584'
5
5
  SHA512:
6
- metadata.gz: 14739845274080c65ef0afb13b9547a91191ded8a935fae3a99bd04ffac16eb6f632fe4f83d4e209d3ef55f0dd3cd4eedfe645f030836423f42c530ac401363c
7
- data.tar.gz: 1e6933e33ec4ae42076b57e8f9c6cc72797fb6cb25ce708f76182224f5fb02f4b4657fc00811e81211dcb23de1989879dda88ec4c5ec21ce98b96de9f1300fe3
6
+ metadata.gz: 701706508efb89ff8adb3ea18e063500c1e3bf83fa89c07f1fe571177d16c7856a3d5dfdc7b4f4927b22fc811a94d7b8a02c318cfac3f7b040a62eb61a5effdf
7
+ data.tar.gz: 1b24d8ab6ffda5757549867550ade78e9f5c1cfcdf2aaf149041e2a4e1822a6e84aa06afd2b22ee006f668d54c09d8b8f600c72f5adccf627d1c4a1111e7eb44
data/.gitignore CHANGED
@@ -34,4 +34,5 @@ build/
34
34
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
35
  .rvmrc
36
36
 
37
- .idea
37
+ .idea
38
+ amplitude-api.iml
data/.rubocop.yml CHANGED
@@ -1,3 +1,35 @@
1
1
  require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.4
5
+
2
6
  Metrics/LineLength:
3
7
  Max: 120
8
+
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
11
+ SupportedStyles:
12
+ - single_quotes
13
+ - double_quotes
14
+
15
+ Style/BlockDelimiters:
16
+ Enabled: true
17
+ Exclude:
18
+ - spec/**/*
19
+
20
+ # Disable some failing rspec cops that fail from upgrading gems until we can clean things up a bit.
21
+ Naming/FileName:
22
+ Exclude:
23
+ - lib/amplitude-api.rb
24
+ RSpec/ExampleLength:
25
+ Enabled: false
26
+ Metrics/BlockLength:
27
+ Enabled: false
28
+ RSpec/MultipleExpectations:
29
+ Enabled: false
30
+ RSpec/NestedGroups:
31
+ Enabled: false
32
+ RSpec/MessageSpies:
33
+ Enabled: false
34
+ RSpec/ContextWording:
35
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
- - 2.3.1
5
- - 2.2.2
6
- - 2.1.6
4
+ - 2.6
5
+ - 2.4
data/Changelog.md ADDED
@@ -0,0 +1,38 @@
1
+ # Amplitude API Changelog
2
+
3
+ We would like to think our many [contributors](https://github.com/toothrot/amplitude-api/graphs/contributors) for
4
+ suggestions, ideas and improvements to Amplitude API.
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
+
10
+ ## 0.3.0 (2021-02-22)
11
+
12
+ * Changes Typhoeus to Faraday to launch requests (**breaking change**)
13
+ * Adds new API fields to Event
14
+ * Event can now include arbitrary properties, so it could be used if the API adds new ones.
15
+
16
+ ## 0.2.0 (2021-02-14)
17
+
18
+ * Updates gem to use HTTP API V2.
19
+
20
+ ## 0.1.1 (2019-01-01)
21
+
22
+ * Fix #41 - Delete API now correctly handles Arrays of IDs.
23
+
24
+ ## 0.1.0 (2019-01-01)
25
+
26
+ * Update Gem dependencies (thanks @kolorahl, @itamar, @krettan)
27
+ * Minimum ruby version is now 2.2
28
+ * Support Delete API (thanks @samjohn)
29
+ * Fix bundle Inline (thanks @jonduarte)
30
+ * Many fixes from @kolorahl
31
+
32
+ ## 0.0.10 (2017-09-13)
33
+
34
+ * Allow to use "Event Segmentation" via API ([#23](https://github.com/toothrot/amplitude-api/pull/23)).
35
+
36
+ ## Older releases
37
+
38
+ Please see the v0.0.9 tag.
data/Gemfile CHANGED
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Generated from /Users/alex/development/amplitude-api/amplitude-api.gemspec
2
- source 'https://rubygems.org'
4
+ source "https://rubygems.org"
3
5
 
4
- gem 'typhoeus', '~> 1.0.2'
6
+ gem "faraday", "~> 1.3"
5
7
 
6
8
  group :development, :test do
7
- gem 'pry', '~> 0.9.12.6'
8
- gem 'rake', '>= 10.0'
9
- gem 'rspec', '>= 2.99.0'
10
- gem 'rubocop', '~> 0.37.2', require: false
11
- gem 'rubocop-rspec'
9
+ gem "pry", "~> 0.12.2"
10
+ gem "rake", ">= 12.0"
11
+ gem "rspec", ">= 2.99.0"
12
+ gem "rubocop", "~> 0.66.0", require: false
13
+ gem "rubocop-rspec"
12
14
  end
data/Gemfile.lock CHANGED
@@ -1,58 +1,63 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- ast (2.2.0)
5
- coderay (1.1.1)
6
- diff-lcs (1.2.5)
7
- ethon (0.9.0)
8
- ffi (>= 1.3.0)
9
- ffi (1.9.14)
10
- method_source (0.8.2)
11
- parser (2.3.0.6)
12
- ast (~> 2.2)
13
- powerpack (0.1.1)
14
- pry (0.9.12.6)
15
- coderay (~> 1.0)
16
- method_source (~> 0.8)
17
- slop (~> 3.4)
18
- rainbow (2.1.0)
19
- rake (10.5.0)
20
- rspec (3.4.0)
21
- rspec-core (~> 3.4.0)
22
- rspec-expectations (~> 3.4.0)
23
- rspec-mocks (~> 3.4.0)
24
- rspec-core (3.4.3)
25
- rspec-support (~> 3.4.0)
26
- rspec-expectations (3.4.0)
4
+ ast (2.4.0)
5
+ coderay (1.1.2)
6
+ diff-lcs (1.3)
7
+ faraday (1.3.0)
8
+ faraday-net_http (~> 1.0)
9
+ multipart-post (>= 1.2, < 3)
10
+ ruby2_keywords
11
+ faraday-net_http (1.0.1)
12
+ jaro_winkler (1.5.2)
13
+ method_source (0.9.2)
14
+ multipart-post (2.1.1)
15
+ parallel (1.17.0)
16
+ parser (2.6.2.0)
17
+ ast (~> 2.4.0)
18
+ pry (0.12.2)
19
+ coderay (~> 1.1.0)
20
+ method_source (~> 0.9.0)
21
+ psych (3.1.0)
22
+ rainbow (3.0.0)
23
+ rake (12.3.3)
24
+ rspec (3.8.0)
25
+ rspec-core (~> 3.8.0)
26
+ rspec-expectations (~> 3.8.0)
27
+ rspec-mocks (~> 3.8.0)
28
+ rspec-core (3.8.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-expectations (3.8.2)
27
31
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.4.0)
29
- rspec-mocks (3.4.1)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-mocks (3.8.0)
30
34
  diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.4.0)
32
- rspec-support (3.4.1)
33
- rubocop (0.37.2)
34
- parser (>= 2.3.0.4, < 3.0)
35
- powerpack (~> 0.1)
36
- rainbow (>= 1.99.1, < 3.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-support (3.8.0)
37
+ rubocop (0.66.0)
38
+ jaro_winkler (~> 1.5.1)
39
+ parallel (~> 1.10)
40
+ parser (>= 2.5, != 2.5.1.1)
41
+ psych (>= 3.1.0)
42
+ rainbow (>= 2.2.2, < 4.0)
37
43
  ruby-progressbar (~> 1.7)
38
- unicode-display_width (~> 0.3)
39
- rubocop-rspec (1.4.0)
40
- ruby-progressbar (1.7.5)
41
- slop (3.6.0)
42
- typhoeus (1.0.2)
43
- ethon (>= 0.9.0)
44
- unicode-display_width (0.3.1)
44
+ unicode-display_width (>= 1.4.0, < 1.6)
45
+ rubocop-rspec (1.32.0)
46
+ rubocop (>= 0.60.0)
47
+ ruby-progressbar (1.10.0)
48
+ ruby2_keywords (0.0.4)
49
+ unicode-display_width (1.5.0)
45
50
 
46
51
  PLATFORMS
47
52
  ruby
48
53
 
49
54
  DEPENDENCIES
50
- pry (~> 0.9.12.6)
51
- rake (>= 10.0)
55
+ faraday (~> 1.3)
56
+ pry (~> 0.12.2)
57
+ rake (>= 12.0)
52
58
  rspec (>= 2.99.0)
53
- rubocop (~> 0.37.2)
59
+ rubocop (~> 0.66.0)
54
60
  rubocop-rspec
55
- typhoeus (~> 1.0.2)
56
61
 
57
62
  BUNDLED WITH
58
- 1.10.6
63
+ 1.17.3
data/Rakefile CHANGED
@@ -1,14 +1,16 @@
1
- require 'bundler/gem_tasks'
2
- require 'rubocop/rake_task'
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
3
5
 
4
6
  RuboCop::RakeTask.new
5
7
 
6
8
  begin
7
- require 'rspec/core/rake_task'
9
+ require "rspec/core/rake_task"
8
10
  RSpec::Core::RakeTask.new(:spec)
9
11
  rescue LoadError
10
- puts 'Unable to load rspec. Have you run `bundle install`?'
12
+ puts "Unable to load rspec. Have you run `bundle install`?"
11
13
  end
12
14
 
13
15
  task(:default).clear
14
- task default: ['rubocop:auto_correct', :spec]
16
+ task default: ["rubocop:auto_correct", :spec]
@@ -1,26 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'amplitude_api/version'
5
+ require "amplitude_api/version"
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = 'amplitude-api'
8
+ spec.name = "amplitude-api"
8
9
  spec.version = AmplitudeAPI::VERSION
9
- spec.authors = ['Alex Rakoczy']
10
- spec.email = ['arakoczy@gmail.com']
11
- spec.summary = 'Send events to the Amplitude API'
12
- spec.description = 'Provides an integration for sending events to Amplitude'
13
- spec.homepage = 'https://github.com/toothrot/amplitude-api'
14
- spec.license = 'MIT'
10
+ spec.authors = ["Alex Rakoczy"]
11
+ spec.email = ["arakoczy@gmail.com"]
12
+ spec.summary = "Send events to the Amplitude API"
13
+ spec.description = "Provides an integration for sending events to Amplitude"
14
+ spec.homepage = "https://github.com/toothrot/amplitude-api"
15
+ spec.license = "MIT"
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ['lib']
20
+ spec.require_paths = ["lib"]
20
21
 
21
- spec.add_development_dependency 'rspec', '~> 2.99', '>= 2.99.0'
22
- spec.add_development_dependency 'rake', '~> 10.0', '>= 10.0'
23
- spec.add_development_dependency 'pry', '~> 0.9.12.6'
24
- spec.add_dependency 'typhoeus', '~> 1.0.2'
25
- spec.required_ruby_version = '~> 2.0'
22
+ spec.add_development_dependency "pry", "~> 0.12.2"
23
+ spec.add_development_dependency "rake", "~> 12.0", ">= 12.0"
24
+ spec.add_development_dependency "rspec", "~> 2.99", ">= 2.99.0"
25
+ spec.add_dependency "faraday", "~> 1.3"
26
+ spec.required_ruby_version = ">= 2.4"
26
27
  end
data/lib/amplitude-api.rb CHANGED
@@ -1,4 +1,3 @@
1
- # rubocop:disable Style/FileName
2
- require_relative 'amplitude_api'
3
- # rubocop:enable Style/FileName
4
- # Whoops.
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "amplitude_api"
data/lib/amplitude_api.rb CHANGED
@@ -1,20 +1,37 @@
1
- require 'json'
2
- require 'bundler/setup'
3
- require 'typhoeus'
4
- require_relative 'amplitude_api/event'
5
- require_relative 'amplitude_api/identification'
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "faraday"
6
5
 
7
6
  # AmplitudeAPI
8
7
  class AmplitudeAPI
9
- TRACK_URI_STRING = 'https://api.amplitude.com/httpapi'.freeze
10
- IDENTIFY_URI_STRING = 'https://api.amplitude.com/identify'.freeze
8
+ require_relative "amplitude_api/config"
9
+ require_relative "amplitude_api/event"
10
+ require_relative "amplitude_api/identification"
11
+
12
+ TRACK_URI_STRING = "https://api.amplitude.com/2/httpapi"
13
+ IDENTIFY_URI_STRING = "https://api.amplitude.com/identify"
14
+ SEGMENTATION_URI_STRING = "https://amplitude.com/api/2/events/segmentation"
15
+ DELETION_URI_STRING = "https://amplitude.com/api/2/deletions/users"
11
16
 
12
- USER_WITH_NO_ACCOUNT = "user who doesn't have an account".freeze
17
+ USER_WITH_NO_ACCOUNT = "user who doesn't have an account"
13
18
 
14
19
  class << self
15
- # @!attribute [ rw ] api_key
16
- # @return [ String ] an Amplitude API Key
17
- attr_accessor :api_key
20
+ def config
21
+ Config.instance
22
+ end
23
+
24
+ def configure
25
+ yield config
26
+ end
27
+
28
+ def api_key
29
+ config.api_key
30
+ end
31
+
32
+ def secret_key
33
+ config.secret_key
34
+ end
18
35
 
19
36
  # ==== Event Tracking related methods
20
37
 
@@ -23,12 +40,12 @@ class AmplitudeAPI
23
40
  # @param [ String ] event_name a string that describes the event, e.g. "clicked on Home"
24
41
  # @param [ String ] user a string or integer that uniquely identifies a user.
25
42
  # @param [ String ] device a string that uniquely identifies the device.
26
- # @param [ Hash ] event_properties a hash that is serialized to JSON,
43
+ # @option options [ Hash ] event_properties a hash that is serialized to JSON,
27
44
  # and can contain any other property to be stored on the Event
28
- # @param [ Hash ] user_properties a hash that is serialized to JSON,
45
+ # @option options [ Hash ] user_properties a hash that is serialized to JSON,
29
46
  # and contains user properties to be associated with the user
30
47
  #
31
- # @return [ Typhoeus::Response ]
48
+ # @return [ Faraday::Response ]
32
49
  def send_event(event_name, user, device, options = {})
33
50
  event = AmplitudeAPI::Event.new(
34
51
  user_id: user,
@@ -53,10 +70,13 @@ class AmplitudeAPI
53
70
  def track_body(*events)
54
71
  event_body = events.flatten.map(&:to_hash)
55
72
 
56
- {
73
+ body = {
57
74
  api_key: api_key,
58
- event: JSON.generate(event_body)
75
+ events: event_body
59
76
  }
77
+ body[:options] = config.options if config.options
78
+
79
+ JSON.generate(body)
60
80
  end
61
81
 
62
82
  # @overload track(event)
@@ -65,11 +85,11 @@ class AmplitudeAPI
65
85
  # @overload track([events])
66
86
  # @param [ Array<AmplitudeAPI::Event> ] Send an array of events in a single request to Amplitude
67
87
  #
68
- # @return [ Typhoeus::Response ]
88
+ # @return [ Faraday::Response ]
69
89
  #
70
90
  # Send one or more Events to the Amplitude API
71
91
  def track(*events)
72
- Typhoeus.post(TRACK_URI_STRING, body: track_body(events))
92
+ Faraday.post(TRACK_URI_STRING, track_body(events), "Content-Type" => "application/json")
73
93
  end
74
94
 
75
95
  # ==== Identification related methods
@@ -108,11 +128,89 @@ class AmplitudeAPI
108
128
  # @overload identify([identifications])
109
129
  # @param [ Array<AmplitudeAPI::Identify> ] Send an array of identifications in a single request to Amplitude
110
130
  #
111
- # @return [ Typhoeus::Response ]
131
+ # @return [ Faraday::Response ]
112
132
  #
113
133
  # Send one or more Identifications to the Amplitude Identify API
114
134
  def identify(*identifications)
115
- Typhoeus.post(IDENTIFY_URI_STRING, body: identify_body(identifications))
135
+ Faraday.post(IDENTIFY_URI_STRING, identify_body(identifications))
136
+ end
137
+
138
+ # ==== Event Segmentation related methods
139
+
140
+ # Get metrics for an event with segmentation.
141
+ #
142
+ # @param [ Hash ] event a hash that defines event.
143
+ # @param [ Time ] start_time a start time.
144
+ # @param [ Time ] end_time a end time.
145
+ # @option options [ String ] m a string that defines aggregate function.
146
+ # For non-property metrics: "uniques", "totals", "pct_dau", or "average" (default: "uniques").
147
+ # For property metrics: "histogram", "sums", or "value_avg"
148
+ # (note: a valid "group_by" value is required in parameter e).
149
+ # @option options [ Integer ] i an integer that defines segmentation interval.
150
+ # Set to -300000, -3600000, 1, 7, or 30 for realtime, hourly, daily, weekly,
151
+ # and monthly counts, respectively (default: 1). Realtime segmentation is capped at 2 days,
152
+ # hourly segmentation is capped at 7 days, and daily at 365 days.
153
+ # @option options [ Array ] s an array that defines segment definitions.
154
+ # @option options [ String ] g a string that defines property to group by.
155
+ # @option options [ Integer ] limit an integer that defines number of Group By values
156
+ # returned (default: 100). The maximum limit is 1000.
157
+ #
158
+ # @return [ Faraday::Response ]
159
+ def segmentation(event, start_time, end_time, **options)
160
+ Faraday.get SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: {
161
+ e: event.to_json,
162
+ m: options[:m],
163
+ start: start_time.strftime("%Y%m%d"),
164
+ end: end_time.strftime("%Y%m%d"),
165
+ i: options[:i],
166
+ s: (options[:s] || []).map(&:to_json),
167
+ g: options[:g],
168
+ limit: options[:limit]
169
+ }.delete_if { |_, value| value.nil? }
170
+ end
171
+
172
+ # Delete a user from amplitude
173
+ #
174
+ # You must pass in either an array of user_ids or an array of amplitude_ids
175
+ #
176
+ # @param [ Array<String> ] (optional) the user_ids to delete
177
+ # based on your database
178
+ # @param [ Array<Integer> ] (optional) the amplitude_ids to delete
179
+ # based on the amplitude database
180
+ # @param [ String ] requester the email address of the person who
181
+ # is requesting the deletion, optional but useful for reporting
182
+ # @param [ Boolean ] (optional) ignore any invalid user IDs(users that do no
183
+ # exist in the project) that were passed in
184
+ # @param [ Boolean ] (optional) delete from the entire org rather than just
185
+ # this project.
186
+ # @return [ Faraday::Response ]
187
+ def delete(user_ids: nil, amplitude_ids: nil, requester: nil, ignore_invalid_id: nil, delete_from_org: nil)
188
+ user_ids = Array(user_ids)
189
+ amplitude_ids = Array(amplitude_ids)
190
+
191
+ faraday = Faraday.new do |conn|
192
+ conn.basic_auth config.api_key, config.secret_key
193
+ end
194
+
195
+ faraday.post(
196
+ DELETION_URI_STRING,
197
+ delete_body(user_ids, amplitude_ids, requester, ignore_invalid_id, delete_from_org),
198
+ "Content-Type" => "application/json"
199
+ )
200
+ end
201
+
202
+ private
203
+
204
+ def delete_body(user_ids, amplitude_ids, requester, ignore_invalid_id, delete_from_org)
205
+ body = {
206
+ amplitude_ids: amplitude_ids,
207
+ user_ids: user_ids,
208
+ requester: requester
209
+ }.delete_if { |_, value| value.nil? || value.empty? }
210
+
211
+ body[:ignore_invalid_id] = ignore_invalid_id.to_s if ignore_invalid_id
212
+ body[:delete_from_org] = delete_from_org.to_s if delete_from_org
213
+ JSON.generate(body)
116
214
  end
117
215
  end
118
216
  end