amplitude-api 0.0.9 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2613d299e9d407475b3a03eecb1633f0a5359b40
4
- data.tar.gz: 7a48cebc3d4c592cb894c5d2bf9b814f0ff55a70
3
+ metadata.gz: 0e82b9bf2580f2da3bd76a71cb172ca89d051d3f
4
+ data.tar.gz: 8d437b02ece1c511b0f5256ee81f4e4b94fa8494
5
5
  SHA512:
6
- metadata.gz: 14739845274080c65ef0afb13b9547a91191ded8a935fae3a99bd04ffac16eb6f632fe4f83d4e209d3ef55f0dd3cd4eedfe645f030836423f42c530ac401363c
7
- data.tar.gz: 1e6933e33ec4ae42076b57e8f9c6cc72797fb6cb25ce708f76182224f5fb02f4b4657fc00811e81211dcb23de1989879dda88ec4c5ec21ce98b96de9f1300fe3
6
+ metadata.gz: 0df83a07615c485e264d00522359ab188e93f462e9e30d77ca6053d0dbc771a534062afc2148b60025ca221eddf1378727a8bcac46f25ec2c5f8480a5dd6ab4f
7
+ data.tar.gz: e9506a8d285f98d90a80ea28b7e80a337f3749597dbcf3be95f170bb7ca1a66cb5cd810fcbffcb020041ccb620cdb5ee547f6bb382cefd1644f5d10639a25264
@@ -6,8 +6,9 @@ require_relative 'amplitude_api/identification'
6
6
 
7
7
  # AmplitudeAPI
8
8
  class AmplitudeAPI
9
- TRACK_URI_STRING = 'https://api.amplitude.com/httpapi'.freeze
10
- IDENTIFY_URI_STRING = 'https://api.amplitude.com/identify'.freeze
9
+ TRACK_URI_STRING = 'https://api.amplitude.com/httpapi'.freeze
10
+ IDENTIFY_URI_STRING = 'https://api.amplitude.com/identify'.freeze
11
+ SEGMENTATION_URI_STRING = 'https://amplitude.com/api/2/events/segmentation'.freeze
11
12
 
12
13
  USER_WITH_NO_ACCOUNT = "user who doesn't have an account".freeze
13
14
 
@@ -16,6 +17,10 @@ class AmplitudeAPI
16
17
  # @return [ String ] an Amplitude API Key
17
18
  attr_accessor :api_key
18
19
 
20
+ # @!attribute [ rw ] secret_key
21
+ # @return [ String ] an Amplitude Secret Key
22
+ attr_accessor :secret_key
23
+
19
24
  # ==== Event Tracking related methods
20
25
 
21
26
  # Send a single event immediately to the AmplitudeAPI
@@ -114,5 +119,39 @@ class AmplitudeAPI
114
119
  def identify(*identifications)
115
120
  Typhoeus.post(IDENTIFY_URI_STRING, body: identify_body(identifications))
116
121
  end
122
+
123
+ # ==== Event Segmentation related methods
124
+
125
+ # Get metrics for an event with segmentation.
126
+ #
127
+ # @param [ Hash ] e a hash that defines event.
128
+ # @param [ Time ] start_time a start time.
129
+ # @param [ Time ] end_time a end time.
130
+ # @param [ String ] m a string that defines aggregate function.
131
+ # For non-property metrics: "uniques", "totals", "pct_dau", or "average" (default: "uniques").
132
+ # For property metrics: "histogram", "sums", or "value_avg"
133
+ # (note: a valid "group_by" value is required in parameter e).
134
+ # @param [ Integer ] i an integer that defines segmentation interval.
135
+ # Set to -300000, -3600000, 1, 7, or 30 for realtime, hourly, daily, weekly,
136
+ # and monthly counts, respectively (default: 1). Realtime segmentation is capped at 2 days,
137
+ # hourly segmentation is capped at 7 days, and daily at 365 days.
138
+ # @param [ Array ] s an array that defines segment definitions.
139
+ # @param [ String ] g a string that defines property to group by.
140
+ # @param [ Integer ] limit an integer that defines number of Group By values
141
+ # returned (default: 100). The maximum limit is 1000.
142
+ #
143
+ # @return [ Typhoeus::Response ]
144
+ def segmentation(e, start_time, end_time, **options)
145
+ Typhoeus.get SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: {
146
+ e: e.to_json,
147
+ m: options[:m],
148
+ start: start_time.strftime('%Y%m%d'),
149
+ end: end_time.strftime('%Y%m%d'),
150
+ i: options[:i],
151
+ s: (options[:s] || []).map(&:to_json),
152
+ g: options[:g],
153
+ limit: options[:limit]
154
+ }.delete_if { |_, value| value.nil? }
155
+ end
117
156
  end
118
157
  end
@@ -1,3 +1,3 @@
1
1
  class AmplitudeAPI
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.0.10'.freeze
3
3
  end
@@ -369,6 +369,26 @@ describe AmplitudeAPI do
369
369
  end
370
370
  end
371
371
 
372
+ describe '.segmentation' do
373
+ let(:end_time) { Time.now }
374
+ let(:start_time) { end_time - 60 * 60 * 24 } # -1 day
375
+
376
+ it 'sends request to Amplitude' do
377
+ expect(Typhoeus).to receive(:get).with(AmplitudeAPI::SEGMENTATION_URI_STRING,
378
+ userpwd: "#{described_class.api_key}:#{described_class.secret_key}",
379
+ params: {
380
+ e: { event_type: 'my event' }.to_json,
381
+ start: start_time.strftime('%Y%m%d'),
382
+ end: end_time.strftime('%Y%m%d'),
383
+ s: [{ prop: 'foo', op: 'is', values: %w(bar) }.to_json]
384
+ })
385
+
386
+ described_class.segmentation({ event_type: 'my event' }, start_time, end_time,
387
+ s: [{ prop: 'foo', op: 'is', values: %w(bar) }]
388
+ )
389
+ end
390
+ end
391
+
372
392
  describe '#body' do
373
393
  it 'adds an api key' do
374
394
  event = AmplitudeAPI::Event.new(
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.9
4
+ version: 0.0.10
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-09-27 00:00:00.000000000 Z
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  requirements: []
127
127
  rubyforge_project:
128
- rubygems_version: 2.6.6
128
+ rubygems_version: 2.5.2
129
129
  signing_key:
130
130
  specification_version: 4
131
131
  summary: Send events to the Amplitude API