Class: AmplitudeAPI

Inherits:
Object
  • Object
show all
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

This class is 115 lines long. It's on the limit, it should be refactored before including more code.

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Config, Event, Identification

Constant Summary collapse

TRACK_URI_STRING =
"https://api2.amplitude.com/2/httpapi"
IDENTIFY_URI_STRING =
"https://api2.amplitude.com/identify"
SEGMENTATION_URI_STRING =
"https://amplitude.com/api/2/events/segmentation"
DELETION_URI_STRING =
"https://amplitude.com/api/2/deletions/users"
USER_WITH_NO_ACCOUNT =
"user who doesn't have an account"
VERSION =
"0.4.2"

Class Method Summary collapse

Class Method Details

.api_keyObject



28
29
30
# File 'lib/amplitude_api.rb', line 28

def api_key
  config.api_key
end

.configObject



20
21
22
# File 'lib/amplitude_api.rb', line 20

def config
  Config.instance
end

.configure {|config| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/amplitude_api.rb', line 24

def configure
  yield config
end

.delete(user_ids: nil, amplitude_ids: nil, requester: nil, ignore_invalid_id: nil, delete_from_org: nil) ⇒ Faraday::Response

Delete a user from amplitude

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 exist in the project) that were passed in this project.

Parameters:

  • (optional) (Array<String>)

    the user_ids to delete

  • (optional) (Array<Integer>)

    the amplitude_ids to delete

  • requester (String) (defaults to: nil)

    the email address of the person who

  • (optional) (Boolean)

    ignore any invalid user IDs(users that do no

  • (optional) (Boolean)

    delete from the entire org rather than just

Returns:

  • (Faraday::Response)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 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

.identify(identification) ⇒ Faraday::Response .identify([identifications]) ⇒ Faraday::Response

Send one or more Identifications to the Amplitude Identify API

Overloads:

  • .identify(identification) ⇒ Faraday::Response

    Parameters:

    • Send (AmplitudeAPI::Identify)

      a single identify to the Amplitude API

  • .identify([identifications]) ⇒ Faraday::Response

    Parameters:

    • Send (Array<AmplitudeAPI::Identify>)

      an array of identifications in a single request to Amplitude

Returns:

  • (Faraday::Response)


134
135
136
# File 'lib/amplitude_api.rb', line 134

def identify(*identifications)
  Faraday.post(IDENTIFY_URI_STRING, 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

Overloads:

Returns:

  • (Hash)


116
117
118
119
120
121
122
123
# 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

.secret_keyObject



32
33
34
# File 'lib/amplitude_api.rb', line 32

def secret_key
  config.secret_key
end

.segmentation(event, start_time, end_time, **options) ⇒ Faraday::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.

Parameters:

  • event (Hash)

    a hash that defines event.

  • start_time (Time)

    a start time.

  • end_time (Time)

    a end time.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • m (String)

    a string that defines aggregate function.

  • i (Integer)

    an integer that defines segmentation interval.

  • s (Array)

    an array that defines segment definitions.

  • g (String)

    a string that defines property to group by.

  • limit (Integer)

    an integer that defines number of Group By values

Returns:

  • (Faraday::Response)


159
160
161
162
163
164
165
166
167
168
169
170
# 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_event(event_name, user, device, options = {}) ⇒ Faraday::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

Parameters:

  • event_name (String)

    a string that describes the event, e.g. "clicked on Home"

  • user (String)

    a string or integer that uniquely identifies a user.

  • device (String)

    a string that uniquely identifies the device.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • event_properties (Hash)

    a hash that is serialized to JSON,

  • user_properties (Hash)

    a hash that is serialized to JSON,

Returns:

  • (Faraday::Response)


49
50
51
52
53
54
55
56
57
58
# 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

.send_identify(user_id, device_id, user_properties = {}) ⇒ Object

==== Identification related methods



97
98
99
100
101
102
103
104
# 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

.track(event) ⇒ Faraday::Response .track([events]) ⇒ Faraday::Response

Send one or more Events to the Amplitude API

Overloads:

  • .track(event) ⇒ Faraday::Response

    Parameters:

  • .track([events]) ⇒ Faraday::Response

    Parameters:

Returns:

  • (Faraday::Response)


91
92
93
# File 'lib/amplitude_api.rb', line 91

def track(*events)
  Faraday.post(TRACK_URI_STRING, track_body(events), "Content-Type" => "application/json")
end

.track_body(event) ⇒ Hash .track_body([events]) ⇒ Hash

Converts a series of AmplitudeAPI::Event objects into a body suitable for the Amplitude API

Overloads:

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
80
# 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