Class: AmplitudeAPI::Event

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

Overview

AmplitudeAPI::Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Event

Create a new Event

Parameters:

  • user_id (String)

    a user_id to associate with the event

  • device_id (String)

    a device_id to associate with the event

  • event_type (String)

    a name for the event

  • event_properties (Hash)

    various properties to attach to the event

  • Time (Time)

    that the event occurred (defaults to now)

  • price (Double)

    (optional, but required for revenue data) price of the item purchased

  • quantity (Integer)

    (optional, but required for revenue data) quantity of the item purchased

  • product_id (String)

    (optional) an identifier for the product.

  • revenue_type (String)

    (optional) type of revenue

  • IP (String)

    address of the user

  • insert_id (String)

    a unique identifier for the event



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/amplitude_api/event.rb', line 59

def initialize(attributes = {})
  self.user_id = getopt(attributes, :user_id, '')
  self.device_id = getopt(attributes, :device_id, nil)
  self.event_type = getopt(attributes, :event_type, '')
  self.event_properties = getopt(attributes, :event_properties, {})
  self.user_properties = getopt(attributes, :user_properties, {})
  self.time = getopt(attributes, :time)
  self.ip = getopt(attributes, :ip, '')
  self.insert_id = getopt(attributes, :insert_id)
  validate_revenue_arguments(attributes)
end

Instance Attribute Details

#device_idString

Returns the device_id to be sent to Amplitude.

Returns:

  • (String)

    the device_id to be sent to Amplitude



9
10
11
# File 'lib/amplitude_api/event.rb', line 9

def device_id
  @device_id
end

#event_propertiesString

Returns the event_properties to be attached to the Amplitude Event.

Returns:

  • (String)

    the event_properties to be attached to the Amplitude Event



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

def event_properties
  @event_properties
end

#event_typeString

Returns the event_type to be sent to Amplitude.

Returns:

  • (String)

    the event_type to be sent to Amplitude



12
13
14
# File 'lib/amplitude_api/event.rb', line 12

def event_type
  @event_type
end

#insert_idString

Returns the unique identifier to be sent to Amplitude.

Returns:

  • (String)

    the unique identifier to be sent to Amplitude



28
29
30
# File 'lib/amplitude_api/event.rb', line 28

def insert_id
  @insert_id
end

#ipString

Returns IP address of the user.

Returns:

  • (String)

    IP address of the user



24
25
26
# File 'lib/amplitude_api/event.rb', line 24

def ip
  @ip
end

#priceString

Returns (required for revenue data) price of the item purchased.

Returns:

  • (String)

    (required for revenue data) price of the item purchased



32
33
34
# File 'lib/amplitude_api/event.rb', line 32

def price
  @price
end

#product_idString

Returns an identifier for the product. (Note: you must send a price and quantity with this field).

Returns:

  • (String)

    an identifier for the product. (Note: you must send a price and quantity with this field)



40
41
42
# File 'lib/amplitude_api/event.rb', line 40

def product_id
  @product_id
end

#quantityString

Returns (required for revenue data, defaults to 1 if not specified) quantity of the item purchased.

Returns:

  • (String)

    (required for revenue data, defaults to 1 if not specified) quantity of the item purchased



36
37
38
# File 'lib/amplitude_api/event.rb', line 36

def quantity
  @quantity
end

#revenue_typeString

Returns type of revenue. (Note: you must send a price and quantity with this field).

Returns:

  • (String)

    type of revenue. (Note: you must send a price and quantity with this field)



44
45
46
# File 'lib/amplitude_api/event.rb', line 44

def revenue_type
  @revenue_type
end

#timeTime

Returns Time that the event occurred (defaults to now).

Returns:

  • (Time)

    Time that the event occurred (defaults to now)



21
22
23
# File 'lib/amplitude_api/event.rb', line 21

def time
  @time
end

#user_idString

Returns the user_id to be sent to Amplitude.

Returns:

  • (String)

    the user_id to be sent to Amplitude



6
7
8
# File 'lib/amplitude_api/event.rb', line 6

def user_id
  @user_id
end

#user_propertiesString

Returns the user_properties to be passed for the user.

Returns:

  • (String)

    the user_properties to be passed for the user



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

def user_properties
  @user_properties
end

Instance Method Details

#==(other) ⇒ true, false

Compares +to_hash+ for equality

Returns:

  • (true, false)


105
106
107
108
109
110
111
# File 'lib/amplitude_api/event.rb', line 105

def ==(other)
  if other.respond_to?(:to_hash)
    to_hash == other.to_hash
  else
    false
  end
end

#add_optional_properties(serialized_event) ⇒ Hash

Returns A serialized Event with optional properties.

Returns:

  • (Hash)

    A serialized Event with optional properties



94
95
96
97
98
99
100
# File 'lib/amplitude_api/event.rb', line 94

def add_optional_properties(serialized_event)
  serialized_event[:device_id] = device_id if device_id
  serialized_event[:time] = formatted_time if time
  serialized_event[:ip] = ip if ip
  serialized_event[:insert_id] = insert_id if insert_id
  serialized_event
end

#to_hashHash

Used for serialization and comparison

Returns:

  • (Hash)

    A serialized Event



83
84
85
86
87
88
89
90
91
# File 'lib/amplitude_api/event.rb', line 83

def to_hash
  serialized_event = {}
  serialized_event[:event_type] = event_type
  serialized_event[:user_id] = user_id
  serialized_event[:event_properties] = event_properties
  serialized_event[:user_properties] = user_properties
  serialized_event = add_optional_properties(serialized_event)
  serialized_event.merge(revenue_hash)
end