Module: Amplitude

Defined in:
lib/amplitude.rb,
lib/amplitude/errors.rb

Defined Under Namespace

Modules: Formatters, Jobs Classes: APIError, APIKeyError, AsyncError, Config, Error, Event, Formatter, HashSlicer

Constant Summary collapse

API_URI =
'https://api.amplitude.com/httpapi'

Class Method Summary collapse

Class Method Details

.api_key(options = {}) ⇒ Object



31
32
33
# File 'lib/amplitude.rb', line 31

def api_key(options = {})
  options.fetch(:api_key, Amplitude.config.api_key)
end

.async_enabled?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/amplitude.rb', line 55

def async_enabled?
  defined?(ActiveJob)
end

.configObject



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

def config
  Amplitude::Config.instance
end

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

Yields:



27
28
29
# File 'lib/amplitude.rb', line 27

def configure
  yield config
end

.create_event(data) ⇒ Object



35
36
37
# File 'lib/amplitude.rb', line 35

def create_event(data)
  data.is_a?(Event) ? data : Event.new(data)
end

.queue(data, options = {}) ⇒ Object

Raises:



50
51
52
53
# File 'lib/amplitude.rb', line 50

def queue(data, options = {})
  raise AsyncError unless async_enabled?
  Amplitude::Jobs::Default.perform_later(create_event(data).to_json, options)
end

.track(data, options = {}) ⇒ Object

Raises:



39
40
41
42
# File 'lib/amplitude.rb', line 39

def track(data, options = {})
  raise APIKeyError unless (key = api_key(options))
  HTTParty.post(API_URI, body: { api_key: key, event: create_event(data).to_json })
end

.track!(data, options = {}) ⇒ Object

Raises:



44
45
46
47
48
# File 'lib/amplitude.rb', line 44

def track!(data, options = {})
  response = track(data, options)
  raise APIError, response unless response.success?
  response
end