class AmplitudeAPI::Event

AmplitudeAPI::Event

Public Class Methods

new(attributes = {}) click to toggle source

Create a new Event

See (Amplitude HTTP API Documentation) for a list of valid parameters and their types.

# File lib/amplitude_api/event.rb, line 18
def initialize(attributes = {})
  @extra_properties = []
  attributes.each do |k, v|
    send("#{k}=", v)
  end
  validate_arguments
end

Public Instance Methods

==(other) click to toggle source

@return [ true, false ]

Compares to_hash for equality

# File lib/amplitude_api/event.rb, line 117
def ==(other)
  return false unless other.respond_to?(:to_h)

  to_h == other.to_h
end
create_getter(attribute_name) click to toggle source
# File lib/amplitude_api/event.rb, line 46
def create_getter(attribute_name)
  self.class.send(:define_method, attribute_name.to_sym) do
    instance_variable_get("@#{attribute_name}")
  end
end
create_setter(attribute_name) click to toggle source
# File lib/amplitude_api/event.rb, line 40
def create_setter(attribute_name)
  self.class.send(:define_method, "#{attribute_name}=".to_sym) do |value|
    instance_variable_set("@#{attribute_name}", value)
  end
end
extra_properties() click to toggle source

@return [ Hash ] Extra properties

Returns optional properties (not belong to the API, are assigned by the user) This way, if the API is updated with new properties, the gem will be able to work with the new specification until the code is modified

# File lib/amplitude_api/event.rb, line 95
def extra_properties
  @extra_properties.map do |prop|
    val = send(prop)
    val ? [prop.to_sym, val] : nil
  end.compact.to_h
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/amplitude_api/event.rb, line 26
def method_missing(method_name, *args)
  super if block_given?
  super unless method_name.to_s.end_with? "="

  property_name = method_name.to_s.delete_suffix("=")

  @extra_properties << property_name

  create_setter property_name
  create_getter property_name

  send("#{property_name}=".to_sym, *args)
end
optional_properties() click to toggle source

@return [ Hash ] Optional properties

Returns optional properties (belong to the API but are optional)

# File lib/amplitude_api/event.rb, line 83
def optional_properties
  AmplitudeAPI::Config.optional_properties.map do |prop|
    val = prop == :time ? formatted_time : send(prop)
    val ? [prop, val] : nil
  end.compact.to_h
end
reserved_event?(type) click to toggle source

@return [ true, false ]

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

# File lib/amplitude_api/event.rb, line 105
def reserved_event?(type)
  ["[Amplitude] Start Session",
   "[Amplitude] End Session",
   "[Amplitude] Revenue",
   "[Amplitude] Revenue (Verified)",
   "[Amplitude] Revenue (Unverified)",
   "[Amplitude] Merged User"].include?(type)
end
respond_to_missing?(method_name, *args) click to toggle source
Calls superclass method
# File lib/amplitude_api/event.rb, line 52
def respond_to_missing?(method_name, *args)
  @extra_properties.include?(method_name) || @extra_properties.include?("#{method_name}=") || super
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [ Hash ] A serialized Event

Used for serialization and comparison

# File lib/amplitude_api/event.rb, line 68
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).merge(extra_properties)
end
Also aliased as: to_h
user_id=(value) click to toggle source
# File lib/amplitude_api/event.rb, line 56
def user_id=(value)
  @user_id =
    if value.respond_to?(:id)
      value.id
    else
      value || AmplitudeAPI::USER_WITH_NO_ACCOUNT
    end
end

Private Instance Methods

formatted_event_properties() click to toggle source
# File lib/amplitude_api/event.rb, line 129
def formatted_event_properties
  Config.instance.event_properties_formatter.call(event_properties)
end
formatted_time() click to toggle source
# File lib/amplitude_api/event.rb, line 125
def formatted_time
  Config.instance.time_formatter.call(time)
end
formatted_user_properties() click to toggle source
# File lib/amplitude_api/event.rb, line 133
def formatted_user_properties
  Config.instance.user_properties_formatter.call(user_properties)
end
getopt(options, key, default = nil) click to toggle source
# File lib/amplitude_api/event.rb, line 172
def getopt(options, key, default = nil)
  options.fetch(key.to_sym, options.fetch(key.to_s, default))
end
revenue_error_message() click to toggle source
# File lib/amplitude_api/event.rb, line 155
def revenue_error_message
  error_field = "product_id" if product_id
  error_field = "revenue_type" if revenue_type

  "You must provide a price or a revenue in order to use the field #{error_field}"
end
revenue_hash() click to toggle source
# File lib/amplitude_api/event.rb, line 162
def revenue_hash
  revenue_hash = {}
  revenue_hash[:productId] = product_id if product_id
  revenue_hash[:revenueType] = revenue_type if revenue_type
  revenue_hash[:quantity] = quantity if quantity
  revenue_hash[:price] = price if price
  revenue_hash[:revenue] = revenue if revenue
  revenue_hash
end
validate_arguments() click to toggle source
# File lib/amplitude_api/event.rb, line 137
def validate_arguments
  validate_required_arguments
  validate_revenue_arguments
end
validate_required_arguments() click to toggle source
# File lib/amplitude_api/event.rb, line 142
def validate_required_arguments
  raise ArgumentError, "You must provide user_id or device_id (or both)" unless user_id || device_id
  raise ArgumentError, "You must provide event_type" unless event_type
  raise ArgumentError, "Invalid event_type - cannot match a reserved event name" if reserved_event?(event_type)
end
validate_revenue_arguments() click to toggle source
# File lib/amplitude_api/event.rb, line 148
def validate_revenue_arguments
  return true if !revenue_type && !product_id
  return true if revenue || price

  raise ArgumentError, revenue_error_message
end