amplitude-api 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +2 -0
- data/Gemfile +7 -5
- data/Gemfile.lock +17 -0
- data/Rakefile +6 -2
- data/amplitude-api.gemspec +13 -13
- data/lib/amplitude-api.rb +4 -109
- data/lib/amplitude_api.rb +109 -0
- data/lib/{amplitude-api → amplitude_api}/event.rb +20 -7
- data/lib/{amplitude-api → amplitude_api}/identification.rb +5 -4
- data/lib/amplitude_api/version.rb +3 -0
- data/readme.md +1 -1
- data/spec/lib/amplitude-api/event_spec.rb +48 -14
- data/spec/lib/amplitude-api/identification_spec.rb +16 -10
- data/spec/lib/amplitude_api_spec.rb +160 -63
- data/spec/spec_helper.rb +1 -1
- metadata +7 -5
- data/lib/amplitude-api/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fa498ab2765d7b0e14d78aacd3caf34d212abde
|
4
|
+
data.tar.gz: c2cf793047298e2d0679c2ec748eae6d9ae46727
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b73383081f15f0bca95841663f0d58b9bcf863c5a183b18b52400a3148d406721ba21dd8fc6ffaf3ff1debfffe86475d25eee9b623766ac31052579526536416
|
7
|
+
data.tar.gz: 9bada553602d018841b12bbd6836f338971359316d3852fe4147e08f4b9d8f35918f221e8bfa59cedc877c673b76714c43c9dd3b98ffa9cf9adc82d60ab9fd76
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# Generated from /Users/alex/development/amplitude-api/amplitude-api.gemspec
|
2
2
|
source 'https://rubygems.org'
|
3
|
-
gem "typhoeus", "~> 0.7.1"
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
gem
|
4
|
+
gem 'typhoeus', '~> 0.7.1'
|
5
|
+
|
6
|
+
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
|
9
11
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
GEM
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
+
ast (2.2.0)
|
4
5
|
coderay (1.1.0)
|
5
6
|
diff-lcs (1.2.5)
|
6
7
|
ethon (0.7.3)
|
7
8
|
ffi (>= 1.3.0)
|
8
9
|
ffi (1.9.8)
|
9
10
|
method_source (0.8.2)
|
11
|
+
parser (2.3.0.6)
|
12
|
+
ast (~> 2.2)
|
13
|
+
powerpack (0.1.1)
|
10
14
|
pry (0.9.12.6)
|
11
15
|
coderay (~> 1.0)
|
12
16
|
method_source (~> 0.8)
|
13
17
|
slop (~> 3.4)
|
18
|
+
rainbow (2.1.0)
|
14
19
|
rake (10.4.2)
|
15
20
|
rspec (2.99.0)
|
16
21
|
rspec-core (~> 2.99.0)
|
@@ -20,9 +25,17 @@ GEM
|
|
20
25
|
rspec-expectations (2.99.2)
|
21
26
|
diff-lcs (>= 1.1.3, < 2.0)
|
22
27
|
rspec-mocks (2.99.3)
|
28
|
+
rubocop (0.37.2)
|
29
|
+
parser (>= 2.3.0.4, < 3.0)
|
30
|
+
powerpack (~> 0.1)
|
31
|
+
rainbow (>= 1.99.1, < 3.0)
|
32
|
+
ruby-progressbar (~> 1.7)
|
33
|
+
unicode-display_width (~> 0.3)
|
34
|
+
ruby-progressbar (1.7.5)
|
23
35
|
slop (3.5.0)
|
24
36
|
typhoeus (0.7.1)
|
25
37
|
ethon (>= 0.7.1)
|
38
|
+
unicode-display_width (0.3.1)
|
26
39
|
|
27
40
|
PLATFORMS
|
28
41
|
ruby
|
@@ -31,4 +44,8 @@ DEPENDENCIES
|
|
31
44
|
pry (~> 0.9.12.6)
|
32
45
|
rake (>= 10.0)
|
33
46
|
rspec (>= 2.99.0)
|
47
|
+
rubocop (~> 0.37.2)
|
34
48
|
typhoeus (~> 0.7.1)
|
49
|
+
|
50
|
+
BUNDLED WITH
|
51
|
+
1.10.6
|
data/Rakefile
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rubocop/rake_task'
|
3
|
+
|
4
|
+
RuboCop::RakeTask.new
|
2
5
|
|
3
6
|
begin
|
4
7
|
require 'rspec/core/rake_task'
|
5
8
|
RSpec::Core::RakeTask.new(:spec)
|
6
9
|
rescue LoadError
|
10
|
+
puts 'Unable to load rspec. Have you run `bundle install`?'
|
7
11
|
end
|
8
12
|
|
9
13
|
task(:default).clear
|
10
|
-
task :
|
14
|
+
task default: ['rubocop:auto_correct', :spec]
|
data/amplitude-api.gemspec
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'amplitude_api/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'amplitude-api'
|
8
8
|
spec.version = AmplitudeAPI::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
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'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_dependency
|
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', '~> 0.7.1'
|
25
25
|
spec.required_ruby_version = '~> 2.0'
|
26
26
|
end
|
data/lib/amplitude-api.rb
CHANGED
@@ -1,109 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require 'amplitude-api/identification'
|
6
|
-
|
7
|
-
class AmplitudeAPI
|
8
|
-
TRACK_URI_STRING = "https://api.amplitude.com/httpapi"
|
9
|
-
IDENTIFY_URI_STRING = "https://api.amplitude.com/identify"
|
10
|
-
|
11
|
-
USER_WITH_NO_ACCOUNT = "user who doesn't have an account"
|
12
|
-
|
13
|
-
class << self
|
14
|
-
# @!attribute [ rw ] api_key
|
15
|
-
# @return [ String ] an Amplitude API Key
|
16
|
-
attr_accessor :api_key
|
17
|
-
|
18
|
-
# ==== Event Tracking related methods
|
19
|
-
|
20
|
-
# Send a single event immediately to the AmplitudeAPI
|
21
|
-
#
|
22
|
-
# @param [ String ] event_name a string that describes the event, e.g. "clicked on Home"
|
23
|
-
# @param [ String ] user a string or integer that uniquely identifies a user.
|
24
|
-
# @param [ Hash ] properties a hash that is serialized to JSON, and can contain any other property to be stored on the Event
|
25
|
-
#
|
26
|
-
# @return [ Typhoeus::Response ]
|
27
|
-
def send_event(event_name, user, properties = {})
|
28
|
-
event = AmplitudeAPI::Event.new(user_id: user, event_type: event_name, event_properties: properties)
|
29
|
-
track(event)
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
# @overload track_body(event)
|
34
|
-
# @param [ AmplitudeAPI::Event ]
|
35
|
-
#
|
36
|
-
# @overload track_body([events])
|
37
|
-
# @param [ Array<AmplitudeAPI::Event> ]
|
38
|
-
#
|
39
|
-
# @return [ Hash ]
|
40
|
-
#
|
41
|
-
# Converts a series of AmplitudeAPI::Event objects into a body
|
42
|
-
# suitable for the Amplitude API
|
43
|
-
def track_body(*events)
|
44
|
-
event_body = events.flatten.map do |event|
|
45
|
-
event.to_hash
|
46
|
-
end
|
47
|
-
|
48
|
-
{
|
49
|
-
api_key: self.api_key,
|
50
|
-
event: JSON.generate(event_body)
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
|
55
|
-
# @overload track(event)
|
56
|
-
# @param [ AmplitudeAPI::Event ] Send a single event to the Amplitude API
|
57
|
-
#
|
58
|
-
# @overload track([events])
|
59
|
-
# @param [ Array<AmplitudeAPI::Event> ] Send an array of events in a single request to Amplitude
|
60
|
-
#
|
61
|
-
# @return [ Typhoeus::Response ]
|
62
|
-
#
|
63
|
-
# Send one or more Events to the Amplitude API
|
64
|
-
def track(*events)
|
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))
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
1
|
+
# rubocop:disable Style/FileName
|
2
|
+
require_relative 'amplitude_api'
|
3
|
+
# rubocop:enable Style/FileName
|
4
|
+
# Whoops.
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'typhoeus'
|
4
|
+
require_relative 'amplitude_api/event'
|
5
|
+
require_relative 'amplitude_api/identification'
|
6
|
+
|
7
|
+
# AmplitudeAPI
|
8
|
+
class AmplitudeAPI
|
9
|
+
TRACK_URI_STRING = 'https://api.amplitude.com/httpapi'.freeze
|
10
|
+
IDENTIFY_URI_STRING = 'https://api.amplitude.com/identify'.freeze
|
11
|
+
|
12
|
+
USER_WITH_NO_ACCOUNT = "user who doesn't have an account".freeze
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# @!attribute [ rw ] api_key
|
16
|
+
# @return [ String ] an Amplitude API Key
|
17
|
+
attr_accessor :api_key
|
18
|
+
|
19
|
+
# ==== Event Tracking related methods
|
20
|
+
|
21
|
+
# Send a single event immediately to the AmplitudeAPI
|
22
|
+
#
|
23
|
+
# @param [ String ] event_name a string that describes the event, e.g. "clicked on Home"
|
24
|
+
# @param [ String ] user a string or integer that uniquely identifies a user.
|
25
|
+
# @param [ Hash ] properties a hash that is serialized to JSON,
|
26
|
+
# and can contain any other property to be stored on the Event
|
27
|
+
#
|
28
|
+
# @return [ Typhoeus::Response ]
|
29
|
+
def send_event(event_name, user, properties = {})
|
30
|
+
event = AmplitudeAPI::Event.new(
|
31
|
+
user_id: user,
|
32
|
+
event_type: event_name,
|
33
|
+
event_properties: properties
|
34
|
+
)
|
35
|
+
track(event)
|
36
|
+
end
|
37
|
+
|
38
|
+
# @overload track_body(event)
|
39
|
+
# @param [ AmplitudeAPI::Event ]
|
40
|
+
#
|
41
|
+
# @overload track_body([events])
|
42
|
+
# @param [ Array<AmplitudeAPI::Event> ]
|
43
|
+
#
|
44
|
+
# @return [ Hash ]
|
45
|
+
#
|
46
|
+
# Converts a series of AmplitudeAPI::Event objects into a body
|
47
|
+
# suitable for the Amplitude API
|
48
|
+
def track_body(*events)
|
49
|
+
event_body = events.flatten.map(&:to_hash)
|
50
|
+
|
51
|
+
{
|
52
|
+
api_key: api_key,
|
53
|
+
event: JSON.generate(event_body)
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
# @overload track(event)
|
58
|
+
# @param [ AmplitudeAPI::Event ] Send a single event to the Amplitude API
|
59
|
+
#
|
60
|
+
# @overload track([events])
|
61
|
+
# @param [ Array<AmplitudeAPI::Event> ] Send an array of events in a single request to Amplitude
|
62
|
+
#
|
63
|
+
# @return [ Typhoeus::Response ]
|
64
|
+
#
|
65
|
+
# Send one or more Events to the Amplitude API
|
66
|
+
def track(*events)
|
67
|
+
Typhoeus.post(TRACK_URI_STRING, body: track_body(events))
|
68
|
+
end
|
69
|
+
|
70
|
+
# ==== Identification related methods
|
71
|
+
|
72
|
+
def send_identify(user_id, user_properties = {})
|
73
|
+
identification = AmplitudeAPI::Identification.new(user_id: user_id, user_properties: user_properties)
|
74
|
+
identify(identification)
|
75
|
+
end
|
76
|
+
|
77
|
+
# @overload identify_body(identification)
|
78
|
+
# @param [ AmplitudeAPI::Identification ]
|
79
|
+
#
|
80
|
+
# @overload identify_body([identifications])
|
81
|
+
# @param [ Array<AmplitudeAPI::Identification> ]
|
82
|
+
#
|
83
|
+
# @return [ Hash ]
|
84
|
+
#
|
85
|
+
# Converts a series of AmplitudeAPI::Identification objects into a body
|
86
|
+
# suitable for the Amplitude Identify API
|
87
|
+
def identify_body(*identifications)
|
88
|
+
identification_body = identifications.flatten.map(&:to_hash)
|
89
|
+
|
90
|
+
{
|
91
|
+
api_key: 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))
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
class AmplitudeAPI
|
2
|
+
# AmplitudeAPI::Event
|
2
3
|
class Event
|
3
4
|
# @!attribute [ rw ] user_id
|
4
5
|
# @return [ String ] the user_id to be sent to Amplitude
|
@@ -9,16 +10,21 @@ class AmplitudeAPI
|
|
9
10
|
# @!attribute [ rw ] event_properties
|
10
11
|
# @return [ String ] the event_properties to be attached to the Amplitude Event
|
11
12
|
attr_accessor :event_properties
|
13
|
+
# @!attribute [ rw ] time
|
14
|
+
# @return [ Time ] Time that the event occurred (defaults to now)
|
15
|
+
attr_accessor :time
|
12
16
|
|
13
17
|
# Create a new Event
|
14
18
|
#
|
15
19
|
# @param [ String ] user_id a user_id to associate with the event
|
16
20
|
# @param [ String ] event_type a name for the event
|
17
21
|
# @param [ Hash ] event_properties various properties to attach to the event
|
18
|
-
|
22
|
+
# @param [ Time ] Time that the event occurred (defaults to now)
|
23
|
+
def initialize(user_id: '', event_type: '', event_properties: {}, time: nil)
|
19
24
|
self.user_id = user_id
|
20
25
|
self.event_type = event_type
|
21
26
|
self.event_properties = event_properties
|
27
|
+
self.time = time
|
22
28
|
end
|
23
29
|
|
24
30
|
def user_id=(value)
|
@@ -34,11 +40,12 @@ class AmplitudeAPI
|
|
34
40
|
#
|
35
41
|
# Used for serialization and comparison
|
36
42
|
def to_hash
|
37
|
-
{
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
43
|
+
serialized_event = {}
|
44
|
+
serialized_event[:event_type] = event_type
|
45
|
+
serialized_event[:user_id] = user_id
|
46
|
+
serialized_event[:event_properties] = event_properties
|
47
|
+
serialized_event[:time] = formatted_time if time
|
48
|
+
serialized_event
|
42
49
|
end
|
43
50
|
|
44
51
|
# @return [ true, false ]
|
@@ -46,10 +53,16 @@ class AmplitudeAPI
|
|
46
53
|
# Compares +to_hash+ for equality
|
47
54
|
def ==(other)
|
48
55
|
if other.respond_to?(:to_hash)
|
49
|
-
|
56
|
+
to_hash == other.to_hash
|
50
57
|
else
|
51
58
|
false
|
52
59
|
end
|
53
60
|
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def formatted_time
|
65
|
+
time.to_i * 1_000
|
66
|
+
end
|
54
67
|
end
|
55
68
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
class AmplitudeAPI
|
2
|
+
# AmplitudeAPI::Identification
|
2
3
|
class Identification
|
3
4
|
# @!attribute [ rw ] user_id
|
4
5
|
# @return [ String ] the user_id to be sent to Amplitude
|
@@ -11,7 +12,7 @@ class AmplitudeAPI
|
|
11
12
|
#
|
12
13
|
# @param [ String ] user_id a user_id to associate with the identification
|
13
14
|
# @param [ Hash ] user_properties various properties to attach to the user identification
|
14
|
-
def initialize(user_id:
|
15
|
+
def initialize(user_id: '', user_properties: {})
|
15
16
|
self.user_id = user_id
|
16
17
|
self.user_properties = user_properties
|
17
18
|
end
|
@@ -30,8 +31,8 @@ class AmplitudeAPI
|
|
30
31
|
# Used for serialization and comparison
|
31
32
|
def to_hash
|
32
33
|
{
|
33
|
-
user_id:
|
34
|
-
user_properties:
|
34
|
+
user_id: user_id,
|
35
|
+
user_properties: user_properties
|
35
36
|
}
|
36
37
|
end
|
37
38
|
|
@@ -40,7 +41,7 @@ class AmplitudeAPI
|
|
40
41
|
# Compares +to_hash+ for equality
|
41
42
|
def ==(other)
|
42
43
|
if other.respond_to?(:to_hash)
|
43
|
-
|
44
|
+
to_hash == other.to_hash
|
44
45
|
else
|
45
46
|
false
|
46
47
|
end
|
data/readme.md
CHANGED
@@ -3,42 +3,76 @@ require 'spec_helper'
|
|
3
3
|
describe AmplitudeAPI::Event do
|
4
4
|
User = Struct.new(:id)
|
5
5
|
|
6
|
-
context
|
7
|
-
describe
|
6
|
+
context 'with a user object' do
|
7
|
+
describe '#body' do
|
8
8
|
it "populates with the user's id" do
|
9
|
-
event = AmplitudeAPI::Event.new(
|
9
|
+
event = AmplitudeAPI::Event.new(
|
10
|
+
user_id: User.new(123),
|
11
|
+
event_type: 'clicked on home'
|
12
|
+
)
|
10
13
|
expect(event.to_hash[:user_id]).to eq(123)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
15
|
-
context
|
16
|
-
describe
|
18
|
+
context 'with a user id' do
|
19
|
+
describe '#body' do
|
17
20
|
it "populates with the user's id" do
|
18
|
-
event = AmplitudeAPI::Event.new(
|
21
|
+
event = AmplitudeAPI::Event.new(
|
22
|
+
user_id: 123,
|
23
|
+
event_type: 'clicked on home'
|
24
|
+
)
|
19
25
|
expect(event.to_hash[:user_id]).to eq(123)
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
23
29
|
|
24
|
-
context
|
25
|
-
describe
|
26
|
-
it
|
27
|
-
event = AmplitudeAPI::Event.new(
|
30
|
+
context 'without a user' do
|
31
|
+
describe '#body' do
|
32
|
+
it 'populates with the unknown user' do
|
33
|
+
event = AmplitudeAPI::Event.new(
|
34
|
+
user_id: nil,
|
35
|
+
event_type: 'clicked on home'
|
36
|
+
)
|
28
37
|
expect(event.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
|
29
38
|
end
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
42
|
describe '#body' do
|
34
|
-
it
|
35
|
-
event = AmplitudeAPI::Event.new(
|
43
|
+
it 'includes the event type' do
|
44
|
+
event = AmplitudeAPI::Event.new(
|
45
|
+
user_id: 123,
|
46
|
+
event_type: 'clicked on home'
|
47
|
+
)
|
36
48
|
expect(event.to_hash[:event_type]).to eq('clicked on home')
|
37
49
|
end
|
38
50
|
|
39
|
-
it
|
40
|
-
event = AmplitudeAPI::Event.new(
|
51
|
+
it 'includes arbitrary properties' do
|
52
|
+
event = AmplitudeAPI::Event.new(
|
53
|
+
user_id: 123,
|
54
|
+
event_type: 'clicked on home',
|
55
|
+
event_properties: { abc: :def }
|
56
|
+
)
|
41
57
|
expect(event.to_hash[:event_properties]).to eq(abc: :def)
|
42
58
|
end
|
59
|
+
|
60
|
+
it 'includes a time for the event' do
|
61
|
+
time = Time.parse('2016-01-01 00:00:00 -0000')
|
62
|
+
event = AmplitudeAPI::Event.new(
|
63
|
+
user_id: 123,
|
64
|
+
event_type: 'clicked on home',
|
65
|
+
time: time
|
66
|
+
)
|
67
|
+
expect(event.to_hash[:time]).to eq(1_451_606_400_000)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'does not include time if it is not set' do
|
71
|
+
event = AmplitudeAPI::Event.new(
|
72
|
+
user_id: 123,
|
73
|
+
event_type: 'clicked on home'
|
74
|
+
)
|
75
|
+
expect(event.to_hash).not_to have_key(:time)
|
76
|
+
end
|
43
77
|
end
|
44
78
|
end
|
@@ -3,8 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe AmplitudeAPI::Identification do
|
4
4
|
User = Struct.new(:id)
|
5
5
|
|
6
|
-
context
|
7
|
-
describe
|
6
|
+
context 'with a user object' do
|
7
|
+
describe '#body' do
|
8
8
|
it "populates with the user's id" do
|
9
9
|
identification = AmplitudeAPI::Identification.new(user_id: User.new(123))
|
10
10
|
expect(identification.to_hash[:user_id]).to eq(123)
|
@@ -12,8 +12,8 @@ describe AmplitudeAPI::Identification do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
context
|
16
|
-
describe
|
15
|
+
context 'with a user id' do
|
16
|
+
describe '#body' do
|
17
17
|
it "populates with the user's id" do
|
18
18
|
identification = AmplitudeAPI::Identification.new(user_id: 123)
|
19
19
|
expect(identification.to_hash[:user_id]).to eq(123)
|
@@ -21,9 +21,9 @@ describe AmplitudeAPI::Identification do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
context
|
25
|
-
describe
|
26
|
-
it
|
24
|
+
context 'without a user' do
|
25
|
+
describe '#body' do
|
26
|
+
it 'populates with the unknown user' do
|
27
27
|
identification = AmplitudeAPI::Identification.new(user_id: nil)
|
28
28
|
expect(identification.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
|
29
29
|
end
|
@@ -31,13 +31,19 @@ describe AmplitudeAPI::Identification do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
describe '#body' do
|
34
|
-
it
|
34
|
+
it 'includes the user id' do
|
35
35
|
identification = AmplitudeAPI::Identification.new(user_id: 123)
|
36
36
|
expect(identification.to_hash[:user_id]).to eq(123)
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
40
|
-
identification = AmplitudeAPI::Identification.new(
|
39
|
+
it 'includes arbitrary user properties' do
|
40
|
+
identification = AmplitudeAPI::Identification.new(
|
41
|
+
user_id: 123,
|
42
|
+
user_properties: {
|
43
|
+
first_name: 'John',
|
44
|
+
last_name: 'Doe'
|
45
|
+
}
|
46
|
+
)
|
41
47
|
expect(identification.to_hash[:user_properties]).to eq(first_name: 'John', last_name: 'Doe')
|
42
48
|
end
|
43
49
|
end
|
@@ -5,11 +5,17 @@ describe AmplitudeAPI do
|
|
5
5
|
@user = Struct.new(:id).new(123)
|
6
6
|
end
|
7
7
|
|
8
|
-
describe
|
9
|
-
context
|
10
|
-
it
|
11
|
-
event = AmplitudeAPI::Event.new(
|
12
|
-
|
8
|
+
describe '.track' do
|
9
|
+
context 'with a single event' do
|
10
|
+
it 'sends the event to Amplitude' do
|
11
|
+
event = AmplitudeAPI::Event.new(
|
12
|
+
user_id: 123,
|
13
|
+
event_type: 'clicked on sign up'
|
14
|
+
)
|
15
|
+
body = {
|
16
|
+
api_key: AmplitudeAPI.api_key,
|
17
|
+
event: JSON.generate([event.to_hash])
|
18
|
+
}
|
13
19
|
|
14
20
|
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
|
15
21
|
|
@@ -17,11 +23,20 @@ describe AmplitudeAPI do
|
|
17
23
|
end
|
18
24
|
end
|
19
25
|
|
20
|
-
context
|
21
|
-
it
|
22
|
-
event = AmplitudeAPI::Event.new(
|
23
|
-
|
24
|
-
|
26
|
+
context 'with multiple events' do
|
27
|
+
it 'sends all events in a single request' do
|
28
|
+
event = AmplitudeAPI::Event.new(
|
29
|
+
user_id: 123,
|
30
|
+
event_type: 'clicked on sign up'
|
31
|
+
)
|
32
|
+
event2 = AmplitudeAPI::Event.new(
|
33
|
+
user_id: 456,
|
34
|
+
event_type: 'liked a widget'
|
35
|
+
)
|
36
|
+
body = {
|
37
|
+
api_key: AmplitudeAPI.api_key,
|
38
|
+
event: JSON.generate([event.to_hash, event2.to_hash])
|
39
|
+
}
|
25
40
|
|
26
41
|
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
|
27
42
|
|
@@ -30,11 +45,20 @@ describe AmplitudeAPI do
|
|
30
45
|
end
|
31
46
|
end
|
32
47
|
|
33
|
-
describe
|
34
|
-
context
|
35
|
-
it
|
36
|
-
identification = AmplitudeAPI::Identification.new(
|
37
|
-
|
48
|
+
describe '.identify' do
|
49
|
+
context 'with a single identification' do
|
50
|
+
it 'sends the identification to Amplitude' do
|
51
|
+
identification = AmplitudeAPI::Identification.new(
|
52
|
+
user_id: 123,
|
53
|
+
user_properties: {
|
54
|
+
first_name: 'John',
|
55
|
+
last_name: 'Doe'
|
56
|
+
}
|
57
|
+
)
|
58
|
+
body = {
|
59
|
+
api_key: AmplitudeAPI.api_key,
|
60
|
+
identification: JSON.generate([identification.to_hash])
|
61
|
+
}
|
38
62
|
|
39
63
|
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
|
40
64
|
|
@@ -42,11 +66,26 @@ describe AmplitudeAPI do
|
|
42
66
|
end
|
43
67
|
end
|
44
68
|
|
45
|
-
context
|
46
|
-
it
|
47
|
-
identification = AmplitudeAPI::Identification.new(
|
48
|
-
|
49
|
-
|
69
|
+
context 'with multiple identifications' do
|
70
|
+
it 'sends all identifications in a single request' do
|
71
|
+
identification = AmplitudeAPI::Identification.new(
|
72
|
+
user_id: 123,
|
73
|
+
user_properties: {
|
74
|
+
first_name: 'Julian',
|
75
|
+
last_name: 'Ponce'
|
76
|
+
}
|
77
|
+
)
|
78
|
+
identification2 = AmplitudeAPI::Identification.new(
|
79
|
+
user_id: 456,
|
80
|
+
user_properties: {
|
81
|
+
first_name: 'John',
|
82
|
+
last_name: 'Doe'
|
83
|
+
}
|
84
|
+
)
|
85
|
+
body = {
|
86
|
+
api_key: AmplitudeAPI.api_key,
|
87
|
+
identification: JSON.generate([identification.to_hash, identification2.to_hash])
|
88
|
+
}
|
50
89
|
|
51
90
|
expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
|
52
91
|
|
@@ -55,92 +94,150 @@ describe AmplitudeAPI do
|
|
55
94
|
end
|
56
95
|
end
|
57
96
|
|
58
|
-
describe
|
59
|
-
it
|
60
|
-
event = AmplitudeAPI::Event.new
|
61
|
-
expect(event.to_hash).to eq(
|
62
|
-
event_type:
|
63
|
-
user_id:
|
97
|
+
describe '.initializer ' do
|
98
|
+
it 'initializes event without parameter' do
|
99
|
+
event = AmplitudeAPI::Event.new
|
100
|
+
expect(event.to_hash).to eq(
|
101
|
+
event_type: '',
|
102
|
+
user_id: '',
|
64
103
|
event_properties: {}
|
65
|
-
|
104
|
+
)
|
66
105
|
end
|
67
106
|
|
68
|
-
it
|
69
|
-
event = AmplitudeAPI::Event.new(
|
70
|
-
expect(event.to_hash).to eq({
|
71
|
-
event_type: "test_event",
|
107
|
+
it 'initializes event with parameter' do
|
108
|
+
event = AmplitudeAPI::Event.new(
|
72
109
|
user_id: 123,
|
73
|
-
|
74
|
-
|
110
|
+
event_type: 'test_event',
|
111
|
+
event_properties: {
|
112
|
+
test_property: 1
|
113
|
+
}
|
114
|
+
)
|
115
|
+
expect(event.to_hash).to eq(
|
116
|
+
event_type: 'test_event',
|
117
|
+
user_id: 123,
|
118
|
+
event_properties: { test_property: 1 }
|
119
|
+
)
|
75
120
|
end
|
76
121
|
end
|
77
122
|
|
78
|
-
describe
|
79
|
-
it
|
80
|
-
event = AmplitudeAPI::Event.new(
|
123
|
+
describe '.send_event' do
|
124
|
+
it 'sends an event to AmplitudeAPI' do
|
125
|
+
event = AmplitudeAPI::Event.new(
|
126
|
+
user_id: @user,
|
127
|
+
event_type: 'test_event',
|
128
|
+
event_properties: { test_property: 1 }
|
129
|
+
)
|
81
130
|
expect(AmplitudeAPI).to receive(:track).with(event)
|
82
131
|
|
83
|
-
AmplitudeAPI.send_event(
|
132
|
+
AmplitudeAPI.send_event('test_event', @user, test_property: 1)
|
84
133
|
end
|
85
134
|
|
86
|
-
context
|
87
|
-
it
|
88
|
-
event = AmplitudeAPI::Event.new(
|
135
|
+
context 'the user is nil' do
|
136
|
+
it 'sends an event with the no account user' do
|
137
|
+
event = AmplitudeAPI::Event.new(
|
138
|
+
user_id: nil,
|
139
|
+
event_type: 'test_event',
|
140
|
+
event_properties: { test_property: 1 }
|
141
|
+
)
|
89
142
|
expect(AmplitudeAPI).to receive(:track).with(event)
|
90
143
|
|
91
|
-
AmplitudeAPI.send_event(
|
144
|
+
AmplitudeAPI.send_event('test_event', nil, test_property: 1)
|
92
145
|
end
|
93
146
|
end
|
94
147
|
|
95
|
-
context
|
96
|
-
it
|
97
|
-
event = AmplitudeAPI::Event.new(
|
148
|
+
context 'the user is a user_id' do
|
149
|
+
it 'sends an event to AmplitudeAPI' do
|
150
|
+
event = AmplitudeAPI::Event.new(
|
151
|
+
user_id: 123,
|
152
|
+
event_type: 'test_event',
|
153
|
+
event_properties: { test_property: 1 }
|
154
|
+
)
|
98
155
|
expect(AmplitudeAPI).to receive(:track).with(event)
|
99
156
|
|
100
|
-
AmplitudeAPI.send_event(
|
157
|
+
AmplitudeAPI.send_event('test_event', @user.id, test_property: 1)
|
101
158
|
end
|
102
159
|
end
|
103
160
|
end
|
104
161
|
|
105
|
-
describe
|
106
|
-
it
|
107
|
-
identification = AmplitudeAPI::Identification.new(
|
162
|
+
describe '.send_identify' do
|
163
|
+
it 'sends an identify to AmplitudeAPI' do
|
164
|
+
identification = AmplitudeAPI::Identification.new(
|
165
|
+
user_id: @user,
|
166
|
+
user_properties: {
|
167
|
+
first_name: 'John',
|
168
|
+
last_name: 'Doe'
|
169
|
+
}
|
170
|
+
)
|
108
171
|
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
109
172
|
|
110
|
-
AmplitudeAPI.send_identify(@user,
|
173
|
+
AmplitudeAPI.send_identify(@user, first_name: 'John', last_name: 'Doe')
|
111
174
|
end
|
112
175
|
|
113
|
-
context
|
114
|
-
it
|
115
|
-
identification = AmplitudeAPI::Identification.new(
|
176
|
+
context 'the user is nil' do
|
177
|
+
it 'sends an identify with the no account user' do
|
178
|
+
identification = AmplitudeAPI::Identification.new(
|
179
|
+
user_id: nil,
|
180
|
+
user_properties: {
|
181
|
+
first_name: 'John',
|
182
|
+
last_name: 'Doe'
|
183
|
+
}
|
184
|
+
)
|
116
185
|
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
117
186
|
|
118
|
-
AmplitudeAPI.send_identify(nil,
|
187
|
+
AmplitudeAPI.send_identify(nil, first_name: 'John', last_name: 'Doe')
|
119
188
|
end
|
120
189
|
end
|
121
190
|
|
122
|
-
context
|
123
|
-
it
|
124
|
-
identification = AmplitudeAPI::Identification.new(
|
191
|
+
context 'the user is a user_id' do
|
192
|
+
it 'sends an identify to AmplitudeAPI' do
|
193
|
+
identification = AmplitudeAPI::Identification.new(
|
194
|
+
user_id: 123,
|
195
|
+
user_properties: {
|
196
|
+
first_name: 'John',
|
197
|
+
last_name: 'Doe'
|
198
|
+
}
|
199
|
+
)
|
125
200
|
expect(AmplitudeAPI).to receive(:identify).with(identification)
|
126
201
|
|
127
|
-
AmplitudeAPI.send_identify(@user.id,
|
202
|
+
AmplitudeAPI.send_identify(@user.id, first_name: 'John', last_name: 'Doe')
|
128
203
|
end
|
129
204
|
end
|
130
205
|
end
|
131
206
|
|
132
|
-
describe
|
133
|
-
it
|
134
|
-
event = AmplitudeAPI::Event.new(
|
207
|
+
describe '#body' do
|
208
|
+
it 'should add an api key' do
|
209
|
+
event = AmplitudeAPI::Event.new(
|
210
|
+
user_id: @user,
|
211
|
+
event_type: 'test_event',
|
212
|
+
event_properties: {
|
213
|
+
test_property: 1
|
214
|
+
}
|
215
|
+
)
|
135
216
|
body = AmplitudeAPI.track_body(event)
|
136
217
|
expect(body[:api_key]).to eq('stub api key')
|
137
218
|
end
|
138
219
|
|
139
|
-
it
|
140
|
-
event = AmplitudeAPI::Event.new(
|
220
|
+
it 'should create an event' do
|
221
|
+
event = AmplitudeAPI::Event.new(
|
222
|
+
user_id: 23,
|
223
|
+
event_type: 'test_event',
|
224
|
+
event_properties: {
|
225
|
+
foo: 'bar'
|
226
|
+
}
|
227
|
+
)
|
141
228
|
body = AmplitudeAPI.track_body(event)
|
142
229
|
|
143
|
-
expected = JSON.generate(
|
230
|
+
expected = JSON.generate(
|
231
|
+
[
|
232
|
+
{
|
233
|
+
event_type: 'test_event',
|
234
|
+
user_id: 23,
|
235
|
+
event_properties: {
|
236
|
+
foo: 'bar'
|
237
|
+
}
|
238
|
+
}
|
239
|
+
]
|
240
|
+
)
|
144
241
|
expect(body[:event]).to eq(expected)
|
145
242
|
end
|
146
243
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.expand_path(File.join('..', '..', 'lib', '
|
1
|
+
require File.expand_path(File.join('..', '..', 'lib', 'amplitude_api'), __FILE__)
|
2
2
|
require 'pry'
|
3
3
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
4
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
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.5
|
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-02-
|
11
|
+
date: 2016-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -87,6 +87,7 @@ extra_rdoc_files: []
|
|
87
87
|
files:
|
88
88
|
- ".gitignore"
|
89
89
|
- ".rspec"
|
90
|
+
- ".rubocop.yml"
|
90
91
|
- ".travis.yml"
|
91
92
|
- ".yardopts"
|
92
93
|
- Gemfile
|
@@ -95,9 +96,10 @@ files:
|
|
95
96
|
- Rakefile
|
96
97
|
- amplitude-api.gemspec
|
97
98
|
- lib/amplitude-api.rb
|
98
|
-
- lib/
|
99
|
-
- lib/
|
100
|
-
- lib/
|
99
|
+
- lib/amplitude_api.rb
|
100
|
+
- lib/amplitude_api/event.rb
|
101
|
+
- lib/amplitude_api/identification.rb
|
102
|
+
- lib/amplitude_api/version.rb
|
101
103
|
- readme.md
|
102
104
|
- spec/lib/amplitude-api/event_spec.rb
|
103
105
|
- spec/lib/amplitude-api/identification_spec.rb
|