Class: Itly::Plugin::Amplitude

Inherits:
Itly::Plugin show all
Defined in:
lib/itly/plugin/amplitude/amplitude.rb,
lib/itly/plugin/amplitude/options.rb,
lib/itly/plugin/amplitude/version.rb,
lib/itly/plugin/amplitude/call_options.rb

Overview

Amplitude plugin class for Itly SDK

Defined Under Namespace

Classes: CallOptions, Options

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, disabled: false) ⇒ Amplitude

Instantiate a new Plugin::Amplitude

Parameters:

  • api_key: (String)

    specify the Amplitude api key

  • disabled: (TrueClass/FalseClass) (defaults to: false)

    set to true to disable the plugin. Default to false



20
21
22
23
24
25
# 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

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



12
13
14
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 12

def disabled
  @disabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 12

def logger
  @logger
end

Instance Method Details

#idString

Get the plugin ID

Returns:

  • (String)

    plugin id



105
106
107
# File 'lib/itly/plugin/amplitude/amplitude.rb', line 105

def id
  'amplitude'
end

#identify(user_id:, properties: nil, options: nil) ⇒ Object

Identify a user

Raise an error if the response is not 200

Parameters:

  • user_id: (String)

    the id of the user in your application

  • properties: (Hash) (defaults to: nil)

    the properties containing user’s traits to pass to your application

  • options: (Itly::Plugin::Amplitude::IdentifyOptions) (defaults to: nil)

    the plugin specific options



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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:) ⇒ Object

Initialize AmplitudeApi client

Parameters:

  • options: (Itly::PluginOptions)

    plugins options



32
33
34
35
36
37
38
39
40
41
# 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

#nameObject

Amplitude specific plugin options class for calls to plugin methods



52
53
54
55
56
57
58
59
# File 'lib/itly/plugin/amplitude/call_options.rb', line 52

%w[Identify Group Page Track Alias].each do |name|
  class_eval(
    <<-EVAL, __FILE__, __LINE__ + 1
      class #{name}Options < CallOptions         # class IdentifyOptions < CallOptions
      end                                        # end
    EVAL
  )
end

#track(user_id:, event:, options: nil) ⇒ Object

Track an event

Raise an error if the response is not 200

Parameters:

  • user_id: (String)

    the id of the user in your application

  • event: (Event)

    the Event object to pass to your application

  • options: (Itly::Plugin::Amplitude::TrackOptions) (defaults to: nil)

    the plugin specific options



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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