Class: AmplitudeAPI
- Inherits:
-
Object
- Object
- AmplitudeAPI
- Defined in:
- lib/amplitude_api.rb,
lib/amplitude_api/event.rb,
lib/amplitude_api/config.rb,
lib/amplitude_api/version.rb,
lib/amplitude_api/identification.rb
Overview
AmplitudeAPI
Defined Under Namespace
Classes: Config, Event, Identification
Constant Summary collapse
- TRACK_URI_STRING =
'https://api.amplitude.com/httpapi'.freeze
- IDENTIFY_URI_STRING =
'https://api.amplitude.com/identify'.freeze
- SEGMENTATION_URI_STRING =
'https://amplitude.com/api/2/events/segmentation'.freeze
- DELETION_URI_STRING =
'https://amplitude.com/api/2/deletions/users'.freeze
- USER_WITH_NO_ACCOUNT =
"user who doesn't have an account".freeze
- VERSION =
'0.1.0'.freeze
Class Method Summary collapse
- .api_key ⇒ Object
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
-
.delete(user_ids: nil, amplitude_ids: nil, requester: nil) ⇒ Typhoeus::Response
Delete a user from amplitude when they request it to comply with GDPR You must pass in either an array of user_ids or an array of amplitude_ids.
-
.identify(*identifications) ⇒ Typhoeus::Response
Send one or more Identifications to the Amplitude Identify API.
-
.identify_body(*identifications) ⇒ Hash
Converts a series of AmplitudeAPI::Identification objects into a body suitable for the Amplitude Identify API.
- .secret_key ⇒ Object
-
.segmentation(event, start_time, end_time, **options) ⇒ Typhoeus::Response
Get metrics for an event with segmentation.
-
.send_event(event_name, user, device, options = {}) ⇒ Typhoeus::Response
Send a single event immediately to the AmplitudeAPI.
-
.send_identify(user_id, device_id, user_properties = {}) ⇒ Object
==== Identification related methods.
-
.track(*events) ⇒ Typhoeus::Response
Send one or more Events to the Amplitude API.
-
.track_body(*events) ⇒ Hash
Converts a series of AmplitudeAPI::Event objects into a body suitable for the Amplitude API.
Class Method Details
.api_key ⇒ Object
26 27 28 |
# File 'lib/amplitude_api.rb', line 26 def api_key config.api_key end |
.config ⇒ Object
18 19 20 |
# File 'lib/amplitude_api.rb', line 18 def config Config.instance end |
.configure {|config| ... } ⇒ Object
22 23 24 |
# File 'lib/amplitude_api.rb', line 22 def configure yield config end |
.delete(user_ids: nil, amplitude_ids: nil, requester: nil) ⇒ Typhoeus::Response
Delete a user from amplitude when they request it to comply with GDPR You must pass in either an array of user_ids or an array of amplitude_ids
based on your database based on the amplitude database is requesting the deletion, optional but useful for reporting
180 181 182 183 184 185 186 187 188 |
# File 'lib/amplitude_api.rb', line 180 def delete(user_ids: nil, amplitude_ids: nil, requester: nil) Typhoeus.post DELETION_URI_STRING, userpwd: "#{api_key}:#{config.secret_key}", body: { amplitude_ids: amplitude_ids, user_ids: user_ids, requester: requester }.delete_if { |_, value| value.nil? } end |
.identify(identification) ⇒ Typhoeus::Response .identify([identifications]) ⇒ Typhoeus::Response
Send one or more Identifications to the Amplitude Identify API
129 130 131 |
# File 'lib/amplitude_api.rb', line 129 def identify(*identifications) Typhoeus.post(IDENTIFY_URI_STRING, body: identify_body(identifications)) end |
.identify_body(identification) ⇒ Hash .identify_body([identifications]) ⇒ Hash
Converts a series of AmplitudeAPI::Identification objects into a body suitable for the Amplitude Identify API
111 112 113 114 115 116 117 118 |
# File 'lib/amplitude_api.rb', line 111 def identify_body(*identifications) identification_body = identifications.flatten.map(&:to_hash) { api_key: api_key, identification: JSON.generate(identification_body) } end |
.secret_key ⇒ Object
30 31 32 |
# File 'lib/amplitude_api.rb', line 30 def secret_key config.secret_key end |
.segmentation(event, start_time, end_time, **options) ⇒ Typhoeus::Response
Get metrics for an event with segmentation.
For non-property metrics: "uniques", "totals", "pct_dau", or "average" (default: "uniques"). For property metrics: "histogram", "sums", or "value_avg" (note: a valid "group_by" value is required in parameter e). Set to -300000, -3600000, 1, 7, or 30 for realtime, hourly, daily, weekly, and monthly counts, respectively (default: 1). Realtime segmentation is capped at 2 days, hourly segmentation is capped at 7 days, and daily at 365 days. returned (default: 100). The maximum limit is 1000.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/amplitude_api.rb', line 154 def segmentation(event, start_time, end_time, **) Typhoeus.get SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: { e: event.to_json, m: [:m], start: start_time.strftime('%Y%m%d'), end: end_time.strftime('%Y%m%d'), i: [:i], s: ([:s] || []).map(&:to_json), g: [:g], limit: [:limit] }.delete_if { |_, value| value.nil? } end |
.send_event(event_name, user, device, options = {}) ⇒ Typhoeus::Response
Send a single event immediately to the AmplitudeAPI
and can contain any other property to be stored on the Event and contains user properties to be associated with the user
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/amplitude_api.rb', line 47 def send_event(event_name, user, device, = {}) event = AmplitudeAPI::Event.new( user_id: user, device_id: device, event_type: event_name, event_properties: .fetch(:event_properties, {}), user_properties: .fetch(:user_properties, {}) ) track(event) end |
.send_identify(user_id, device_id, user_properties = {}) ⇒ Object
==== Identification related methods
92 93 94 95 96 97 98 99 |
# File 'lib/amplitude_api.rb', line 92 def send_identify(user_id, device_id, user_properties = {}) identification = AmplitudeAPI::Identification.new( user_id: user_id, device_id: device_id, user_properties: user_properties ) identify(identification) end |
.track(event) ⇒ Typhoeus::Response .track([events]) ⇒ Typhoeus::Response
Send one or more Events to the Amplitude API
86 87 88 |
# File 'lib/amplitude_api.rb', line 86 def track(*events) Typhoeus.post(TRACK_URI_STRING, body: track_body(events)) end |
.track_body(event) ⇒ Hash .track_body([events]) ⇒ Hash
Converts a series of AmplitudeAPI::Event objects into a body suitable for the Amplitude API
68 69 70 71 72 73 74 75 |
# File 'lib/amplitude_api.rb', line 68 def track_body(*events) event_body = events.flatten.map(&:to_hash) { api_key: api_key, event: JSON.generate(event_body) } end |