class Itly::Plugin::Amplitude

Amplitude plugin class for Itly SDK

Amplitude plugin class for Itly SDK

Constants

VERSION

Attributes

disabled[R]
logger[R]

Public Class Methods

new(api_key:, disabled: false) click to toggle source

Instantiate a new Plugin::Amplitude

@param [String] api_key: specify the Amplitude api key @param [TrueClass/FalseClass] disabled: set to true to disable the plugin. Default to false

Calls superclass method
# File lib/itly/plugin/amplitude/amplitude.rb, line 20
def initialize(api_key:, disabled: false)
  super()
  @disabled = disabled

  ::AmplitudeAPI.config.api_key = api_key
end

Public Instance Methods

id() click to toggle source

Get the plugin ID

@return [String] plugin id

# File lib/itly/plugin/amplitude/amplitude.rb, line 105
def id
  'amplitude'
end
identify(user_id:, properties: nil, options: nil) click to toggle source

Identify a user

Raise an error if the response is not 200

@param [String] user_id: the id of the user in your application @param [Hash] properties: the properties containing user's traits to pass to your application @param [Itly::Plugin::Amplitude::IdentifyOptions] options: the plugin specific options

Calls superclass method
# File lib/itly/plugin/amplitude/amplitude.rb, line 52
def identify(user_id:, properties: nil, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log user_id: user_id, properties: properties, options: options
  logger&.info "#{id}: identify(#{log})"

  # Send through the client
  payload = {}
  payload.merge! options.to_hash if options
  payload.merge! properties if properties

  call_end_point(options&.callback) do
    ::AmplitudeAPI.send_identify user_id, nil, payload
  end
end
load(options:) click to toggle source

Initialize AmplitudeApi client

@param [Itly::PluginOptions] options: plugins options

Calls superclass method
# File lib/itly/plugin/amplitude/amplitude.rb, line 32
def load(options:)
  super
  # Get options
  @logger = options.logger

  # Log
  logger&.info "#{id}: load()"

  logger&.info "#{id}: plugin is disabled!" if @disabled
end
track(user_id:, event:, options: nil) click to toggle source

Track an event

Raise an error if the response is not 200

@param [String] user_id: the id of the user in your application @param [Event] event: the Event object to pass to your application @param [Itly::Plugin::Amplitude::TrackOptions] options: the plugin specific options

Calls superclass method
# File lib/itly/plugin/amplitude/amplitude.rb, line 79
def track(user_id:, event:, options: nil)
  super
  return unless enabled?

  # Log
  log = Itly::Loggers.vars_to_log(
    user_id: user_id, event: event&.name, properties: event&.properties, options: options
  )
  logger&.info "#{id}: track(#{log})"

  # Send through the client
  call_end_point(options&.callback) do
    ::AmplitudeAPI.track ::AmplitudeAPI::Event.new(
      user_id: user_id,
      event_type: event.name,
      event_properties: event.properties,
      **(options&.to_hash || {})
    )
  end
end

Private Instance Methods

call_end_point(callback) { || ... } click to toggle source
# File lib/itly/plugin/amplitude/amplitude.rb, line 115
def call_end_point(callback)
  raise 'You need to give a block' unless block_given?

  # Call remote endpoint (Note: the AmplitudeAPI is using Faraday)
  response = yield

  # yield to the callback passed in to options
  callback&.call(response.status, response.body)

  # Return in case of success
  return if response.status >= 200 && response.status < 300

  # Raise in case of error
  message = "The remote end-point returned an error. Response status: #{response.status}. "\
    "Raw body: #{response.body}"
  raise Itly::RemoteError, message
end
enabled?() click to toggle source
# File lib/itly/plugin/amplitude/amplitude.rb, line 111
def enabled?
  !@disabled
end