class AmplitudeAPI
This class is 115 lines long. It’s on the limit, it should be refactored before including more code.
rubocop:disable Metrics/ClassLength
Constants
- DELETION_URI_STRING
- IDENTIFY_URI_STRING
- SEGMENTATION_URI_STRING
- TRACK_URI_STRING
- USER_WITH_NO_ACCOUNT
- VERSION
Public Class Methods
# File lib/amplitude_api.rb, line 28 def api_key config.api_key end
# File lib/amplitude_api.rb, line 20 def config Config.instance end
# File lib/amplitude_api.rb, line 24 def configure yield config end
Delete a user from amplitude
You must pass in either an array of user_ids or an array of amplitude_ids
@param [ Array<String> ] (optional) the user_ids to delete based on your database @param [ Array<Integer> ] (optional) the amplitude_ids to delete based on the amplitude database @param [ String ] requester the email address of the person who is requesting the deletion, optional but useful for reporting @param [ Boolean ] (optional) ignore any invalid user IDs(users that do no exist in the project) that were passed in @param [ Boolean ] (optional) delete from the entire org rather than just this project. @return [ Faraday::Response ]
# File lib/amplitude_api.rb, line 187 def delete(user_ids: nil, amplitude_ids: nil, requester: nil, ignore_invalid_id: nil, delete_from_org: nil) user_ids = Array(user_ids) amplitude_ids = Array(amplitude_ids) faraday = Faraday.new do |conn| conn.request :basic_auth, config.api_key, config.secret_key end faraday.post( DELETION_URI_STRING, delete_body(user_ids, amplitude_ids, requester, ignore_invalid_id, delete_from_org), "Content-Type" => "application/json" ) end
@overload identify(identification)
@param [ AmplitudeAPI::Identify ] Send a single identify to the Amplitude API
@overload identify()
@param [ Array<AmplitudeAPI::Identify> ] Send an array of identifications in a single request to Amplitude
@return [ Faraday::Response ]
Send one or more Identifications to the Amplitude Identify API
# File lib/amplitude_api.rb, line 134 def identify(*identifications) Faraday.post(IDENTIFY_URI_STRING, identify_body(identifications)) end
@overload identify_body
(identification)
@param [ AmplitudeAPI::Identification ]
@overload identify_body()
@param [ Array<AmplitudeAPI::Identification> ]
@return [ Hash ]
Converts a series of AmplitudeAPI::Identification
objects into a body suitable for the Amplitude Identify API
# File lib/amplitude_api.rb, line 116 def identify_body(*identifications) identification_body = identifications.flatten.map(&:to_hash) { api_key: api_key, identification: JSON.generate(identification_body) } end
# File lib/amplitude_api.rb, line 32 def secret_key config.secret_key end
Get metrics for an event with segmentation.
@param [ Hash ] event a hash that defines event. @param [ Time ] start_time a start time. @param [ Time ] end_time a end time. @option options [ String ] m a string that defines aggregate function. 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). @option options [ Integer ] i an integer that defines segmentation interval. 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. @option options [ Array ] s an array that defines segment definitions. @option options [ String ] g a string that defines property to group by. @option options [ Integer ] limit an integer that defines number of Group By values returned (default: 100). The maximum limit is 1000.
@return [ Faraday::Response ]
# File lib/amplitude_api.rb, line 159 def segmentation(event, start_time, end_time, **options) Faraday.get SEGMENTATION_URI_STRING, userpwd: "#{api_key}:#{secret_key}", params: { e: event.to_json, m: options[:m], start: start_time.strftime("%Y%m%d"), end: end_time.strftime("%Y%m%d"), i: options[:i], s: (options[:s] || []).map(&:to_json), g: options[:g], limit: options[:limit] }.delete_if { |_, value| value.nil? } end
Send a single event immediately to the AmplitudeAPI
@param [ String ] event_name a string that describes the event, e.g. “clicked on Home” @param [ String ] user a string or integer that uniquely identifies a user. @param [ String ] device a string that uniquely identifies the device. @option options [ Hash ] event_properties a hash that is serialized to JSON, and can contain any other property to be stored on the Event
@option options [ Hash ] user_properties a hash that is serialized to JSON, and contains user properties to be associated with the user
@return [ Faraday::Response ]
# File lib/amplitude_api.rb, line 49 def send_event(event_name, user, device, options = {}) event = AmplitudeAPI::Event.new( user_id: user, device_id: device, event_type: event_name, event_properties: options.fetch(:event_properties, {}), user_properties: options.fetch(:user_properties, {}) ) track(event) end
Identification
related methods¶ ↑
# File lib/amplitude_api.rb, line 97 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
@overload track(event)
@param [ AmplitudeAPI::Event ] Send a single event to the Amplitude API
@overload track()
@param [ Array<AmplitudeAPI::Event> ] Send an array of events in a single request to Amplitude
@return [ Faraday::Response ]
Send one or more Events to the Amplitude API
# File lib/amplitude_api.rb, line 91 def track(*events) Faraday.post(TRACK_URI_STRING, track_body(events), "Content-Type" => "application/json") end
@overload track_body
(event)
@param [ AmplitudeAPI::Event ]
@overload track_body()
@param [ Array<AmplitudeAPI::Event> ]
@return [ Hash ]
Converts a series of AmplitudeAPI::Event
objects into a body suitable for the Amplitude API
# File lib/amplitude_api.rb, line 70 def track_body(*events) event_body = events.flatten.map(&:to_hash) body = { api_key: api_key, events: event_body } body[:options] = config.options if config.options JSON.generate(body) end
Private Class Methods
# File lib/amplitude_api.rb, line 204 def delete_body(user_ids, amplitude_ids, requester, ignore_invalid_id, delete_from_org) body = { amplitude_ids: amplitude_ids, user_ids: user_ids, requester: requester }.delete_if { |_, value| value.nil? || value.empty? } body[:ignore_invalid_id] = ignore_invalid_id.to_s if ignore_invalid_id body[:delete_from_org] = delete_from_org.to_s if delete_from_org JSON.generate(body) end