Class: AmplitudeAnalytics::EventOptions

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

Overview

EventOptions

Direct Known Subclasses

BaseEvent

Constant Summary collapse

EVENT_KEY_MAPPING =
{
  'user_id' => ['user_id', String],
  'device_id' => ['device_id', String],
  'event_type' => ['event_type', String],
  'time' => ['time', Integer],
  'event_properties' => ['event_properties', Hash],
  'user_properties' => ['user_properties', Hash],
  'groups' => ['groups', Hash],
  'app_version' => ['app_version', String],
  'platform' => ['platform', String],
  'os_name' => ['os_name', String],
  'os_version' => ['os_version', String],
  'device_brand' => ['device_brand', String],
  'device_manufacturer' => ['device_manufacturer', String],
  'device_model' => ['device_model', String],
  'carrier' => ['carrier', String],
  'country' => ['country', String],
  'region' => ['region', String],
  'city' => ['city', String],
  'dma' => ['dma', String],
  'language' => ['language', String],
  'price' => ['price', Float],
  'quantity' => ['quantity', Integer],
  'revenue' => ['revenue', Float],
  'product_id' => ['productId', String],
  'revenue_type' => ['revenueType', String],
  'location_lat' => ['location_lat', Float],
  'location_lng' => ['location_lng', Float],
  'ip' => ['ip', String],
  'idfa' => ['idfa', String],
  'idfv' => ['idfv', String],
  'adid' => ['adid', String],
  'android_id' => ['android_id', String],
  'event_id' => ['event_id', Integer],
  'session_id' => ['session_id', Integer],
  'insert_id' => ['insert_id', String],
  'library' => ['library', String],
  'ingestion_metadata' => ['ingestion_metadata', IngestionMetadata],
  'group_properties' => ['group_properties', Hash],
  'partner_id' => ['partner_id', String],
  'version_name' => ['version_name', String]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id: nil, device_id: nil, time: nil, event_properties: nil, user_properties: nil, groups: nil, app_version: nil, platform: nil, os_name: nil, os_version: nil, device_brand: nil, device_manufacturer: nil, device_model: nil, carrier: nil, country: nil, region: nil, city: nil, dma: nil, language: nil, price: nil, quantity: nil, revenue: nil, product_id: nil, revenue_type: nil, location_lat: nil, location_lng: nil, ip: nil, idfa: nil, idfv: nil, adid: nil, android_id: nil, event_id: nil, session_id: nil, insert_id: nil, ingestion_metadata: nil, group_properties: nil, partner_id: nil, version_name: nil, callback: nil) ⇒ EventOptions

Returns a new instance of EventOptions.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/amplitude/event.rb', line 87

def initialize(user_id: nil, device_id: nil, time: nil, event_properties: nil,
               user_properties: nil, groups: nil, app_version: nil, platform: nil, os_name: nil,
               os_version: nil, device_brand: nil, device_manufacturer: nil, device_model: nil,
               carrier: nil, country: nil, region: nil, city: nil, dma: nil, language: nil,
               price: nil, quantity: nil, revenue: nil, product_id: nil, revenue_type: nil,
               location_lat: nil, location_lng: nil, ip: nil, idfa: nil, idfv: nil, adid: nil,
               android_id: nil, event_id: nil, session_id: nil, insert_id: nil,
               ingestion_metadata: nil, group_properties: nil, partner_id: nil, version_name: nil,
               callback: nil)
  @user_id = user_id
  @device_id = device_id
  @event_type = event_type
  @time = time
  @event_properties = event_properties
  @user_properties = user_properties
  @groups = groups
  @app_version = app_version
  @platform = platform
  @os_name = os_name
  @os_version = os_version
  @device_brand = device_brand
  @device_manufacturer = device_manufacturer
  @device_model = device_model
  @carrier = carrier
  @country = country
  @region = region
  @city = city
  @dma = dma
  @language = language
  @price = price
  @quantity = quantity
  @revenue = revenue
  @product_id = product_id
  @revenue_type = revenue_type
  @location_lat = location_lat
  @location_lng = location_lng
  @ip = ip
  @idfa = idfa
  @idfv = idfv
  @adid = adid
  @android_id = android_id
  @event_id = event_id
  @session_id = session_id
  @insert_id = insert_id
  @ingestion_metadata = 
  @group_properties = group_properties
  @partner_id = partner_id
  @version_name = version_name
  @event_callback = callback
  @retry = 0
end

Instance Attribute Details

#adidObject

Returns the value of attribute adid.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def adid
  @adid
end

#android_idObject

Returns the value of attribute android_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def android_id
  @android_id
end

#app_versionObject

Returns the value of attribute app_version.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def app_version
  @app_version
end

#carrierObject

Returns the value of attribute carrier.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def carrier
  @carrier
end

#cityObject

Returns the value of attribute city.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def city
  @city
end

#countryObject

Returns the value of attribute country.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def country
  @country
end

#device_brandObject

Returns the value of attribute device_brand.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def device_brand
  @device_brand
end

#device_idObject

Returns the value of attribute device_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def device_id
  @device_id
end

#device_manufacturerObject

Returns the value of attribute device_manufacturer.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def device_manufacturer
  @device_manufacturer
end

#device_modelObject

Returns the value of attribute device_model.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def device_model
  @device_model
end

#dmaObject

Returns the value of attribute dma.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def dma
  @dma
end

#event_idObject

Returns the value of attribute event_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def event_id
  @event_id
end

#event_propertiesObject

Returns the value of attribute event_properties.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def event_properties
  @event_properties
end

#event_typeObject

Returns the value of attribute event_type.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def event_type
  @event_type
end

#group_propertiesObject

Returns the value of attribute group_properties.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def group_properties
  @group_properties
end

#groupsObject

Returns the value of attribute groups.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def groups
  @groups
end

#idfaObject

Returns the value of attribute idfa.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def idfa
  @idfa
end

#idfvObject

Returns the value of attribute idfv.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def idfv
  @idfv
end

#ingestion_metadataObject

Returns the value of attribute ingestion_metadata.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def 
  @ingestion_metadata
end

#insert_idObject

Returns the value of attribute insert_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def insert_id
  @insert_id
end

#ipObject

Returns the value of attribute ip.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def ip
  @ip
end

#languageObject

Returns the value of attribute language.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def language
  @language
end

#libraryObject

Returns the value of attribute library.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def library
  @library
end

#location_latObject

Returns the value of attribute location_lat.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def location_lat
  @location_lat
end

#location_lngObject

Returns the value of attribute location_lng.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def location_lng
  @location_lng
end

#os_nameObject

Returns the value of attribute os_name.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def os_name
  @os_name
end

#os_versionObject

Returns the value of attribute os_version.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def os_version
  @os_version
end

#partner_idObject

Returns the value of attribute partner_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def partner_id
  @partner_id
end

#platformObject

Returns the value of attribute platform.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def platform
  @platform
end

#priceObject

Returns the value of attribute price.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def price
  @price
end

#product_idObject

Returns the value of attribute product_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def product_id
  @product_id
end

#quantityObject

Returns the value of attribute quantity.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def quantity
  @quantity
end

#regionObject

Returns the value of attribute region.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def region
  @region
end

#retryObject

Returns the value of attribute retry.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def retry
  @retry
end

#revenueObject

Returns the value of attribute revenue.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def revenue
  @revenue
end

#revenue_typeObject

Returns the value of attribute revenue_type.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def revenue_type
  @revenue_type
end

#session_idObject

Returns the value of attribute session_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def session_id
  @session_id
end

#timeObject

Returns the value of attribute time.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def time
  @time
end

#user_idObject

Returns the value of attribute user_id.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def user_id
  @user_id
end

#user_propertiesObject

Returns the value of attribute user_properties.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def user_properties
  @user_properties
end

#version_nameObject

Returns the value of attribute version_name.



79
80
81
# File 'lib/amplitude/event.rb', line 79

def version_name
  @version_name
end

Instance Method Details

#[](key) ⇒ Object



139
140
141
# File 'lib/amplitude/event.rb', line 139

def [](key)
  instance_variable_get("@#{key}")
end

#[]=(key, value) ⇒ Object



143
144
145
# File 'lib/amplitude/event.rb', line 143

def []=(key, value)
  send("#{key}=", value) if verify_property(key, value)
end

#callback(status_code, message = nil) ⇒ Object



220
221
222
# File 'lib/amplitude/event.rb', line 220

def callback(status_code, message = nil)
  @event_callback.call(self, status_code, message) if @event_callback.respond_to?(:call)
end

#event_bodyObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/amplitude/event.rb', line 201

def event_body
  event_body = {}
  EVENT_KEY_MAPPING.each do |key, mapping|
    next unless include?(key) && self[key]

    value = self[key]
    if value.is_a?(mapping[1])
      event_body[mapping[0]] = value
    else
      puts "#{self.class}.#{key} expected #{mapping[1]} but received #{value.class}."
    end
  end
  event_body['ingestion_metadata'] = @ingestion_metadata.body if @ingestion_metadata.respond_to?(:body)
  %w[user_properties event_properties group_properties].each do |properties|
    next unless event_body[properties]
  end
  AmplitudeAnalytics.truncate(event_body.sort.to_h)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/amplitude/event.rb', line 147

def include?(key)
  instance_variable_defined?("@#{key}") && !instance_variable_get("@#{key}").nil?
end

#to_sObject



224
225
226
# File 'lib/amplitude/event.rb', line 224

def to_s
  JSON.generate(event_body)
end

#valid_object?(obj) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
# File 'lib/amplitude/event.rb', line 175

def valid_object?(obj)
  obj.each do |key, value|
    return false unless valid_properties?(key, value)
  end
  true
end

#valid_properties?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/amplitude/event.rb', line 151

def valid_properties?(key, value)
  return false unless key.is_a?(String)

  if value.is_a?(Array)
    result = true
    value.each do |element|
      return false if element.is_a?(Array)

      if element.is_a?(Hash)
        result &&= valid_object?(element)
      elsif !element.is_a?(Numeric) && !element.is_a?(String) && !element.is_a?(TrueClass) && !element.is_a?(FalseClass)
        result = false
      end
      break unless result
    end
    return result
  end

  return valid_object?(value) if value.is_a?(Hash)

  value.is_a?(TrueClass) || value.is_a?(FalseClass) ||
    value.is_a?(Numeric) || value.is_a?(String) || value.is_a?(Symbol)
end

#verify_property(key, value) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/amplitude/event.rb', line 182

def verify_property(key, value)
  return true if value.nil?

  unless instance_variable_defined?("@#{key}")
    AmplitudeAnalytics.logger.error("Unexpected event property key: #{key}")
    return false
  end

  expected_type = EVENT_KEY_MAPPING[key][1]
  unless value.is_a?(expected_type)
    AmplitudeAnalytics.logger.error("Event property #{key} expected #{expected_type} but received #{value.class}.")
    return false
  end

  return valid_object?(value) if value.is_a?(Hash)

  true
end