Class: AmplitudeAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/amplitude-api.rb,
lib/amplitude-api/event.rb,
lib/amplitude-api/version.rb

Defined Under Namespace

Classes: Event

Constant Summary collapse

URI_STRING =
"https://api.amplitude.com/httpapi"
USER_WITH_NO_ACCOUNT =
"user who doesn't have an account"
VERSION =
'0.0.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyString

Returns an Amplitude API Key.

Returns:

  • (String)

    an Amplitude API Key



14
15
16
# File 'lib/amplitude-api.rb', line 14

def api_key
  @api_key
end

Class Method Details

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

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

Overloads:

Returns:

  • (Hash)


38
39
40
41
42
43
44
45
46
# File 'lib/amplitude-api.rb', line 38

def body(*events)
  event_body = events.flatten.map do |event|
    event.to_hash
  end
  post_body = {
    api_key: self.api_key,
    event: JSON.generate(event_body)
  }
end

.send_event(event_name, user, properties = {}) ⇒ Typhoeus::Response

Send a single event immediately to the AmplitudeAPI

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.

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

    a hash that is serialized to JSON, and can contain any other property to be stored on the Event

Returns:

  • (Typhoeus::Response)


23
24
25
26
# File 'lib/amplitude-api.rb', line 23

def send_event(event_name, user, properties = {})
  event = AmplitudeAPI::Event.new(user_id: user, event_type: event_name, event_properties: properties)
  track(event)
end

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

Send one or more Events to the Amplitude API

Overloads:

  • .track(event) ⇒ Typhoeus::Response

    Parameters:

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

    Parameters:

Returns:

  • (Typhoeus::Response)


57
58
59
# File 'lib/amplitude-api.rb', line 57

def track(*events)
  Typhoeus.post(URI_STRING, body: body(events))
end