Class: AmplitudeAPI::Event

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

Overview

AmplitudeAPI::Event

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Event

Create a new Event

See (Amplitude HTTP API Documentation)[https://amplitude.zendesk.com/hc/en-us/articles/204771828-HTTP-API] for a list of valid parameters and their types.


14
15
16
17
18
19
# File 'lib/amplitude_api/event.rb', line 14

def initialize(attributes = {})
  attributes.each do |k, v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end
  validate_arguments
end

Instance Method Details

#==(other) ⇒ true, false

Compares +to_hash+ for equality

Returns:

  • (true, false)

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

def ==(other)
  return false unless other.respond_to?(:to_h)

  to_h == other.to_h
end

#optional_propertiesHash

Returns Optional properties.

Returns:

  • (Hash)

    Optional properties


46
47
48
49
50
51
# File 'lib/amplitude_api/event.rb', line 46

def optional_properties
  %i[device_id time ip platform country insert_id].map do |prop|
    val = prop == :time ? formatted_time : send(prop)
    val ? [prop, val] : nil
  end.compact.to_h
end

#reserved_event?(type) ⇒ true, false

Returns true if the event type matches one reserved by Amplitude API.

Returns:

  • (true, false)

56
57
58
59
60
61
62
63
# File 'lib/amplitude_api/event.rb', line 56

def reserved_event?(type)
  ['[Amplitude] Start Session',
   '[Amplitude] End Session',
   '[Amplitude] Revenue',
   '[Amplitude] Revenue (Verified)',
   '[Amplitude] Revenue (Unverified)',
   '[Amplitude] Merged User'].include?(type)
end

#to_hashHash Also known as: to_h

Used for serialization and comparison

Returns:

  • (Hash)

    A serialized Event


33
34
35
36
37
38
39
40
41
42
# File 'lib/amplitude_api/event.rb', line 33

def to_hash
  event = {
    event_type: event_type,
    event_properties: formatted_event_properties,
    user_properties: formatted_user_properties
  }
  event[:user_id] = user_id if user_id
  event[:device_id] = device_id if device_id
  event.merge(optional_properties).merge(revenue_hash)
end

#user_id=(value) ⇒ Object


21
22
23
24
25
26
27
28
# File 'lib/amplitude_api/event.rb', line 21

def user_id=(value)
  @user_id =
    if value.respond_to?(:id)
      value.id
    else
      value || AmplitudeAPI::USER_WITH_NO_ACCOUNT
    end
end