Class: AmplitudeAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/amplitude_api.rb,
lib/amplitude_api/event.rb,
lib/amplitude_api/version.rb,
lib/amplitude_api/identification.rb

Overview

AmplitudeAPI

Defined Under Namespace

Classes: Event, Identification

Constant Summary collapse

TRACK_URI_STRING =
'https://api.amplitude.com/httpapi'.freeze
IDENTIFY_URI_STRING =
'https://api.amplitude.com/identify'.freeze
USER_WITH_NO_ACCOUNT =
"user who doesn't have an account".freeze
VERSION =
'0.0.7'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyString

Returns an Amplitude API Key.

Returns:

  • (String)

    an Amplitude API Key



17
18
19
# File 'lib/amplitude_api.rb', line 17

def api_key
  @api_key
end

Class Method Details

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

Send one or more Identifications to the Amplitude Identify API

Overloads:

  • .identify(identification) ⇒ Typhoeus::Response

    Parameters:

    • Send (AmplitudeAPI::Identify)

      a single identify to the Amplitude API

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

    Parameters:

    • Send (Array<AmplitudeAPI::Identify>)

      an array of identifications in a single request to Amplitude

Returns:

  • (Typhoeus::Response)


108
109
110
# File 'lib/amplitude_api.rb', line 108

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

Overloads:

Returns:

  • (Hash)


90
91
92
93
94
95
96
97
# File 'lib/amplitude_api.rb', line 90

def identify_body(*identifications)
  identification_body = identifications.flatten.map(&:to_hash)

  {
    api_key: api_key,
    identification: JSON.generate(identification_body)
  }
end

.send_event(event_name, user, 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

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.

  • event_properties (Hash)

    a hash that is serialized to JSON,

  • user_properties (Hash)

    a hash that is serialized to JSON,

Returns:

  • (Typhoeus::Response)


31
32
33
34
35
36
37
38
39
# File 'lib/amplitude_api.rb', line 31

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

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

==== Identification related methods



75
76
77
78
# File 'lib/amplitude_api.rb', line 75

def send_identify(user_id, user_properties = {})
  identification = AmplitudeAPI::Identification.new(user_id: user_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

Overloads:

  • .track(event) ⇒ Typhoeus::Response

    Parameters:

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

    Parameters:

Returns:

  • (Typhoeus::Response)


69
70
71
# File 'lib/amplitude_api.rb', line 69

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

Overloads:

Returns:

  • (Hash)


51
52
53
54
55
56
57
58
# File 'lib/amplitude_api.rb', line 51

def track_body(*events)
  event_body = events.flatten.map(&:to_hash)

  {
    api_key: api_key,
    event: JSON.generate(event_body)
  }
end